Categories

## Programmer interview challenge 2, part 4: Using “watchers” to play FizzBuzz “properly”

After reading the previous article on FizzBuzz solutions, Reginald “raganwald” Braithwaite, whom I know from my days as part of the Toronto tech scene, tweeted this:

JenniferPlusPlus agrees:

They both make a good point. If you’re playing the FizzBuzz game as the original children’s game and not as an exercise to prove that you can actually write a program, you’d do it like this:

• Players sit in a circle, not unlike “the circle” in That ’70s Show.(Players don’t have to be high, unless it helps.)
• The player designated to go first says the number 1, and each player afterwards counts one number in turn. The next player in the circle says 2, and so on.
• However, for every third number, instead of calling out the number, the player whose turn it is should say “Fizz”.
• …and for every fifth number, instead of calling out the number, the player whose turn it is should say “Buzz”.
• The “Fizz” and “Buzz” rules, as the kids would say, stack. In other words, for every number that is both the third and fifth, the player needs to say “Fizz” followed by “Buzz”, or “FizzBuzz”.

So in the spirit of the original game, I’ve put together a FizzBuzz solution that uses “watchers” to keep track of  “every xth number”, with one watcher to keep track of when it’s time to say “Fizz”, and another for when it’s time to say “Buzz”. When it’s time to say “FizzBuzz”, they’ll work in tandem.

I created a class called `WordWatcher`, which can be summarized as shown below:

Here’s its code:

```class WordWatcher:

def __init__(self, interval, word):
self.counter = 0
self.time_for_word = False
self.interval = interval
self.word = word

def observe_next_turn(self):
self.counter += 1
if self.counter == self.interval:
self.counter = 0
self.time_for_word = True
else:
self.time_for_word = False

def speak(self):
if self.time_for_word:
return self.word
else:
return ""```

• For those of you who aren’t familiar with Python’s approach to class methods, the first parameter for every method in a class is `self`. It’s the one parameter you don’t fill when calling a method, because Python calls it implicitly (seemingly in violation of Python’s general guideline that explicit is better than implicit). There’s a reason behind this, and it’s explained in this article: Understanding self in Python.
• Also note that instance variables are declared and defined in the initializer method, `__init__()`, and any reference to them is always preceded by `self`.
• The `observe_next_turn()` method is meant to be called as the fizzBuzz method proceeds to each new number. It updates the watcher’s internal counter and sets the `time_for_word` flag accordingly.
• The `speak()` method outputs the watcher’s word if it’s time to say the word, or an empty string otherwise.

For FizzBuzz, we’ll need to create two watchers:

1. One to keep watch for every third turn, at which point it should say “Fizz”, and
2. one to keep watch for every third turn, at which point it should say “Buzz”.

With the `WordWatcher` class defined, we can create these two watchers like so:

```fizz_watcher = WordWatcher(3, "Fizz")
buzz_watcher = WordWatcher(5, "Buzz")```

It will become handy to have these two watchers in the same place. Since the “ha ha only serious” joke about Python is that everything is a list, let’s put them into a list:

`word_watchers = [fizz_watcher, buzz_watcher]`

Let’s define a fizzBuzz() function that makes use of this list of word watchers:

```def fizzBuzz(word_watchers = [], first = 1, last = 100):
final_result = ""

for number in range(1, 101):
current_result = ""

if len(word_watchers) > 0:
# This part might need some explaining
_ = [word_watcher.observe_next_turn() for word_watcher in word_watchers]
words = map(lambda word_watcher : word_watcher.speak(), word_watchers)
current_result += functools.reduce(lambda total, next_element : total + next_element, list(words))

if current_result == "":
current_result = str(number)

final_result += current_result

if number < last:
final_result += ", "
else:
final_result += "."

return final_result```

If you’ve been following the FizzBuzz series of articles, most of this code will be familiar. The part that might need explaining is the part with the comment “This part might need some explaining”.

## Explaining the part that needs explaining

Let’s look at the first of the three lines of code in that part:

`_ = [word_watcher.observe_next_turn() for word_watcher in word_watchers]`
• The `_` on the left side of the `=` sign is a throwaway variable. It says “I don’t care about what you do on the other side of the `=` sign; only that you do something on the other side of the `=` sign”.
• On the right side of the`=` sign is a list comprehension, which is Python’s “show, don’t tell” way of building lists. This list comprehension simply says “call the `observe_next_turn()` method of every object in the list”.

Let’s look at the next line:

`words = map(lambda word_watcher : word_watcher.speak(), word_watchers)`
• This line creates a map that converts the watchers in the list into the words they should say for this turn. If the current turn means that it’s time for any one of them to speak, the watcher will be mapped to the word it’s supposed to say. Otherwise, it will be mapped to an empty string.

And now, the final line:

