Currently, I'm working on a problem on CodeWars named "Training on Calculating with Functions" using Python 3.6. The way that this problem works is, for example, if seven(times(five())) is called, then the function should return 35. Here is a snippet from my code:
def nine(arr): # your code here
if arr is None:
return 9
return operator(arr[0], 9, arr[1])
def plus(num): # your code here
return ["+", num]
def operator(op, n1, n2):
switcher = {
"+": n1 + n2,
"-": n1 - n2,
"*": n1 * n2,
"/": n1 / n2
}
return switcher.get(op, "Invalid")
The full code is on this link (with -> top and without -> bottom args): Hastebin
My logic here (originally) was to use *args as the argument, and if args was None then the function would return its own value (five() => 5), this value would then be passed into one of the 'operator' functions, which would return a list of its own operator (i.e. +, -, /, *) and the number passed into it. Then, in the outermost function, you would pass in the array from the 'operator' function, since args was not None, you would take the first item in the args tuple (the returned value from the 'operator' function which was passed in), then you would access that lists' first and second values, the rest is pretty self-explanatory.
However, when I attempted to run this, I would get the error index beyond tuple range (ref: line 95 on Hastebin). As a result, I switched to a new logic where I would explicitly add the argument to. the function, but (obviously), when run, there was an error that no argument was passed into the function (innermost function --> five() in seven(times(five()))).
I would like to know if there are any ways I can fix my existing code, but any alternate solutions are more than welcome.
Additionally, here is a link to the kata: Codewars Kata