Is it fizz buzz?Friday, 21st February 2020 ◆ Softly making amends to get ready (11)
I remember playing Fizz Buzz at primary school in my Year 6 class (age 10-11). Starting with 1 and going down the register, each kid counts the next number. If the number is a multiple of 3 or 5 (or 15), however, you don't say the number and instead say either "fizz", "buzz" (or "fizzbuzz") respectively.
1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz.... you get the gist.
It was fun, simple and achieved its goal of training our mental maths skills.
Recently, though, the game has re-entered my consciousness as it has become a popular tool by which to test programmers. I have not been asked to code fizzbuzz in an interview so far, however it is best to be prepared. So for a possible future in which I am asked to create an implementation under pressure, I will spend some time thinking about it now. I'll pick three popular languages I have been using recently, and make an implementation in each of them.
I have had the "pleasure" of using XSLT in both of my last two jobs. It's a language used to transform XML documents. I have seen it be used to transform backend data from XML into HTML for the front-end (in a move I would class as "not sensible"), and to convert RSS feeds from various providers into a single format so they can all be processed together ("sensible").
XSLT is meant to be used to transform documents on a largely element-by-element basis. For example, to transform
<pet type="cat">. It can get much more complex than that, but still there is no need for loops, so the language doesn't have them.
This is clearly the perfect choice for a language with which to create fizz-buzz. XML is ubiquitous, so by outputting it we are opening the possibility for our program to interface with a wide variety of applications. Furthermore, we are not tied into a small pool of compilers or interpreters as if we'd used a more convential language. There are XSLT implementations written in every language under the sun, so our prospective employer will definitely have no problems in running our program.
We do have to get around the issue of there being no loops, which we can do by creating a recursive template. We also have to provide a valid XML file as an input document, even though we ignore it. Nevermind, these are minor inconveniences compared to the myriad benefits of writing our program in XSLT.
If you want to try it yourself, there are many XSLT runners online, such as this one. For the XML input, you can use any valid XML, such as
MySQL is another language I've used a lot of. If you have a database full of data, using MySQL to interact with it is a dream. However, if you want to create a phantom table with new values, things get a little more complex. There are no loops in MySQL, and no easy way to select a list of numbers without simply writing them all out.
Let's say I want to create a dummy table with two rows, I must write each select separately and get their union:
SELECT 0, 1 selects one row, with each number in a separate column which is not what I want.
If I want to select, say, the first 256 integers, this would be unfeasible. However, if I were to join 8 tables like the above, MySQL would select 2^8 rows. Then I can convert those rows into the numbers I want using binary. Easy!
There is a CSS feature called counters which would allow the numbers to be generated via pure CSS. However, it didn't work in my browser of choice (Safari), so I am leveraging the automatic numbering you get with
ol lists instead. This does means we get a decimal point after each number, but I can live with that.
This one does require a bit of HTML as well, so perhaps that is cheating...
There are no comments yet.