`current_result += functools.reduce(lambda total, next_element : total + next_element, list(words))`
• For some reason, `map()` comes built into Python, but you have to import the `functools` library in order to use `map()`’s partner in crime, `reduce()`. Remember `reduce()` is a functional programming thingy that takes a collection of items, performs some kind of calculation on that collection, and returns a single value (which you might call a reduction of the collection).
• The first argument that I’ve provided to `reduce()` is a lambda — a small function that isn’t given a name — that simply takes the current item in the list and adds it to the previous collected items. Applied over the entire list, it builds a “total”, which in this case is all the words output by the watchers’ `speak()` methods concatenated together.
• The second argument is the words map converted into a list. This is the list that the `reduce()` method will operate on.

At the end of those three lines, `current_result` will contain one of the following:

• The empty string
• Fizz
• Buzz
• FizzBuzz

If `current_result` is still empty at this point, it means that it’s not time for any of the watchers’ words. If this is the case, the string version of the current number is concatenated to `current_result`:

```if current_result == "":
current_result += str(number)```

Here’s the code in its entirety:

```import functools

class WordWatcher:

def __init__(self, interval, word):
self.counter = 0
self.time_for_word = False
self.interval = interval
self.word = word

def observe_next_turn(self):
self.counter += 1
if self.counter == self.interval:
self.counter = 0
self.time_for_word = True
else:
self.time_for_word = False

def speak(self):
if self.time_for_word:
return self.word
else:
return ""

def fizzBuzz(word_watchers = [], first = 1, last = 100):
final_result = ""

for number in range(1, 101):
current_result = ""

if len(word_watchers) > 0:
_ = [word_watcher.observe_next_turn() for word_watcher in word_watchers]
words = map(lambda word_watcher : word_watcher.speak(), word_watchers)
current_result += functools.reduce(lambda total, next_element : total + next_element, list(words))

if current_result == "":
current_result += str(number)

final_result += current_result

if number < last:
final_result += ", "
else:
final_result += "."

return final_result

fizz_watcher = WordWatcher(3, "Fizz")
buzz_watcher = WordWatcher(5, "Buzz")
word_watchers = [fizz_watcher, buzz_watcher]
print(fizzBuzz(word_watchers))```

And for completeness’ sake, here’s the test file:

```import pytest
from fizzbuzz_with_watchers import fizzBuzz, WordWatcher

plain_1_to_100_result = "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100."
fizzBuzz_1_to_100_result = "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz."

def test_fizzBuzz_null():
result = fizzBuzz()
assert result == plain_1_to_100_result, f"The watcher solution returned the wrong result:\nExpected: {plain_1_to_100_result}\nActual: {result}."

def test_fizzBuzz_fizz_and_buzz():
fizz_watcher = WordWatcher(3, "Fizz")
buzz_watcher = WordWatcher(5, "Buzz")
word_watchers = [fizz_watcher, buzz_watcher]
result = fizzBuzz([fizz_watcher, buzz_watcher])
assert result == fizzBuzz_1_to_100_result, f"The watcher solution returned the wrong result:\nExpected: {fizzBuzz1To100Result}\nActual: {result}."```

You can download fizzbuzz_with_watchers.py and test_fizzbuzz_with_watchers.py here (2KB, zipped folder with 2 Python files).

## That’s a lot of fuss for Fizzbuzz. Why did you do all that?

1. Reginald asked me to, and I’ve known and respected him for ages, and JenniferPlusPlus seconded the request.
2. Wait until you see what customers ask you to do.

1. Don’t feel bad. I had the same trouble when I first learned functional programming, and that was back in 1991, when the computers that ran functional language interpreters were in labs. I spent a lot of time in Queen’s University’s DEClab, which was full of machines that were cutting edge at the time made by a vendor that no longer exists. Computer time, as well as info on any kind of programming, never mind functional programming, was a lot harder to come by. (In case you were wondering, the language we learned was Miranda.)
2. If you’ve never worked in Python, some of it can be quite weird. It does eventually make sense.
3. Let me know, either via email or in the comments, if there’s anything you’d like me to cover in greater detail.

## Do you have an alternate solution?

I’ve love to hear about it and present it here! Again, let me know via email or in the comments.

## What’s next

An elegant JavaScript implementation.

Categories

## The modulo operator

The standard FizzBuzz solution relies on the modulo operator, which it uses to determine if a number is a multiple of 3 or 5.

If you have a math, computer science, or engineering background, the odds are good that you encountered the modulo operator in your studies, as your courses tended to take a mathematical approach to  programming. (Remember relational calculus from your intro to databases course?)

If you came into programming from some other field and really got into it because you have a knack for problem-solving, you might not be aware of modulo math. That doesn’t mean that you can’t come up with a FizzBuzz solution.

## FizzBuzz minus the modulo operator

When you present the FizzBuzz challenge to a large enough group of programmers — typically a dozen or more — there will be a very determined person who will insist that you provide them with no hints whatsoever. It happens.

