0

Here I want to remove any vowels which repeat consecutively. But it shows error as "list out of index".

So I tried break if last element in list is reached, but still won't work.

Here is the code I tried:-

a=[]

b=str(input("enter the string"))

a=b.split(',')

c=['a','e','i','o','u']

for i in c:

    for j in range(0,len(a)):

        if (a[j+1] == a[len(a)]) is True:

            break;

        elif ((a[j] == a[j+1]) & (a[j+1] == i)) is True:

                del[j]
e.join(a)

print(e)        

Please show me how to solve this problem, or any other problem if in there.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
Aakib Shaikh
  • 1
  • 2
  • 4
  • 1
    Hello. Welcome. You shouldn't just say "it won't work" and you should include all the necessary inputs. Provide your inputs, describe what you expect to happen, and then describe how what happens is different. – Jean-Paul Calderone Aug 23 '19 at 17:27
  • Also see https://stackoverflow.com/help/minimal-reproducible-example – Jean-Paul Calderone Aug 23 '19 at 17:27
  • What is e? Try to include any required information if want a full solution, otherwise remove anything unnecessary to your problem. – Chrismon Chin Aug 23 '19 at 17:40

6 Answers6

1

How about maintaining a stack for consecutive vowels? whenever you see non-vowel string re-initialize the stack list and when you see vowels but are not consecutive you just add to the final list

stack=[]
new_list=[]
vowel=['a','i','o','u','e']
for i in your_string: # replace your string with actual string
    if i not in vowel:
        if len(stack) == 1:
            new_list.append(stack[0])
        new_list.append(i)
        stack = []
    else:
        stack.append(i)
if len(stack) == 1:
    new_list.append(stack[0])
mad_
  • 8,121
  • 2
  • 25
  • 40
0

You get a list out of index error because you are indexing at a value not in a

if (a[j+1] == a[len(a)]) is True:

a[len(a)] does not exist, arrays are zero indexed so they start from 0. An array with 5 items has index 0,1,2,3,4

the line should be:

if (a[j+1] == a[len(a) - 1]) is True:

Also 'is True' is redundant, so further refined:

if a[j+1] == a[len(a) - 1]:

Also is del[j] an error? should it be del a[j]? If this is the case, the program will run into further errors as you are iterating over the entire Original size of the array but deleting values during this iteration, so it will look for items that no longer exist.

Chrismon Chin
  • 429
  • 2
  • 12
0

First detail is this: if (a[j+1] == a[len(a)]) is True: from what I understood this is to break the code when it is necessary. But that is completely unnecessary. Instead you should fix the number of iterations in the beginning, which should be for j in range(0,len(a)-1):

Another problem is that you aren't really iterating through the letters, just each comma separated phrases. If you put "Hello, World" you aren't checking the letters, you check "Hello" and " World". You might as well remove b and let a just be the raw input.

Since a would then be a string, to erase the i-th letter of a string you can use the function below.

def remove_jth(word, j):
    word = word[:j] + word[j+1:]

Finally since you are using range, it will build a "list" that has the size len(a) when you start the for, but as you remove vowels, the length of a gets shorter and the range you had at the beginning will be to much. If you fix all of these things you should have it right

0

You can do it this way:

a=[]

b=str("a,a,a,b,e,e,e,c,x,d")

a=b.split(',')

c=['a','e','i','o','u']
j = 0
for i in c:
  while j < len(a)-1:
      if a[j] == a[j+1] and a[j] == i:
          del a[j]
      else:
          j = j+1
  j=0

Use a while loop to check through the list and delete consecutive duplicates. Then reset the the while loop back to zero and run through the list again.

Using a for loop will not work,if you are deleting items in the list that is being used to iterate over.

b=str("a,a,a,b,e,e,e,c,x,d") was just used to test the code. To print the results you can just do print(a) it will print the list out.

I used this Stack Overflow post as reference.

Thomas
  • 72
  • 10
0

I edited it to suit you:

result=original.replace('a','').replace('e',''),replace('i','').replace('o','').replace('u','')

original is your input string and result is your output string.

0
t=input()
st=""
vow=['a','e','i','o','u']
st+=t[0]
for i in range(1,len(t)):
  if(t[i].lower() in vow):
    if(t[i-1].lower() not in vow and i-1>=0):
      st+=t[i]
  else:
     st+=t[i]
print(st)
  • 3
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Maximouse Mar 25 '20 at 14:49
  • While this may answer the question, it was flagged for review. Answers with no explanation are often considered low-quality. Please provide some commentary for why this is the correct answer. – Dan Mar 26 '20 at 03:32