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

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0

    Exclamation Help me out please!


    Hello to all of you wazzzzzzaaaaaaa

    I need some help on python programming, would appreciate it alot!

    I've made two *.py files, one that is called labb2funktioner.py only has two functions where I define what is VOCALES (a, e, i,o,u, y etc etc..) and the other function in that same file is for consonants.

    Now the thing is I'm gonna create a python file called VISKNING.py and in that file I want it to ask the user to Write something:
    when he does, the program will re-write his text but delete the vocales. Like this:

    Fr jag viska ditt namn?
    Fr jg vsk dtt nmn?
    Visst fr du det
    Vsst fr d dt

    The thing is I don't know how to code it so that the program DELETES the vocales.. the rest is all good though. I need some help on how to make hte program pick out all the vocales in the text and then delete them.

    Thanks in advance,
    Micheal
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    France
    Posts
    53
    Rep Power
    15
    There is a trivial solution for this problem. First, you should define two lists (instead of two functions) as global variables for the vowels and the consonants like this :

    Code:
    vowels = [ 'a', 'A', 'e', 'E', 'i', 'I', ... ]
    consonants = [ 'b', 'B', 'c', 'C', ... ]
    Then, the function that deletes the vowels from an input stream should look like this :

    Code:
    def DeleteVowels( input_stream ) :
        new_stream = ""     # This will hold the result
        for char in input_stream :
            if char in consonants :
                new_stream += char
        return new_stream
    Note that this solution is certainly not optimal (you'd better find an implementation that uses functions from the string module or C functions) but if performance isn't a matter for you, this should work well enough.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This should give you the results your after, it works a little differently than Zoso's example. Primarily the removeVowels() function appends to a list rather than using string concatenation, to improve performance when working with large strings.

    Code:
    >>> vowels = 'aeiouy'
    >>> def removeVowels(originalString):
    	output = []
    	for letter in originalString:
    		if letter.lower() not in vowels:
    			output.append(letter)
    	return ''.join(output)
    
    >>> removeVowels('Help me out please!')
    'Hlp m t pls!'
    >>>
    >>>
    >>>
    >>>
    >>> def replaceVowels(theString):
    	for letter in vowels:
    		theString = theString.replace(letter, '')
    	return theString
    
    >>> replaceVowels('Help me out please!')
    'Hlp m t pls!'
    >>> 
    >>> theString = 'Help me out please!'
    >>> 
    >>> [l for l in theString if l not in vowels]
    ['H', 'l', 'p', ' ', 'm', ' ', 't', ' ', 'p', 'l', 's', '!']
    >>> ''.join([l for l in theString if l not in vowels])
    'Hlp m t pls!'
    >>>
    All these examples will give you the same results but work slightly differently . For preformance though I would go with the first function.

    Hope this helps,

    Mark.
    Last edited by netytan; October 28th, 2004 at 12:23 PM. Reason: vowels missing.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Code:
    import sys
    from labb2funktioner import isVowel
    
    text = raw_input("Please type something: ")
    for character in text:
        if not isVowel(character):
            sys.stdout.write(character)
    You don't need both isVowel and isConsonant...
    I do "not isVowel" so it prints out other characters like spaces and returns. If you just did "if isConsonant(character)" it would miss spaces and so on.)

    Or

    Code:
    >>> filter(lambda c: not (c in 'aeiouAEIOU'), "Hello World")
    'Hll Wrld'
    Or

    Code:
    >>> import re
    >>> re.sub("[aeiouAEIOU]", "", "Hello World")
    'Hll Wrld'
    >>>
    :P
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Sweden
    Posts
    20
    Rep Power
    0
    Thanks for the help!! Really appreciated!

    However since this is a test I have to do it in a specific way, like this: this is the code in my labb2funktioner.py
    -------
    #coding: iso-8859-1

    from string import *

    def vokal(bokstav):
    bokstav=lower(bokstav)
    lista="aeiou"
    leta = lista.find(bokstav)
    if leta >=0:
    return True
    else:
    return False

    def konsonant(bokstav):
    bokstav=lower(bokstav)
    lista="zxcvbnmsdfghjklqwrtp"
    leta = lista.find(bokstav)
    if leta >=0:
    return True
    else:
    return False
    -------

    now in my viskning.py i've come so far:

    -----
    #coding: iso-8859-1

    from labb2funktioner import *

    bokstav = raw_input("Mata in text: \n")
    if vokal(bokstav):
    for i in range(bokstav):
    print i
    -------

    But I don't know how to pick out the vocales and delete them??

    Thanks,

IMN logo majestic logo threadwatch logo seochat tools logo