When that happens, there’s invariably someone who’s either never heard of the modulo operator (`%`, which returns the remainder of a division operation) or has forgotten it exists. I’ve also seen a competition comprising quick programming challenges where contestants were told to implement FizzBuzz, but without using modulo.

The more mathematically-inclined will use a method like this:

```def multiple_of_n(factor, number):
return math.floor(number / factor) == number / factor```

`multiple_of_n()` determines if a number n is a multiple of a factor f if the result of n / f is the same as n / f with the fractional part removed.

Occasionally, you’ll run into programmers who are unaware that there are functions to remove the fractional part of a number, either through rounding or truncation. Some of them make up for their lack of math background with a combination of creativity and grit.

I’ve seen one solution that looked something like this:

```def multiple_of_5(number):
number_as_string = str(number)
last_digit = number_as_string[-1]
return last_digit in ['0', '5']```

This function turns the given number into a string, isolates the rightmost character of that string, and then returns `True` if that character is “0” or “5”, which is true for the string form of numbers that are multiples of 5.

My reaction:

That was nothing compared to one method I saw that someone cobbled together to determine if a number was a multiple of 3. They remembered the old grade-school rule that if you add the digits of a number and the total is a multiple of 3, then the number is a multiple of 3.

Based on that, they wrote something like this:

```def multiple_of_3(number):
number_as_string = str(number)
total_of_digits = 0
for digit in number_as_string:
total_of_digits += int(digit)

Again, this function starts by converting the given number into a string. It then iterates through that string character by character, turning each character into a number and adding it to a running total. It then checks to see if that total is in a list of multiples of 3.

Since the standard FizzBuzz challenge is supposed to be performed on the numbers 1 through 100, the largest multiple of 3 will be 99, and the sum of its digits will be 18. Hence the list of multiples of 3 starts with 3 and ending with 18.

My reaction:

But hey, it works!

## FizzBuzz plus grit

I’ve seen a handful of people with bachelors’ and even masters’ degrees in computer science face the FizzBuzz test and completely fail to produce working code. I’ve been more impressed by the self-taught coders who, in spite of not knowing about the modulo operator, charge head-first into the problem and solve it. These non-modulo solutions might cause a mathematician to react like this…

…but I think that they’re a sign of grit, which is an important quality for a programmer. These people took what they knew, applied a little creativity, and solved a problem that they shouldn’t have been able to solve. They remind me of a line from aviation pioneer Igor Sikorsky:

According to the laws of aerodynamics, the bumblebee can’t fly, but the bumblebee doesn’t know the laws of aerodynamics, so it goes ahead and flies.

Sooner or later, if you’re working on applications that actually matter, you’re going to run into seemingly insurmountable problems. There will always be the fear that something is just too hard to do. Impostor syndrome may rear its ugly head. Developing grit — and yes, it can be developed — is important, and it’s a quality I look for when forming a team.

## What’s next

Using “watchers” to play FizzBuzz “properly”.

Categories

## Programmer interview challenge 2, part 2: Functional FizzBuzz

(In case you missed it, here’s the previous article in this series.)

FizzBuzz became popular in the late 2000 – 2010 decade, which was around the same time that may programmers were beginning to rediscover functional programming. I say rediscover rather than discover because functional programming goes back all the way to Lisp, whose spec was written in 1958, which is the dawn of time as far as modern computing is concerned. As Wikipedia puts it, Lisp is “the second-oldest high-level programming language in widespread use today. Only Fortran is older, by one year.”

(Both Fortran and Lisp are heavily based in mathematics — in fact, Fortran is short for FORmula TRANslation. This is one of the reasons that there’s a strong math bias in programming to this day.)

One senior developer I know tested prospective developers’ functional programming skills by issuing this test to anyone who passed the original FizzBuzz test:

Write FizzBuzz, but this time, instead of FizzBuzzifying the numbers 1 through 100, FizzBuzzify the contents of an array, which can contain any number of integers, in any order.

(The senior developer didn’t use the word “FizzBuzzify,” but I think you get my point.)

The resulting app, if given this array…

[30, 41, 8, 26, 3, 7, 11, 5]

…should output this array:

[‘FizzBuzz’, 41, 8, 26, ‘Fizz’, 7, 11, ‘Buzz’]

Note that the original array contained all integers, while the result array can contain both strings and integers. The senior developer was interviewing programmers who’d be working in Ruby, where you can easily use arrays of mixed types.

You’d get a passing grade if your solution simply adapted the original FizzBuzz to take an array as its input. Here’s a Python implementation of that solution:

```def fizzBuzz_list_imperatively(numbers):
finalResult = []

for number in numbers:
currentResult = None
isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)

if isMultipleOf3 and isMultipleOf5:
currentResult = "FizzBuzz"
elif isMultipleOf3:
currentResult = "Fizz"
elif isMultipleOf5:
currentResult = "Buzz"
else:
currentResult = number

