Basically, I need to write a recursive function that takes a list of strings and then returns a list of strings with each character in each string repeated/doubled.
For example, doubleStringList(['I','love','overflow']) would return ['II','lloovvee','oovveerrffllooww'].
I decided to split it up into two recursive functions. I already made one function that returns the strings with doubled characters:
def doubleCharacter(seq):
if len(seq) == 0:
return seq
else:
return (seq[0] * 2) + doubleCharacter(seq[1:])
For example, entering doubleCharacter("overflow") would return 'oovveerrffllooww'
I'm trying to create a second recursive function that uses the first except for a list of strings as the paremeter rather than a single string. For example:
def doubleStringList(sList):
if len(sList) == 0:
return sList
else:
return doubleCharacter(sList[0]) + doubleStringList(sList[1:])
Theoretically, if I entered doubleStringList(["overflow","is","great"]), it should return ["oovveerrffllooww", "iiss", "ggrreeaatt"]. If someone could please help me figure out this code, I would appreciate it greatly.
The error message gives a big clue:
TypeError: can only concatenate str (not "list") to str
So, the last line should be
return [doubleCharacter(sList[0])] + doubleStringList(sList[1:])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.