Renaming object keys recursively

There are a couple of problems there. One is that you’re falling prey to The Horror of Implicit Globals by failing to declare your build variable in the function. But the logic has issues as well, here’s a minimal reworking: var keys_short = [“ch”,”d”,”u”,”tz”]; var keys_long = [“children”,”data”,”user_id”,”time_zone”]; function refit_keys(o){ var build, key, destKey, ix, … Read more

Infinite recursion in C

Whenever you call a function, the arguments are pushed on the stack, which means that data on the stack segment is “allocated”. When the function is called, the return adress is also pushed on the stack, by the CPU, so it knows where to return to. In your example case this means, that no arguments … Read more

What is the maximum recursion depth, and how to increase it?

It is a guard against a stack overflow, yes. Python (or rather, the CPython implementation) doesn’t optimize tail recursion, and unbridled recursion causes stack overflows. You can check the recursion limit with sys.getrecursionlimit: import sys print(sys.getrecursionlimit()) and change the recursion limit with sys.setrecursionlimit: sys.setrecursionlimit(1500) but doing so is dangerous — the standard limit is a … Read more

Given a list of elements in lexicographical order (i.e. [‘a’, ‘b’, ‘c’, ‘d’]), find the nth permutation – Average time to solve?

9 min, including test import math def nthperm(li, n): n -= 1 s = len(li) res = [] if math.factorial(s) <= n: return None for x in range(s-1,-1,-1): f = math.factorial(x) d = n / f n -= d * f res.append(li[d]) del(li[d]) return res #now that’s fast… nthperm(range(40), 123456789012345678901234567890)