finalResult.append(currentResult)

return finalResult```

However, the developer was looking for a more functional approach. In functional programming, if you’re being asked to perform some kind of calculation based on the contents of a list, you should probably use a map, filter, or reduce operation.

In case you’re not quite familiar with what these are, here’s a simple explanation that uses emojis:

The map operation, given a list and a function, applies that function to every item in the given list, which creates a new list. The senior developer granted bonus points to anyone who came up with a map-based solution.

Here’s a Python implementation of what the senior developer was looking for:

```def fizzBuzz_list_functionally(number_list):
return list(map(fizzBuzzify, number_list))

def fizzBuzzify(number):
isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)

if isMultipleOf3 and isMultipleOf5:
return "FizzBuzz"
elif isMultipleOf3:
return "Fizz"
elif isMultipleOf5:
return "Buzz"
else:
return number```

This implementation breaks the problem into two functions:

• A `fizzBuzzify()` function, which given a number, returns Fizz, Buzz, FizzBuzz, or the original number, depending on its value, and
• A `map()` function, which applies `fizzBuzzify()` across the entire array.

Remember, the senior developer was looking for Ruby developers, and Ruby doesn’t support nested functions. Python does, however, and I like packaging things neatly to prevent errors. I think that if a function a makes exclusive use of another function b, you should nest b inside a.

With that in mind, let’s update `fizzBuzz_list_functionally()`:

```def fizzBuzz_list_functionally(number_list):

def fizzBuzzify(number):
isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)

if isMultipleOf3 and isMultipleOf5:
return "FizzBuzz"
elif isMultipleOf3:
return "Fizz"
elif isMultipleOf5:
return "Buzz"
else:
return number

return list(map(fizzBuzzify, number_list))```

## What’s next

In the next installment in this series, we’ll look at FizzBuzz solutions that don’t use the modulo (`%`) operator.

Categories

## Spotting the programmers who can’t program

Sooner or later, you will encounter — or worse still, end up working with — a programmer who only seems good at programming. This person will have an impressive-looking resume. They’ll know all the proper terminology, be able to speak intelligently about the key concepts in this programming language or that framework or library, and may even have given a pretty good talk at a meetup or conference. But when they’re put to the task of actually writing working software, they just can’t do it.

These aren’t programmers who have difficulty taking on big problems, such as the kind you run into when working on complex problems and writing all-new code from scratch. They’re not even programmers who run into trouble just working on the sort of everyday problems that you encounter maintaining established, working software. These are programmers who can’t solve simple problems, the sort that you should be able to do during a lunch or coffee break. They might be good in other roles in the development process, but not in one where they have to write production code.

As a result, there’s a category of little assignments whose sole purpose isn’t to identify great programmers, or even good ones, but to spot the ones you shouldn’t hire. The best known of these is the dreaded FizzBuzz.

## FizzBuzz, explained

Fizzbuzz is an exercise that then-developer Imran Ghory wrote about back in 2007. The programmer is asked to implement a program — often in the language of their choice — that prints the numbers from 1 to 100, but…

1. If the number is a multiple of 3, print Fizz instead of the number.
2. If the number is a multiple of 5, print Buzz instead of the number.
3. If the number is a multiple of both 3 and 5, print FizzBuzz instead of the number.

Simply put, its output should look something like this:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz.

In his blog entry on FizzBuzz, Imran wrote:

Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes.

Want to know something scary ? – the majority of comp sci graduates can’t. I’ve also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.

I’m not saying these people can’t write good code, but to do so they’ll take a lot longer to ship it. And in a business environment that’s exactly what you don’t want.

Let’s look at a couple of Python implementations.

## The dumb-as-a-bag-of-hammers solution

If you think your interviewer has a sense of humor, you might try throwing this solution at them. I put this in a file named fizzbuzz.py:

```def fizzBuzzDumb():
return "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz."```

Note that I wrote this as a function that returns a string instead of as just a `print` statement. There’s a reason for this — as a function, it’s testable.

Let’s create a file for FizzBuzz tests. I called mine test_fizzbuzz.py. It’s also pretty dumb — all it does it confirm that `fizzBuzzDumb()` spits out the right result. It’s pretty much guaranteed to pass, since I copied and pasted the string from `fizzBuzzDumb()` into the constant that the test uses to confirm that the output is correct:

```import pytest
from fizzbuzz import fizzBuzzDumb

fizzBuzz1To100Result = "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz."

def test_dumb_fizzBuzz():
result = fizzBuzzDumb()
assert result == fizzBuzz1To100Result, f"The dumb solution returned the wrong result:\nExpected: {fizzBuzz1To100Result}\nActual: {result}."```

With fizzbuzz.py and test_fizzbuzz.py in hand, I ran the test, and it unsurprisingly passed:

## Working towards a real solution

### The multiple problem

The first stumbling block I’ve seen people trying to write FizzBuzz is that they have no idea how to tell if a number x is a multiple of some other number y. This happens particularly often when the programmer doesn’t come from a math background.

(There’s a bit of math snobbery and bias in classical computer science. Some of it is just general academic snobbery, and some of it is from the fact that computer science wasn’t originally its own field of study in universities, but often a branch of the math or engineering department.)

To solve this problem, you want to use the modulo operator — the % symbol. It performs integer division, but instead of giving you the quotient (the result of a division), it gives you the remainder.

For example `3 % 2` gives you a result of 1. That’s because after dividing 3 by 2, you get a remainder of 1. `5 % 2` also gives you a result of 1, because 2 goes into 5 twice, leaving you a remainder of 1. `9 % 5` gives you a result of 4, as 5 goes into 9 once, leaving a remainder of 4.

If a division operation results in no remainder, % returns a result of 0. For instance `2 % 2`, `4 % 2`, `6 % 2`, `8 % 2`, and `10 % 2` all return a result of zero, since they’re all even numbers, which are all evenly divisible by 2. Another way of putting it is to say that they’re multiples of 2.

With this in mind, we can easily come up with a couple of statements that test if a number is a multiple of 3 and if a number is a multiple of 5:

```isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)```

### Fizz, Buzz, FizzBuzz, or number?

This is the part that really trips up the weak programmers. It doesn’t follow this simple decision structure:

• If condition A is met:
• Otherwise, if condition B is met:
• Otherwise, if condition C is met:
• Otherwise, if none of the above conditions are met:

FizzBuzz’s decision structure is more like this:

• If condition A is met:
• Otherwise, if condition B is met:
• But if both condition A and B are met:
• Otherwise, if none of the above conditions are met:

This seems like a minor change, but it’s enough to make FizzBuzz a way of filtering out people might have serious trouble writing production software.

I’ve demonstrated live-coding Fizzbuzz in a number of presentations, and the approach I usually take is summarized in this Python code:

```if isMultipleOf3 and isMultipleOf5:
currentResult = "FizzBuzz"
elif isMultipleOf3:
currentResult = "Fizz"
elif isMultipleOf5:
currentResult = "Buzz"
else:
currentResult = str(number)```

At the end of this `if` statement, currentResult contains one of the following: FizzBuzz, Fizz, Buzz, or a number.

When live-coding in front of an audience — which is pretty much what a technical interview is — you want to keep a couple of things in mind:

• You want to use the code to communicate your intent to the audience as clearly as possible.
• Complexity is your enemy. You want to make the simplest thing that works.

My approach was to do handle the trickiest case first. My `if` statement handles the case where the number is both a multiple of 3 and a multiple of 5 first, followed by the individual multiple cases, followed by the default case. It’s simple, it’s easy to follow, and best of all, it works.

### Putting it all together

Here’s the `fizzBuzz()` function that I wrote. I put it in fizzbuzz.py, just after the definition of `fizzBuzzDumb()`:

```def fizzBuzz(first = 1, last = 100):
finalResult = ""

for number in range(first, last + 1):
currentResult = ""
isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)

if isMultipleOf3 and isMultipleOf5:
currentResult = "FizzBuzz"
elif isMultipleOf3:
currentResult = "Fizz"
elif isMultipleOf5:
currentResult = "Buzz"
else:
currentResult = str(number)

finalResult += currentResult

if number < last:
finalResult += ", "
else:
finalResult += "."

return finalResult```

Here’s the full text of fizzbuzz.py:

```def fizzBuzzDumb():
return "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz."

def fizzBuzz(first = 1, last = 100):
finalResult = ""

for number in range(first, last + 1):
currentResult = ""
isMultipleOf3 = (number % 3 == 0)
isMultipleOf5 = (number % 5 == 0)

if isMultipleOf3 and isMultipleOf5:
currentResult = "FizzBuzz"
elif isMultipleOf3:
currentResult = "Fizz"
elif isMultipleOf5:
currentResult = "Buzz"
else:
currentResult = str(number)

finalResult += currentResult

if number < last:
finalResult += ", "
else:
finalResult += "."

return finalResult```

This new function calls for a new test. Here’s the updated test_fizzbuzz.py:

```import pytest
from fizzbuzz import fizzBuzzDumb, fizzBuzz

fizzBuzz1To100Result = "1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, FizzBuzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, FizzBuzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, FizzBuzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, FizzBuzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz."

def test_dumb_fizzBuzz():
result = fizzBuzzDumb()
assert result == fizzBuzz1To100Result, f"The dumb solution returned the wrong result:\nExpected: {fizzBuzz1To100Result}\nActual: {result}."

def test_fizzBuzz():
result = fizzBuzz()
assert result == fizzBuzz1To100Result, f"The dumb solution returned the wrong result:\nExpected: {fizzBuzz1To100Result}\nActual: {result}."```

With fizzbuzz.py and test_fizzbuzz.py revised, I ran the test, and it passed again:

You can download fizzbuzz.py and test_fizzbuzz.py here (2KB, zipped folder with 2 Python files).

## What’s next

In the next installment in this series, we’ll look at a different approach to coding FizzBuzz: functional programming!

Categories

## Programmer interview challenge 1, revisited: Revising “Anagram” in Python and implementing it in JavaScript

In the previous article, I wrote about a challenge that’s often given to programmers at a tech interview: Write a program that determines if two words are anagrams. I posted a solution in Python; check it out here. In this article, we’ll refactor the Python code and write a JavaScript implementation.

## Refactoring the Python version

Looking at the code for `anagram()`, it’s quite clear that it isn’t DRY (Don’t Repeat Yourself), but manifestly the opposite: WET (Write Everything Twice)!

Under the time constraints of a technical interview, you might not always have the time or cognitive bandwidth to keep your code DRY, but if you should try to do so if possible. You may find that it helps convey your algorithmic thinking more effectively to the interviewer, and that’s what you want. After all, your goal throughout the process is to prove that you can actually program.

The repeated code is the part that takes a string, sorts its characters into alphabetical order, and removes the leading space if it exists. Let’s turn that code into its own method:

```def sortLetters(word):
# Returns the given word with its letters sorted
# into alphabetical order and with any
word_lowercase = word.lower()
return ''.join(sorted(word_lowercase)).lstrip()```

With this method defined, we can use it in `anagram()`. In fact, we can nest it within `anagram()`. Here’s the revision:

```def anagram(first_word, second_word):
# Returns True if the given words are made of the exact same characters,
# ignoring capitalization and spaces.

def sortLetters(word):
# Returns the given word with its letters sorted
# into alphabetical order and with any
word_lowercase = word.lower()
return ''.join(sorted(word_lowercase)).lstrip()

return sortLetters(first_word) == sortLetters(second_word)```

Creating the `sortLetters()` method doesn’t just DRY up the code, but helps the method better convey what it does. Now, what `anagram()` does is very clearly conveyed by its return statement: it tells you if the first word with its letters sorted is the same as the second word with its letters sorted.

I confirmed that this refactored code works by running the tests, which show just how useful having tests is.

## Implementing `anagram()` in JavaScript

Here’s `anagram()` in JavaScript:

```function anagram(firstWord, secondWord) {

function sortLetters(word) {
return word
.toLowerCase()
.split('')
.sort()
.join('')
.trim()
}

return sortLetters(firstWord) === sortLetters(secondWord)
}```

Note that the JavaScript version of `sortLetters()` is structured slightly differently from the Python version. That’s because JavaScript’s `sort()` is an array method rather than a general function like Python’s `sorted()`.

In the JavaScript version of sortLetters(), I use method chaining to spell out what happens to the given word step by step:

1. Convert the word to lower case
2. Convert that into an array of characters
3. Sort that array
4. Convert that array into a string
5. Remove any trailing or leading whitespace

I could’ve written `sortLetters()` this way…

```function sortLetters(word) {
return word.toLowerCase().split('').sort().join('').trim()
}```

…but I find that “put each method in the chain on its own line” approach more clearly conveys what I’m trying to do:

```function sortLetters(word) {
return word
.toLowerCase()
.split('')
.sort()
.join('')
.trim()
}```

Next: The Swift version!

Categories

## Programmer interview challenge 1: Anagram

An anagram is a word, phrase, or name that can be formed by rearranging the letters in another word, phrase, or name. For example, iceman is an anagram of cinema, and vice versa. Ignoring spaces and capitalizations, “Florida” is an anagram of “rod fail”.

“Anagram” is a common programming challenge that I’ve seen issued to prospective developers in technical interviews: Write a program or function that can tell if two given words are anagrams of each other. Here’s how you solve it.

## The general idea

One solution to the problem is hinted at in the definition of “anagram”. Let’s look at it again:

An anagram is a word, phrase, or name that can be formed by rearranging the letters in another word, phrase, or name.

The word rearranging should be your hint. Somehow, the solution should involve rearranging the letters of both words so that you can compare them. Another word for rearranging is reordering, and when you encounter that word in programming, you should think of sorting. That’s where the solution lies:

If two words are anagrams of each other, sorting each word’s letters into alphabetical order should create two identical words. For example, if you sort the letters in cinema and iceman into alphabetical order, both will be turned into aceimn.

With that in mind, let’s try writing an anagram-detecting function. Given two strings, it should return `true` if they’re anagrams of each other, and `false` otherwise.

## The Python version

This assumes that you’ve got Python and pytest installed on your system. I recommend installing the Individual Edition of the Anaconda Python distribution, followed by entering `pip install pytest` at the commend line.

Let’s do this the test-first way. We’ll write the test first, and then write the code. This means that we’ll want to create two files:

• anagram.py, which will contain the actual code of the solution, and
• test_anagram.py, which will contain test for that code.

Here’s the code for the test:

```import pytest
from anagram import anagram

def test_simple_anagram():
assert anagram('iceman', 'cinema'), "'cinema' is an anagram of 'iceman'."```

…and here’s the code for the initial iteration of the anagram function:

```def anagram(first_word, second_word):
return False```

To run the test, enter `pytest` at the command line. You should see output that looks like this:

Now that we have a failing test, let’s write code to make it pass.

Sorting the letters in a string is Python can be done by using a couple of methods:

• `sorted()`, which when given a string, returns an array containing that string’s letters in ascending order. For example, `sorted('cinema')` returns `['a', 'c', 'e', 'i', 'm', 'n']`.
• `join()`, which when given a string and an array, returns a string where the elements of the array are joined by the given string. For example, `'*'.join(['a', 'b', 'c'])` returns `'a*b*c'`.

Here’s my code:

```def anagram(first_word, second_word):
first_word_sorted = ''.join(sorted(first_word))
second_word_sorted = ''.join(sorted(second_word))
return first_word_sorted == second_word_sorted```

Running `pytest` now gives a passing test:

Let’s now deal with cases with capitalization and spaces. Ideally, the `anagram()` method should treat “Florida” and “rod fail” as anagrams. We’ll specify this in the test:

```import pytest
from anagram import anagram

def test_simple_anagram():
assert anagram('iceman', 'cinema'), "'cinema' is an anagram of 'iceman'."

def test_complex_anagram():
assert anagram('Florida', 'rod fail'), "'rod fail', if you ignore spaces and capitalization, is an anagram of 'Florida'."```

Running `pytest` yields these results: 1 failed test and 1 passed test…

We can fix this through the use of another two methods:

• `lower()`, which when applied to a string, converts all its letters to lowercase. For example, `'RADAR'.lower()` returns `'radar'`.
• `lstrip()`, which when applied to a string, removes any whitespace characters from the left side. Since the space character has a lower value than any letter in the alphabet, it will always be the leftmost character in a string whose characters have been sorted into ascending order.

Here’s my revised code:

```def anagram(first_word, second_word):
first_word_lowercase = first_word.lower()
first_word_sorted = ''.join(sorted(first_word_lowercase)).lstrip()
second_word_lowercase = second_word.lower()
second_word_sorted = ''.join(sorted(second_word_lowercase)).lstrip()
return first_word_sorted == second_word_sorted```

Running `pytest` now shows that all the tests pass:

Just to be safe, let’s add a test to make sure than `anagram()` returns `False` when given two strings that are not anagrams of each other:

```import pytest
from anagram import anagram

def test_simple_anagram():
assert anagram('iceman', 'cinema'), "'cinema' is an anagram of 'iceman'."

def test_complex_anagram():
assert anagram('Florida', 'rod fail'), "'rod fail', if you ignore spaces and capitalization, is an anagram of 'Florida'."

def test_non_anagram():
assert anagram('ABC', 'xyz') == False, "'ABC' and 'xyz' are not anagrams."```

All test pass when `pytest` is run:

And trying all sorts of pairs of strings confirms what the test tells us: `anagram()` works!

```# All of these return True
anagram('Oregon', 'no ogre')
anagram('North Dakota', 'drank a tooth')
anagram('Wisconsin', 'cows in sin')

# All of these return False
anagram('Florida', 'i oil sauna') # Try Louisiana
anagram('New York', 'on my wig') # Try Wyoming
anagram('Georgia', 'navy sin panel') # Try Pennsylvania
```

…and there you have it!

Next: Implementing `anagram()` in JavaScript, Swift, and possibly other languages.

Categories

## A couple of handy Python methods that use regular expressions: “Word to initialism” and “Initialism to acronym”

Comic by xkcd. Tap to see the source.

## tl;dr: Here’s the code

It’s nothing fancy — a couple of Python one-line methods:

• `word_to_initialism()`, which converts a word into an initialism
• `initialism_to_acronym()`, which turns an initialism into an acronym
```import re

def word_to_initialism(word):
"""Turns every letter in a given word to an uppercase letter followed by a period.

For example, it turns “goat” into “G.O.A.T.”.
"""
return re.sub('([a-zA-Z])', '\\1.', word).upper()

def initialism_to_acronym(initialism):
"""Removes the period from an initialism, turning it into an acronym.

For example, it turns “N.A.S.A.” into “NASA”.
"""
return re.sub('\.', '', initialism)```

## The project and its dictionary

I’ve been working on a Python project that makes use of a JSON “dictionary” file of words or phrases and their definitions. Here’s a sample of the first few entries in the file, formatted nicely so that they’re a little more readable:

```{
"abandoned industrial site": [
"Site that cannot be used for any purpose, being contaminated by pollutants."
],

"abandoned vehicle": [
"A vehicle that has been discarded in the environment, urban or otherwise, often found wrecked, destroyed, damaged or with a major component part stolen or missing."
],

"abiotic factor": [
"Physical, chemical and other non-living environmental factor."
],

"Any street or narrow stretch of paved surface that leads to a specific destination, such as a main highway."
],

"The ability to bring goods to and from a port that is able to harbor sea faring vessels."
],

"accident": [
"An unexpected, unfortunate mishap, failure or loss with the potential for harming human life, property or the environment.",
"An event that happens suddenly or by chance without an apparent cause."
],

"accumulator": [
"A rechargeable device for storing electrical energy in the form of chemical energy, consisting of one or more separate secondary cells.\\n(Source: CED)"
],

"acidification": [
"Addition of an acid to a solution until the pH falls below 7."
],

"acidity": [
"The state of being acid that is of being capable of transferring a hydrogen ion in solution."
],

"acidity degree": [
"The amount of acid present in a solution, often expressed in terms of pH."
],

"acid rain": [
"Rain having a pH less than 5.6."
],

"acid": [
"A compound capable of transferring a hydrogen ion in solution.",
"Being harsh or corrosive in tone.",
"Having an acid, sharp or tangy taste.",
"A powerful hallucinogenic drug manufactured from lysergic acid.",
"Having a pH less than 7, or being sour, or having the strength to neutralize  alkalis, or turning a litmus paper red."
],

...

}```

The dictionary’s keys are strings that represent the words or phrases, while its values are arrays, where each element in that array is a definition for that word or phrase. To look up the meaning(s) of the word “acid,” you’d use the statement `dictionary["acid"]`.

Dictionary keys are case-sensitive. For most words and phrases in the dictionary, that’s not a problem. Any entry in the dictionary that isn’t for a proper noun (the name of a person, place, organization, or the title of a work) has a completely lowercase key. It’s easy to massage a search term into lowercase with Python’s `lower()` method for strings.

Any entry in the dictionary that is for a proper noun is titlecased — that is, the first letter in each word is uppercase, and the remaining letters are lowercase. Once again, a search term can be massaged into titlecase in Python; that’s what the`title()`method for strings is for.

When looking up an entry in the dictionary, my application tries a reasonable set of variations on the search term:

• As entered by the user (stripped of leading and trailing spaces, and sanitized)
• Converted to lowercase with `lower()`
• Converted to titlecase with `title()`
• Converted to uppercase with `upper()`

For example, for the search term “FLorida” (the “FL” capitalization is an intentional typo), the program tries querying the dictionary using `dictionary["FLorida"]`, `dictionary["florida"]`, and `dictionary["Florida"]`.

Looking up words or phrases made out of initials are a little more challenging because people spell them differently:

• The Latin term for “after noon” — post meridiem — is spelled as pm, p.m., PM, and P.M.
• Some people write the short form for “United States of America” as USA, while others write it as U.S.A.

To solve this problem, I wrote two short methods:

• `word_to_initialism()`, which converts a word into an initialism
• `initialism_to_acronym()`, which turns an initialism into an acronym

Here’s the code for both…

```import re

def word_to_initialism(word):
"""Turns every letter in a given word to an uppercase letter followed by a period.

For example, it turns “goat” into “G.O.A.T.”.
"""
return re.sub('([a-zA-Z])', '\\1.', word).upper()

def initialism_to_acronym(initialism):
"""Removes the period from an initialism, turning it into an acronym.

For example, it turns “N.A.S.A.” into “NASA”.
"""
return re.sub('\.', '', initialism)```

…and here are these methods in action:

```# Outputs “G.O.A.T.”
print(f"word_to_initialism(): {word_to_initialism('goat')}")

print(f"initialism_to_acronym(): {initialism_to_acronym('R.A.D.A.R.')}")```

Both use regular expressions. Here’s the regular expression statement that drives`word_to_initialism()`:

`re.sub('([a-zA-Z])', '\\1.', word)`

`re.sub()` is Python’s regular expression substitution method, and it takes three arguments:

• The pattern to look for, which in this case is `[a-zA-Z]`, which means “any alphabetical character in the given string, whether lowercase or uppercase”. Putting this in parentheses puts the pattern in a group.
• The replacement, which in this case is `\\1.`. The `\\1` specifies that the replacement will start with the contents of the first group, which is the detected alphabetical character. It’s followed by the string literal `.` (period), which means that a period will be added to the end of every alphabetical character in the given string.
• The given string, in which the replacement is supposed to take place.

The regular expression behind `initialism_to_acronym()` is even simpler:

`re.sub('\.', '', initialism)`

In this method, `re.sub()` is given these arguments:

• The pattern to look for, which in this case is `\.`, which means “any period character”.
• The replacement, which is the empty string.
• The given string, in which the replacement is supposed to take place.