-1

This code takes all the vowels in a string and flip thier positions.

class Solution:
    def reverseVowels(self, s: str) -> str:
        
        list_vowels= [x for x in s if x in 'aeiouAEIOU'][::-1]
        
        list=[x for x in s]
        
        z=0
        for number in range(0,len(s)):
            
            if list[number] in 'aeiouAEIOU':
                
                list[number]=list_vowels[z]
                z+=1
                
                
                
        return "".join(list)

I'm still a beginner but I have this feeling that if I submit this code in an interview for exemple, I won't be taken seriously.

Thanks to who ever took time to help me.

matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • https://stackoverflow.com/questions/931092/reverse-a-string-in-python – Captain Caveman Nov 04 '22 at 13:16
  • 1
    @CaptainCaveman don't think they need to reverse a string but flip positions of certain characters – StonedTensor Nov 04 '22 at 13:17
  • 1
    Looks like you have a working piece of code you want to write in a better way - such questions are better asked on https://codereview.stackexchange.com/ – matszwecja Nov 04 '22 at 13:18
  • 1
    What does _flip their positions_ mean, exactly? – 001 Nov 04 '22 at 13:20
  • imagine that you are the interviewer. Is your code clear enough? I can see a variable `list_vowels`, ok that looks like the list of vowels. then you have something called `list` (not a good name). what is list? you then initialize `z=0`. what is z? You iterate over length of s, use `enumerate` instead. If you want to present a code, take a look at small details. eg: `list_vowels= [...x` why is there a space between = and [ but not between s and =? Your function is called `reverseVowels` and your variables `list_vowels`. Why are you mixing naming styles? – Sembei Norimaki Nov 04 '22 at 13:21
  • 1
    Also, if you want to get this job, I recommend _not_ naming a variable `list`. – 001 Nov 04 '22 at 13:21

1 Answers1

0

The code can be simplified as follows.

  • use reverse iterator reversed to get successive elements of vowels in reverse order as needed
  • Use Python PEP 8 Style Guide for naming functions and variables
  • Making use of a 'constant' rather than defining vowels multiple times (aids code maintainability)
  • In Python it's highly discouraged to use builtin functions as variable names (e.g. list)
  • Not clear why the solution uses a method rather than a standalone function but kept as posted (looks like a Leetcode answer template).

Code

class Solution:
    def reverse_vowels(self, s: str, VOWELS: str = 'aeiouAEIOU') -> str:
        # iterator with vowels reverse
        rev_vowels = reversed([c for c in s if c in VOWELS]) 

        # Choose character for non-vowles, elese choose next element of reverse iterator for vowel
        return ''.join(c if not c in VOWELS else next(rev_vowels) for c in s)

Test

Sol = Solution()
for t in ['aeio', 'hello']:
    print(f'{t} -> {Sol.reverse_vowels(t)}')

Output

aeio -> oiea
hello -> holle
DarrylG
  • 16,732
  • 2
  • 17
  • 23