Using puzzles to improve your scripting skills
I've blogged before about Unleash your coding superpowers in your free time; looking for opportunities to expand upon your testing knowledge by solving everyday problems outside the work place. A good way to spot these opportunities, rather than seeking out complex situations, is to instead spot when your first instinct says "that's easy"; and LinkedIn is a great source for those!
*A none-too-taxing puzzle which appeared recently on LinkedIn.*
Not too taxing, right? It might take a few seconds, but you'll think of an answer? Exactly - which is why this (for some reason) had over 50,000 responses on LinkedIn. It's like those puzzles which say "90% of people get this wrong first time..." - you're baited into replying to show how brilliantly clever you are! (Even though, on this particular thread, I saw several answers such as "India".)
It was such a banal challenge that I immediately decided to solve it to the extreme, by finding every possible answer. It seemed that I could do this with a simple Python script, and that I could easily extend the script to solve the puzzle for any letter of the alphabet.
Here's the script which I wrote. When executed, it'll output 26 lists, from A-Z, showing countries which do not contain each letter in turn.
It's relatively few lines of code; over half of it is comments, whitespace and output formatting. Still, I had a couple of interesting challenges along the way: I'd decided to dynamically load a list of countries from an online JSON list of countries, which involved my first use of Python's urllib and json libraries.
More interesting, particularly from a tester's perspective, were the questions which arose while I was scripting this. Firstly: What's a country? For instance, the list which I'd utilised contained Vatican City and Taiwan, but these are among the nations which aren't officially recognised by the United Nations; does that make them invalid for the purposes of the quiz? After all, such distinctions are important when you're taking part in programmes such as Pointless:
Secondly, what's the name of a country? For example, "United Kingdom" doesn't contain an A, unless you're referring to "United Kingdom of Great Britain and Northern Ireland". Then you've got "Ivory Coast" which contains an A, unless you're referring to its native name of "Côte d'Ivoire". Discovering data examples such as these are a key element to testing before a product exists, as they aid in the refinement of up-front definitions.
Having executed my script, I was also able to reverse-engineer the puzzle to determine if it was the "hardest" challenge possible. In this case, yes it was: the list of countries without an "A" was significantly shorter than any other letter of the alphabet, significantly moreso than the other vowels, which surprised me somewhat.