python 3.x - Is this recursive? -
second attempt here, wanted know if considered recursive function.
the purpose of function take string , if the first element equal last element append last element list , return nothing, else call istelf , pass same string index [1] append first element list
i know error checking needs done on if statement. doing try , head around recursion...struggling honest.
also never write program if trivial wanted check if understanding correct far.
def parse(thelist): thelist.reverse() parsedstring = ''.join(thelist) return parsedstring def recursivemessage(thestring): lastelement = thestring[len(thestring) - 1] if thestring[0] == lastelement: buildstring.append(thestring[0]) return none else: recursivemessage(thestring[1::]) buildstring.append(thestring[0]) toprint = "hello everyone!" buildstring = [] recursivemessage(toprint) print(parse(buildstring)) thanks again.
is recursive?
if @ point in function's execution calls itself, consider recursive. happens in example, recursivemessage indeed recursive.
so quicker recursion or iteration?
recursion slower , consumes more space due new stack frame having created on call stack each recursive call. if know recursive function need run many times, iteration best route.
as interesting side note, many compilers optimize recursive function rolling out loop anyways.
Comments
Post a Comment