I just started working on a software puzzle I had seen a few times on the T. I think I’m getting closer to a correct solution. If you haven’t seen it yet, you can check it out on ITA Software’s website. The basic idea is if you wrote out the numbers 1-999,999,999 (so “one” “two”…”fivehundredseven” etc.) then sorted the billion words alphabetically and concatenated them, what would letter number 51,000,000,000 be?
At first I thought it would be easy. But after spending a good 2 hours thinking and playing around with different ideas, I was kind of stumped. I tried some simple brute force methods but those would have taken a few days to solve. I also started writing my solution in C so I could kill two birds with one stone(learning C better and getting better at algorithms in general). But I was spending a lot of time looking things up so I switched over to C++, which I know a bit better. But then I again kept getting hung up on little things and wanted to get to the meat of the problem so I returned to my favorite Python.
My strategy is to write a recursive function that builds a list of the left most words(eight, eighteen, eighteenhundred etc) and adds the sum of the letters on their branches(you can reuse the count of the letters in branches for words like eighteenhundred and seventeenhundred, as they both will have identical trees extending to the right). If the desired sum is passed by adding the sum of a branch, I call the function recursively and dive into that branch. This way I’m alphabetizing the list as I go, and skipping the alphabetizing and counting of a huge number of words by relying on constants for those branches.
My problem right now is that the letter I get is in the middle of a number(it is supposed to be the last letter). I’m trying to figure out what’s wrong but after a good two hours of debugging I’m thinking of taking a different approach. My function has developed a whole lot of nested loops. Hopefully I can solve this soon and move on to the other two puzzles listed on their website.
I’m not sure if doing puzzles like this is the most efficient method to improve my programming skills, but it can’t hurt.




Recent Comments