I've solved this challange on CodeWars in the other way, and now I'm learning from other solutions. One of them is:
# Recursive solution
def sqInRect(lng, wdth, recur = 0):
if lng == wdth:
return (None, [lng])[recur] # If this is original function call, return None for equal sides (per kata requirement);
# if this is recursion call, we reached the smallest square, so get out of recursion.
lesser = min(lng, wdth)
return [lesser] sqInRect(lesser, abs(lng - wdth), recur = 1)
Could you please explain me what does this line mean: return (None, [lng])[recur]
(the [recur]
especially - I didn't see such a thing anywhere... is it an index, list or something? What functionality does it provide?). I know what does the code do, because mine does the same thing in another way, but I'm asking just about this sqare bracket in return
Thanks a lot!
CodePudding user response:
There's nothing magic about it; it's tuple indexing, plain and simple.
(None, [lng])
creates a tuple with two elements, and [recur]
grabs the first or second element of this tuple depending on whether recur
is 0 or 1.