Reformatting a license key: start at the right-hand end
Take a string of letters, digits, and hyphens. Uppercase the letters and regroup the characters with a hyphen between groups. Every group should have K characters, with one exception: the first can be shorter.
With K = 4, 5F3Z-2e-9-w becomes 5F3Z-2E9W. With K = 2, 2-5g-3-J becomes 2-5G-3J.
The phrase “except the first group” sent me down the wrong path. I took it to mean keep the first group from the input. Both examples happened to fit that reading. I found the first hyphen, stripped all the la ...
Finding the number that has no partner
In this array, every number turns up twice except one. In [2, 2, 1] the odd one out is 1; in [4, 1, 2, 1, 2] it’s 4. The challenge is to find it in one linear pass without setting aside extra storage.
My first thought was to ask Python how often each number occurs:
123for i in nums: if(nums.count(i)==1): return i
That looks like a single loop until you remember what count has to do. Each call searches the list again.
Next I tried sorting the numbers so the pairs would sit side by side ...
Did I see this number in the last k positions?
A duplicate by itself isn’t enough here. The question asks whether there are two different positions holding the same number, with at most k places between them.
I spent longer on this than I expected. I knew checking every possible pair would be slow. Then I started thinking about groups of k+1 elements, and what to do about a duplicate that showed up after I’d moved on to the next group. I was making it harder than it needed to be.
The official solution gave me a better question to ask at each ...
Where do the zeros at the end of a factorial come from?
3! is 6, so it has no trailing zeros. 5! is 120, so it has one. The challenge was to count those zeros for any n in O(log n) time.
I started by calculating the whole factorial and peeling off its digits:
12345678910import mathdef func(n): num = math.factorial(n) res = 0 while(num>0): k = num%10 if(k==0): res = res+1 num = num //10 return res
Try imagining that with 999!. It’s a lot of work to build such a huge integer just to ask about its last few ...
My Darling
Thoughts after watching Darling in the FranxxOn technologyUtopiaIn the world of the show, humanity has achieved longevity using fuel from underground. Society runs like a machine, each person a cog in an organization reminiscent of 1984. Human desires must be suppressed because they introduce uncertainty. Having attained longevity, people give up reproduction in pursuit of immortality; retired men become “brains in vats.” Control the brain’s reward mechanism and feed it dopamine at intervals, an ...






