Thread: Help!!

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    10

    Help!!


    Say i wanna create a function called "notvowel" and it'll take a character as its argument. then if the character is a vowel, i want it to return "false". if it's not a vowel, then "true". And i need to do this for both upper and lower case..

    so far i have...

    vowel = "A"
    def notvowel(m):
    if m == vowel:
    return "false"
    else:
    return "true"

    but that works only if i type in print notvowel("A").. so i was wondering what do i need to do to get A E I O U and a e i o u in there..?

    Thanks~
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hi Jack,
    You cld do it this way....

    Code:
    >>> vowel='aeiouAEIOU'
    >>> def notVowel(m):
    ... 	if m in vowel:print m,'is a vowel!'
    ... 	else: print m,'is not a vowel!'
    output samples...........

    Code:
    >>> notVowel('r')
    r is not a vowel!
    >>> notVowel('e')
    e is a vowel!
    >>> notVowel('A')
    A is a vowel!

    Rgds,
    Subha
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    A slightly different approach to this would be to return a boolean value for the expression: character not in vowels

    Code:
    >>> vowels = 'aeiou'
    >>> 
    >>> def notVowel(character):
    ...     return character.lower() not in vowels
    ... 
    >>> notVowel('a')
    False
    >>> notVowel('b')
    True
    >>> notVowel('c')
    True
    >>>
    Besides being smaller this also gives you more controle than a hard coded if statment and makes the function more reusable . Although since this function is so small anyway you probably wouldn't need it; you could use the expression above.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19
    Just a thought: wouldn't it be better if vowels was a list, not a string?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I don't see why it would be...

    Memory:
    - A string takes up a string indicator plus an amount per character.
    - A list takes a list indicator, plus a type indicator per item, plus the item space, possibly plus overhead for locating the items in the list in memory.

    Searching: A string is iterable, so is a list. I don't know how 'in' does it's searching, so I don't know how to compare the two.

    Speed:
    Code:
    >>> from timeit import Timer
    >>> 
    >>> t1 = Timer("'I'.lower() in 'aeiou'")
    >>> t1.timeit()
    0.38837737019252927
    >>> 
    >>> t2 = Timer("'I' in 'aAeEiIoOuU'")
    >>> t2.timeit()
    0.14456659072571654
    >>> 
    >>> t3 = Timer("'I'.lower() in ['a', 'e', 'i', 'o', 'u']")
    >>> t3.timeit()
    1.1399176930540733
    >>> 
    >>> t4 = Timer("'I' in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']")
    >>> t4.timeit()
    1.15652784698554
    >>>
    t2 runs about ten times faster than t4 (and removing the .lower() function seems to be worth the extra 5 characters of typing too).

    Legibility:
    Code:
    >>> 'I' in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    >>>
    >>> 'I' in 'aeiouAEIOU'
    The second gets my vote for being less cluttered and 'busy'.

    Lines of code: No more lines, no fewer.

    Amount of typing: Significantly more characters in the list version, as you need two apostrophes and a comma per entry and two list brackets compared to two apostrophes total.

    Filesize: More characters for the list, so a larger file.

    Time to compile:
    Using the built in compile() function, I get:
    Code:
    >>> import compile
    >>> tc1 = Timer("compile(\"'aeiou'\", '', 'single')")
    >>> tc2 = Timer("compile(\"['a', 'e', 'i', 'o', 'u']\", '', 'single')")
    >>> tc1.timeit(100000)
    2.0333970894835147
    >>> tc2.timeit(100000)
    4.0413572016534545
    >>>
    Compiling the list is getting on for taking twice as long.
    If we change that to include both upper and lower case vowels, it becomes:

    Code:
    >>> tc1 = Timer("compile(\"'aeiouAEIOU'\", '', 'single')")
    >>> tc2 = Timer("compile(\"['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\", '', 'single')")
    >>> tc1.timeit(100000)
    2.0576436968426606
    >>> tc2.timeit(100000)
    5.838339542008498
    >>>
    Much the same for the string, significantly slower for the list...
  10. #6
  11. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19
    Hmmm.... interesting... I gotta remember that.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    36
    Rep Power
    10
    Thank you all for the help^^

    -Jack

IMN logo majestic logo threadwatch logo seochat tools logo