I'm trying to split and organize a string in a single function, my goal is to seperate lowercase and uppercase characters and then return a new string essentially like so:
"lowercasestring" + " " + "uppercasestring".
Importantly all characters must return in the order they were recieved but split up. My problem is that i have to do this recursively in a single function(for educational purposes) and i struggle to understand how this is doable without an external function calling the recursive and then modifying the string.
def split_rec(string):
if string == '':
return "-" #used to seperate late
elif str.islower(string[0]) or string[0] == "_" or string[0] == ".": #case1
return string[0] + split_rec(string[1:])
elif str.isupper(string[0]) or string[0] == " " or string[0] == "|": #case2
return split_rec(string[1:]) + string[0]
else: #discard other
return split_rec(string[1:])
def call_split_rec(string):
##Essentially i want to integrate the functionality of this whole function into the recursion
mystring = split_rec(string)
left, right = mystring.split("-")
switch_right = right[::1]
print(left + " " + switchright)
The recursion alone would return:
"lowerUPPERcaseCASE" -> "lowercase" + "ESACREPPU"
My best attempt at solving this in a single function was to make case2:
elif str.isupper(string[-1]) or string[-1] == " " or string[-1] == "|": #case2
return split_rec(string[:-1]) + string[-1]
So that the uppercase letters would be added with last letter first, in order to correctly print the string. The issue here is that i obviously just get stuck when the first character is uppercase and the last one is lowercase.
I've spent alot of time trying to figure out a good solution to this, but im unable and there's no help for me to be found. I hope the question is not too stupid - if so feel free to remove it. Thanks!