2

So I have to create a program that rewrites a string backword and alternates vowels and consonants like in this example:

ex_1: 
input ->  'abcdefi'
output -> 'ifedacb'
ex_2:
input -> 'vblsdeloai'
output ->'iladoselbv'

What I did so far:

word = input('the word is: ')
b = word[::-1]
list_vowels = []
list_consonants = []
final_list = []
string = ''
vowels = ['a', 'e','i','o','u']
for i in str(b):
   if i in vowels:
       list_vowels.append(i)
   elif i not in vowels:
       list_consonants.append(i)

part where I'm stuck

for j in list_vowels :
    final_list.append(j)
for b in list_consonants :  
   final_list.append(b)

converts my final list into a string

for q in e
    string = string + e
print (string)

so I convert the string backwords than I use a for to iterate over every char and compare it to the vowel list. If it is in the vowel list then append to a new list list_vowels if not append it to a new list list_consonants. Now I'm stuck at the part where I have to create a list that is created by the two list list_vowels and list_consonats. I don't now how can I make the two for work simultaniously. The zip functions make the list a tuple and I don't think I can use that. I'm prety new and any help will be awsome. If you think I can aproach this problem differently feel free to tell me I am new to programing and I don't realy now how.

LeftyRPZ
  • 31
  • 4
  • Sounds like zip is what you want - just convert it to a list at the end by calling the list function: list(zip(list_vowels, list_consonants)) – R. Davidson Dec 01 '19 at 03:33
  • 2
    I'd like to confirm. `abcdefi` should become `ifedacb` right? Did you swap the last 2 characters? – Nick Lee Dec 01 '19 at 03:36
  • It's is input -> abcdefi then -> ifedcba then -> ifedacb if there are no more vowels or consonats to alternate then it writes the rest of the string. Yes you are correct – LeftyRPZ Dec 01 '19 at 03:40
  • But you wrote `ifedabc` (notice the order of last 2 characters). I just want to make sure. =) – Nick Lee Dec 01 '19 at 03:44
  • I edited ty for telling me – LeftyRPZ Dec 01 '19 at 04:12

3 Answers3

1

You need itertools.zip_longest to zip through two lists taking an empty string as fillvalue:

''.join(map(lambda x: x[0] + x[1], zip_longest(list_vowels, list_consonants, fillvalue='')))

Code:

from itertools import zip_longest

word = input('the word is: ')
b = word[::-1]
list_vowels = []
list_consonants = []
final_list = []
string = ''
vowels = ['a', 'e','i','o','u']

for i in b:
   if i in vowels:
       list_vowels.append(i)
   elif i not in vowels:
       list_consonants.append(i)

print(''.join(map(lambda x: x[0] + x[1], zip_longest(list_vowels, list_consonants, fillvalue=''))))
Austin
  • 25,759
  • 4
  • 25
  • 48
1

The line you're missing is turning your two list of 1) vowels and 2) consonants into a zipped string. You can do this with itertools.zip_longest().

This is the line you will need:

from itertools import zip_longest

''.join(''.join(x) for x in zip_longest(list_vowels, list_consonants, fillvalue=''))
In[1]: 'vblsdeloai'
Out[1]: 'iladoselbv'
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
1
from itertools import zip_longest

def split(s):
    vowels = ['a', 'e', 'i', 'o', 'u']
    return list(filter(lambda c: c in vowels, s)), \
           list(filter(lambda c: c not in vowels, s))

def reverse_interleave(s):
    vowels, consonants = list(map(reversed, split(s)))
    return ''.join(
        map(lambda x: ''.join(x), 
            zip_longest(vowels, consonants, fillvalue='')))

print(reverse_interleave('abcdefi'))
print(reverse_interleave('vblsdeloai'))
  1. Split the characters into vowels and non-vowels.

  2. Reverse them.

  3. Interleave them using zip_longest() in this case.

Nick Lee
  • 5,639
  • 3
  • 27
  • 35
  • Why iterate twice instead of looping and appending to two different lists? Putting that aside a moment, is `list(filter(lambda c: c in vowels, s)))` really better than `[c for c in s if c in vowels]`? – AMC Dec 01 '19 at 04:08
  • I just like a more functional style of coding. The code looks clearer to me that way. I don't oppose people using a more procedural style. That's just me. – Nick Lee Dec 01 '19 at 04:15
  • Nice :) Do you program in any functional languages, or do you just stick to a functional style in languages which support the paradigm? – AMC Dec 01 '19 at 04:20
  • I don't use functional languages per se. I learn about and practice this style as a matter of interest. I am attracted to this style wherever it is supported, except in some environments where it is materially more efficient otherwise, e.g. MicroPython. – Nick Lee Dec 01 '19 at 04:43
  • Do you know of any good resources on the subject, then? (Functional programming in Python/non-functional languages) :) – AMC Dec 01 '19 at 10:46
  • Well, I just tried to get acquainted with functional concepts by studying Haskell, Clojure, etc. Once you get the hang of it, it's not hard to transplant them to other languages. – Nick Lee Dec 01 '19 at 15:53