Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Vowel/Constanent
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Thuged_Out_G




PostPosted: Sat Apr 29, 2006 12:01 am   Post subject: Vowel/Constanent

Was bored, and saw another user was asking how to do this .. so i decided to add it to turing. So it can be easily used.

This module will check a given letter if it is a vowel/consonant.


Check.tu
code:

unit
module Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        if index (w, "a") not= 0 or index (w, "e") not= 0 or index (w, "i") not= 0 or index (w, "o") not= 0 or index (w, "u") not= 0 or index (w, "A") not= 0 or index (w, "E") not= 0 or index (w, "I") not= 0 or index (w, "O") not= 0 or index (w, "U") not= 0 then
            result true
        else
            result false
        end if
    end Vowel

    fcn Cons (w : string) : boolean
        if index (w, "a") not= 0 or index (w, "e") not= 0 or index (w, "i") not= 0 or index (w, "o") not= 0 or index (w, "u") not= 0 or index (w, "A") not= 0 or index (w, "E") not= 0 or index (w, "I") not= 0 or index (w, "O") not= 0 or index (w, "U") not= 0 then
            result false
        else
            result true
        end if
    end Cons
end Check


Add that to "%oot/support/predefs", save as "check.tu"
Then add the line "%oot/support/predefs/Check.tu" to "%oot/support/predefs/predefs.lst" right at the end of the rest of the functions/procuderes

you can then call it by

code:

put Check.Vowel("E") %returns true
put Check.Vowel("B") %returns false
put Check.Cons("B") %returns true
put Check.Cons("E") %returns false


May be useful in some programs, im sure just about every compsci student in HS has had an assignment where that could have been useful at some point. Razz
Sponsor
Sponsor
Sponsor
sponsor
Thuged_Out_G




PostPosted: Sat Apr 29, 2006 12:11 am   Post subject: (No subject)

Wow, i spelt consonant wrong. In both the thread name and fcn Embarassed

Also, is there a way for (w:string) to be a variable number of arguments.
Like i think in ruby it is "*args". Is there anything like this in turing.
Cervantes




PostPosted: Sat Apr 29, 2006 6:39 am   Post subject: (No subject)

Why not just add this to the String module?

Thuged_Out_G wrote:

Also, is there a way for (w:string) to be a variable number of arguments.
Like i think in ruby it is "*args". Is there anything like this in turing.

That's right. *args in ruby, however, is basically an array.
code:

irb(main):001:0> def foo(*args)
irb(main):002:1>   p *args
irb(main):003:1>   p args
irb(main):004:1> end
=> nil
irb(main):005:0> foo(1, 2, 3)
1
2
3
[1, 2, 3]
=> nil

You can do a similar thing in Turing:
code:

proc foo (args : array 1 .. * of int)

However, all the arguments must be of the same type, and you'll have to make an array before hand to send to foo. Lots of extra work.

Also, it would be easier if you converted the word to all upper case or all lower case so you didn't have to check "e" and "E".
do_pete




PostPosted: Sat Apr 29, 2006 9:50 am   Post subject: (No subject)

You only really need the Vowel fcn because if it returns false then you know its a consonant
Delos




PostPosted: Sat Apr 29, 2006 11:58 am   Post subject: (No subject)

do_pete wrote:
You only really need the Vowel fcn because if it returns false then you know its a consonant


Quite true. If you *really* want to have both Vowel and Cons fcns, simply create a 'Check' fcn, and call that fcn from within the Vowel and Cons structures. The Check fcn would essentially do what is already being done.
Thuged_Out_G




PostPosted: Sat Apr 29, 2006 12:30 pm   Post subject: (No subject)

This sounds n00bish, but how do you change a letter to upper or lowercase?
Also, is there another way to do this? Other than using 'index'. I know i could use ord, IS there another option?
[Gandalf]




PostPosted: Sat Apr 29, 2006 12:36 pm   Post subject: (No subject)

Either use Str.Upper() and Str.Lower() or if you want to create the functions yourself read Cervantes' string manipulation tutorial which is found in the Turing Walkthrough.
TokenHerbz




PostPosted: Sat Apr 29, 2006 12:46 pm   Post subject: (No subject)

code:

for i: 65 .. 90
    put chr(i) ..
    put chr(i + 32) ..
end for


you can get the number of the letter with ord(letter)

EDIT: gandalf, my turing dosn't have "Str.Upper() and Str.Lower()"
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Apr 29, 2006 1:06 pm   Post subject: (No subject)

With a little work this code can be much more concise.

untested

code:
unit
module Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        var w2 : string = Str.Lower (w)
        var vowels : string = "aeiou"
       
        for i : 1..5
            if index (vowels (i), w2) not= 0 then
               result true
            end if
        end for
       
        result false
    end Vowel

    fcn Cons (w : string) : boolean
        var w2 : string = Str.Lower (w)
        var o : int = ord (w2)
   
        result (o >= 65 and o <= 90 and not Vowel (w2))
    end Cons
end Check
MysticVegeta




PostPosted: Mon May 01, 2006 1:57 pm   Post subject: (No subject)

hehe wtd messed up for the first time in history, the vowels is not an array wtd! the loop could simply be reduced by:
code:
if index(vowels, string) > 0 then
         return true
