im trying to return all the middle letters in a string, eg, list = ["Guesss", "what", "idk"] which would return ead where if list[i] is odd it gets the middle letter and if list[i] is even it gets the second middle letter like in what the middle letters are ha and returns the second letter a.
How can I do this using recursion? - with no loops
This is what I've come up so far:
def get_middle_letters(words):
if words == []:
return ""
if len(words[0]) % 2 == 1:
middle = len(words[0]) // 2
return (words[0][middle]) + (get_middle_letters(words[1:]))
elif len(words[0]) % 2 == 0:
middle = len(words[0]) // 2
return ((words[0][middle + 1]) + (get_middle_letters(words[1:])))
words = ['Humans', 'are', 'lazy']
print(get_middle_letters(words))
print(get_middle_letters([]), "#EMPTY")
print(get_middle_letters(['words']))