0

I've been trying to write a scrip that would turn the input to lowercase, erase all vowels and put a period before every consonant. But my script always gets stuck/timing out. Can you help me out?

input = raw_input()

input1 = input.lower()

input_list = list(input1)


for letter in input_list:
    if str(letter) == "a" or str(letter) == "o" or str(letter) == "y" or str(letter) == "e" or str(letter) == "u" or str(letter) == "i":
        input_list.pop(input_list.index(letter))
    else:
        input_list.insert(input_list.index(letter) - 1, ".")


print ''.join(input_list)
Waylander
  • 49
  • 5
  • you generally don't want to add/remove from a list while you are iterating over it. Also your script is also adding periods before periods it looks like. – anthonybell Aug 31 '15 at 20:26
  • possible duplicate of [strange result when removing item from a list](http://stackoverflow.com/questions/6260089/strange-result-when-removing-item-from-a-list) – Peter Wood Aug 31 '15 at 20:28
  • In addition to what others said, keep in mind that `input_list.index(letter)` returns the first index where `letter` is found, not the index of the current letter in the loop. For popping it wouldn't make a difference, but when inserting dots before consonants, even without the issue of the infinite loop, you would be inserting them before the first consonant found, and not the current consonant. – Paulo Almeida Aug 31 '15 at 20:40

3 Answers3

4

First of all you shouldn't change an iterable in iterating time, then instead of using multiple or you can simply put the vowels in a set and check the membership with in operand also for removing a certain item you can use a list comprehension :

input = raw_input()
>>> input = raw_input()
this is a test

>>> ''.join(['.{}'.format(i) for i in input if i not in 'aoyeui'])
'.t.h.s. .s. . .t.s.t'
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • what is lambda doing here? where does `x` come from? – Zion Aug 31 '15 at 20:32
  • @Zion That wasn't a proper way to go, I updated the answer with new approach! – Mazdak Aug 31 '15 at 20:33
  • I was just wondering about the lambda.cuz I'v been seeing it used quite a lot with regex now. so a matched object is passed to lambda? – Zion Aug 31 '15 at 20:47
  • @Zion Yes, actually all the match groups and then you can process them based on your conditions. – Mazdak Sep 01 '15 at 07:02
1

You insert the period, and then find yourself back at the same consonant, so then you insert another period, and on and on and on...

So you're stuck in an infinite loop.

Brendan W
  • 3,303
  • 3
  • 18
  • 37
0
word = "HeLLO"
print("".join(["." + i for i in re.sub("[aeiou]","",word.lower())]))

output:

".h.l.l"
  1. no vowels
  2. all lowercase
  3. periods appear only before consonants not after
Zion
  • 1,570
  • 6
  • 24
  • 36