else
     return false

note: this is untested too

Ofcourse your way would work too, by cehcking each letter but one thing I learned, why use redundant code?

Note: I dont know what happened, why did it double post?? Could someone delete it please, i really dont know what happened, sorry guys
[Gandalf]




PostPosted: Tue May 02, 2006 1:18 am   Post subject: (No subject)

I would guess that wtd programs in Turing less than regularily, and he probably doesn't even have it installed, so any mistakes can hopefully be excused.

Anyway, ironically, you are wrong and wtd had it right. The way you had it the program would check if string contained every vowel in order. You should be checking to see if w2 contains any of the letters in vowels. I hope that by now you know that in Turing you access the character at a specified index in a string the same way you access the element at a specified index in an array.
Also, you got the arguments to the index function wrong, it's:
code:
index (string, vowels)


Now, some minor syntatic troubles in wtd's code:
code:
module Check
    import Str   %imported the Str module explicitly for use in Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        var w2 : string := Str.Lower (w)   %here and in a few other places the = was turned to Turing's :=
        var vowels : string := "aeiou"
       
        for i : 1..5
            if index (w2, vowels (i)) not= 0 then   %fixed syntax of index()
               result true
            end if
        end for
       
        result false
    end Vowel

    fcn Cons (w : string) : boolean
        var w2 : string := Str.Lower (w)
        var o : int := ord (w2)
   
        result (o >= 65 and o <= 90 and not Vowel (w2))
    end Cons
end Check
Thuged_Out_G




PostPosted: Tue May 02, 2006 2:00 am   Post subject: (No subject)

I can up with something a bit different. The reason for the "redundant" code, was because I only decided to write it out of boredom when i saw i post in turing help forum, for it i needed to know exzactly which letter was a vowel/consonant.

This one will tell you if its a vowel, consonant, non letter, or white space.

code:

fcn vowel (w : string) : string
    var w1 : string
    if ord (w) >= 65 and ord (w) <= 90 then                                             %if its in caps
        w1 := chr (ord (w) + 32)                                                        %make lower case
    else
        w1 := w
    end if
    if ord (w1) = 32 then                                                               %ord(" ")=32
        result "WS"                                                                     %white space
    elsif w1 = "a" or w1 = "e" or w1 = "i" or w1 = "o" or w1 = "u" then
        result "true"                                                                   %vowel
    elsif ord (w1) < 65 or ord (w1) > 122 or ord (w1) >= 91 and ord (w1) <= 96 then
        result "IC"                                                                     %illegal char
    else
        result "false"                                                                  %only thing left, consonant
    end if
end vowel



As advised, i got rid of the cons function ... Since like previously stated, was useless. My turing, also does not have Str.Lower(), Str.Upper() ... so i was stuck with using chr(ord()) to convert to lower case.
MysticVegeta




PostPosted: Tue May 02, 2006 5:37 pm   Post subject: (No subject)

[Gandalf] wrote:
I would guess that wtd programs in Turing less than regularily, and he probably doesn't even have it installed, so any mistakes can hopefully be excused.

Anyway, ironically, you are wrong and wtd had it right. The way you had it the program would check if string contained every vowel in order. You should be checking to see if w2 contains any of the letters in vowels. I hope that by now you know that in Turing you access the character at a specified index in a string the same way you access the element at a specified index in an array.
Also, you got the arguments to the index function wrong, it's:
code:
index (string, vowels)


Now, some minor syntatic troubles in wtd's code:
code:
module Check
    import Str   %imported the Str module explicitly for use in Check
    export Vowel, Cons
    fcn Vowel (w : string) : boolean
        var w2 : string := Str.Lower (w)   %here and in a few other places the = was turned to Turing's :=
        var vowels : string := "aeiou"
       
        for i : 1..5
            if index (w2, vowels (i)) not= 0 then   %fixed syntax of index()
               result true
            end if
        end for
       
        result false
    end Vowel

    fcn Cons (w : string) : boolean
        var w2 : string := Str.Lower (w)
        var o : int := ord (w2)
   
        result (o >= 65 and o <= 90 and not Vowel (w2))
    end Cons
end Check

oh sorry about that, I did actually say that wtd method works, I made a mistake in the order of index(string,vowels). Sorry about that.
PerylDemise




PostPosted: Wed May 03, 2006 11:07 pm   Post subject: (No subject)

Is there any way to achieve this without using units or modules?
[Gandalf]




PostPosted: Wed May 03, 2006 11:15 pm   Post subject: (No subject)

Yes, just remove the appropriate code from the module:
code:
fcn Vowel (w : string) : boolean
    var w2 : string := Str.Lower (w)
    var vowels : string := "aeiou"

    for i : 1 .. 5
        if index (w2, vowels (i)) not= 0 then
            result true
        end if
    end for

    result false
end Vowel

fcn Cons (w : string) : boolean
    var w2 : string := Str.Lower (w)
    var o : int := ord (w2)

    result (o >= 65 and o <= 90 and not Vowel (w2))
end Cons

But why?

MysticVegeta wrote:
I made a mistake in the order of index(string,vowels).

You also made the much larger error of checking the specified string variable for each vowel, in order, at once.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: