Skip to content

Instantly share code, notes, and snippets.

@juandesant
Created February 22, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juandesant/92237a2b36e7751d3d97d71518940ca0 to your computer and use it in GitHub Desktop.
Save juandesant/92237a2b36e7751d3d97d71518940ca0 to your computer and use it in GitHub Desktop.
string = input("Which string you want to vowel-reverse?\n> ")
listed_string = list(string)
# Helper function to find whether a character is a vowel
def is_vowel(c):
"""Determines is c is a vowel; returns False if it is not, True if it is."""
result = False
vowel_list = list("aeiou")
try:
position = vowel_list.index(c)
result = True
except ValueError as e:
pass # nothing to do if it is not in there
return result
def index_of_true(a_list, a_true_function):
return [idx for idx, x in enumerate(a_list) if a_true_function(x)]
def reverse_list(a_list):
return [a_list[len(a_list)-i-1] for i in range(0,len(a_list))]
vowels_indexes = index_of_true(listed_string, is_vowel)
def swap_items_from_pairs(a_list, list_of_pairs):
result_list = a_list.copy()
for v_idx1, v_idx2 in list_of_pairs:
c1 = result_list[v_idx1]
c2 = result_list[v_idx2]
result_list[v_idx2] = c1
result_list[v_idx1] = c2
return result_list
# Reversing indexes without reverse function
reversed_indexes = reverse_list(vowels_indexes)
list_of_swaps = [(x,y) for x,y in zip(reversed_indexes, vowels_indexes)]
list_of_swaps = list_of_swaps[:(len(list_of_swaps)//2)] # truncate to integer half: i.e. 3//2 --> 1
print("".join(swap_items_from_pairs(listed_string, list_of_swaps)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment