A C# programmer in King Python’s Court

If you’ve been reading my recent blog posts you may be expecting a long, highly technical post. Good news, this week it’s more of a longish summary than an epic technical manual (or bad news for those who seek out new long technical posts as entertainment, you lunatics you 🙂 ) My tasks this week with the biggest unknowns have actually been code katas in languages I’m unfamiliar with, rather than the previous weeks refactoring fun.

Poker Hand Ranking

The first was to work through a poker hand ranking algorithm in Python. Starting with a set of written out rules for ranking hands and using a set of test data from CodeWars, I had to take a hand, parse them into something usable, then compare two of them to see which one would win. Though my recent background is more C# or Python, I’ve previously supported a python application so I didn’t actually expect it to give me too much more trouble than getting a developing environment set up and hammering out some rules about flushes, straights, and so forth. I had no problems with the IDE, created the project…. and sat there making small false start after false start. It was intensely frustrating as I can read already written Python well enough as it roughly resembles Basic with obsessive indentation being used to group statements instead of keywords like End If, End For, etc. Writing it from scratch though with an unfamiliar algorithm though just wasn’t working. So as a way to get my feet under me I retreated to my familiar C#, hammered out a frankly horrible looking implementation of the algorithm. Some clean up later so it was easier for me to read and I returned to Python, equipped with a basic set of tasks that I knew I had to understand well enough to translate it. Mostly basic stuff like defining classes, iterating arrays. Along the way being reminded of switch statements are great (Python doesn’t have them) when I had to chain together if and else-if statements. Adding new sets of tests as I defined each basic component so I could verify that I was getting what I expected from the alterations I was making to suit. Even then I can’t say the translation worked flawlessly or with minimal reworking, I used a few LINQ statements in the c# version that initially in my ignorance I failed to translate in the cleanest fashion. Mostly though it was a lot of trips back and forth to the Python documentation and restructuring logic until I got it working. On to the next code kata I went…

Wonderland Number in F#

To quickly summarize this one, my task was to calculate a 6 digit number that didn’t repeat any digits and could be multiplied by 2-6 and the resulting number was the same length and has the same digits, just likely in a different order. Oh and I was to do it in F# which is Microsoft’s answer to the Functional Language paradigm that I wrote about last week. I was certain this one would be a breeze as well to get set up for, the kata comes with tests pre-defined, in theory all I had to do was load it up into Visual Studio and figure out what to put into the empty function declaration. None of it worked, and after some discussion with Kris, I ended up recreating it in a new project and learning how to write the tests in a way that VS actually would compile and run. From there I had a dim idea how to proceed, but inspiration as it happened tripped me up. I realized one night on a walk that I could eliminate all but 100,000 of my 6 digit number set before I wrote a line of code. The reasoning was sound and so I started looking for ways to filter the initial sequence down (eliminating numbers with duplicated digits). This in mind I ran headlong into places that my simplistic understanding of F# couldn’t quite solve, but could come close. I spent a couple hours trying to figure out how to rewrite how I’d picture doing it in other languages to work in F#, array shuffling, iteration, permutations and other “fun” half-visualized plans. In the end, I realized that the step I had intended to take once I had this part done was actually the only step I needed in the first place. I then finished it in 5 minutes, and spent the next 15 poking it trying to make it break because surely it couldn’t be that easy.

That’s the two main tasks I worked on this week, in terms of keeping things simple I definitely had some false starts, but so far a successful sprint. Incidentally, this exposure to F# is already helping me follow some walkthroughs on angular better than I did previously. Specifically the using other operators to chain calls together instead of parentheses.

Leave a comment