Categories
Career Programming

Programmer interview challenge 2: The dreaded FizzBuzz, in Python

The “job interview” scene from Good Will Hunting.

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:
    • Perform task X.
  • Otherwise, if condition B is met:
    • Perform task Y.
  • Otherwise, if condition C is met:
    • Perform task Z.
  • Otherwise, if none of the above conditions are met:
    • Perform the default task.

FizzBuzz’s decision structure is more like this:

  • If condition A is met:
    • Perform task X.
  • Otherwise, if condition B is met:
    • Perform task Y.
  • But if both condition A and B are met:
    • Don’t perform task X or Y. Perform task Z instead.
  • Otherwise, if none of the above conditions are met:
    • Perform the default task.

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!

Recommended reading

Previously, in the “Programmer interview challenge” series

Categories
Career Programming

Programmer interview challenge 1, revisited once more: “Anagram” in Ruby and C#

Oh hey — why not one more? In the past three articles, I’ve been writing about a challenge that’s often given to programmers at a tech interview: Write a program that determines if two words are anagrams.

In the first article, I wrote a basic Python solution. In the second article, I refined the Python solution and then based a JavaScript solution on it. In the third article, I showed a couple of Swift implementations — one simple, one hardcore. In this article, I’ll present Ruby and C# solutions.

The Ruby version

I used to work with Ruby and Rails a fair bit, so I really should post a Ruby version:

def sortLetters(word)
   word.downcase.chars.sort.join.strip 
end

def anagram?(word1, word2)
   sortLetters(word1) == sortLetters(word2) 
end

# Let’s try it out
puts anagram?('iceman', 'cinema')     # true
puts anagram?('Florida', 'fail rod')  # true
puts anagram?('ABC', 'xyz')           # false

Some things to note:

  • Ruby doesn’t support nested methods. You can put a method inside another method in Ruby, but that doesn’t make it a nested method. That’s why I’ve defined sortLetters() and anagram?() as two separate methods.
  • I wrote these methods to reflect the preferred “delightfully terse” style that is preferred way in the Ruby world. InsortLetters(), and I skip the return statement (optional in Ruby; in its absence, a method returns the result of its last line), use method chaining, and skip any unnecessary parentheses, hence word.downcase.chars.sort.join.strip, which very clearly states that it does the following:
    • Convert the given string to all lowercase characters.
    • Split the resulting string into an array of characters.
    • Sort the array.
    • Turn the array back into a string.
    • Strip any leading and trailing strings from string. If the original string had any spaces in it, they would be leading spaces in the current string.
  • Following Ruby convention, I added a ? to the “anagram” name to make the name of the method anagram?(), which is the Ruby convention for methods that return boolean values. As with the versions of anagram()that I wrote in JavaScript, Python, and Swift, two words are anagrams of each other if their sorted forms are the same.

If you’re interested in trying the histogram approach, you may want to try out Ruby’s Enumerable#tally method:

'foo'.chars.tally   # results in {"f"=>1, "o"=>2}

The C# version

Here’s the C# version. It uses LINQ, because LINQ often makes life easier:

using System.Linq;

string sortLetters(string word) 
{
  return String.Concat(word.ToLower().OrderBy(c => c)).Trim();
}

static bool anagram(string word1, string word2) 
{
  return sortLetters(word1) == sortLetters(word2);
}

Previously, in the “Programmer interview challenge” series

Categories
Career Programming

Programmer interview challenge 1, revisited again: “Anagram” in Swift, the easy and hardcore way

In the past two articles, I’ve been writing about a challenge that’s often given to programmers at a tech interview: Write a program that determines if two words are anagrams.

In the first article, I wrote a basic Python solution. In the second article, I refined the Python solution and then based a JavaScript solution on it. In this article, I’ll present a couple of Swift solutions.

A simple Swift solution

Here’s a Swift solution that follows the same logic as the Python and JavaScript solutions I presented in the previous article:

func anagram(word1: String, word2: String) -> Bool { 

  func sortLetters(in word: String) -> String { 
    return String(word.lowercased().sorted()).trimmingCharacters(in: .whitespaces)
  } 

  return sortLetters(in: word1) == sortLetters(in: word2) 
}

The Swift version of anagram() takes the same approach as the Python and JavaScript version by making use of a function named sortLetters(in:) that sorts the letters of a string in place. Here’s what it does:

  1. It takes the given word — stored in the parameter word — and converts it to all lowercase letters using String’s lowercased() method.
  2. The all-lowercase word is converted into a sorted array of Characters by String’s sorted() method.
  3. The sorted array of Characters is converted back into a string using String’s initializer.
  4. Any leading whitespace in the string (the result of sorting a String with spaces in it) is removed with String’s trimmingCharacters(in:) method.

Once sortLetters(in:) is defined, all anagram() has to do is apply it to both words and see if the results are the same.

Jessy Catterwaul’s hardcore Swift solution

When something about Swift confuses me (it happens!) my go-to person is Jessy Catterwaul, video course creator at raywenderlich.com (where I’m an author). His approach to the anagram problem was to use histograms and bucketing — or in simpler terms, to use a data structure to store the counts of each letter in both words. If both words have the same number of the same letters, they’re anagrams of each other.

He sent me this code, which defines an extension for Swift’s Dictionary class. It gives Dictionary a new initializer method, init(bucketing:). If you provide this initializer with a string, it creates a dictionary whose keys are the letters in the string, and whose corresponding values are the counts of those letters. For example, if you provide the initializer with the string abbccc, it returns a the dictionary ["a": 1, "b": 2, "c": 3].

Here’s the code:

public extension Dictionary where Value == Int {
  /// Create “buckets” from a sequence of keys,
  /// such as might be used for a hsitogram.
  init<Keys: Sequence>(bucketing unbucketedKeys: Keys)
    where Keys.Element == Key {
      self.init(
        zip(unbucketedKeys, AnyIterator { 1 }),
        uniquingKeysWith: +
      )
    }
}

Once the extension is defined, the following code…

let mississippiLetterCount = Dictionary(bucketing: "Mississippi")
print("Mississippi letter count: \(mississippiLetterCount)")

…gives you this output:

Mississippi letter count: [“s”: 4, “i”: 4, “M”: 1, “p”: 2]

What’s up next

Next week, I’ll showcase another programming challenge that I’ve seen in technical interviews: The dreaded FizzBuzz! I’ll also devote some time to covering the “why” of programming challenges.

Previously, in the “Programmer interview challenge” series

Categories
Career Programming

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
  # leading space removed.
  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
    # leading space removed.
    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!

Previously, in the “Programmer interview challenge” series

Categories
Career Programming

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
Career

Is maintaining a Stack Overflow developer story worth the effort?

Screenshot of the top my Stack Overflow story, as of May 24, 2020.
Tap the image to see the original.

In addition to updating my LinkedIn profile, I’ve also been updating my Stack Overflow developer story. I’m rather fond of the timeline that its Story View provides, and how it lets you highlight more than just positions, but applications you’ve released, your articles or videos, awards, certifications, and assessments, and other milestones:

Screenshot of my Stack Overflow story timeline, as of May 24, 2020.
Tap the image to see the full timeline.

As nice as its format is, I wonder if maintaining it is worth the effort. Does anybody else keep their Stack Overflow developer story up to date? Do people who are hiring look at them in the same way they look at LinkedIn profiles?

Categories
Career Tampa Bay

Why Connections Matter More Than Your Resume: This afternoon on Synapse’s “Libate and Learn”

This afternoon from 4:30 to 5:30, Synapse is hosting a “Libate and Learn” session titled Why Connections Matter More Than Your Resume.

Here’s the abstract:

When looking for employment, we all know how to build a resume. But what else can we do? How important is your network and utilizing your connections? How can we build these connections in a virtual world? Are you wondering how to get in with the right company? Grab a drink, relax, and join us remotely for a FREE virtual event with Synapse’s next episode of Libate and Learn with Rich Heruska, Leigh-Ann Buchanan, and Jill St. Thomas, moderated by Synapse’s CEO Brian Kornfeld. They’ll share best practices for building your network and maintaining connections with the right stakeholder. We will then open up the discussion for Q&A.

Participating in this session will be:

  • Rich Heruska, serial entrepreneur, former accelerator director at Tampa Bay Wave, and Synapse Board Member, who’ll talk about…
    • What small and medium sized businesses are looking for
    • Why building a network during a downtime or recession is important
    • How a network can take you to the next steps of your career
    • What can someone building a network expect out of an entrepreneurial support organization and how can they maximize their benefits
  • Leigh-Ann Buchanan, Founding Executive Director of Venture Cafe Miami, who will cover:
    • Connections matter, particularly to build an ecosystem and with the right stakeholders
    • Ecosystems are built entirely on connections
    • Multiple ways people can get value from connections
    • Who are the important people and stakeholders to network with
    • How networking at in-person events or online can differ, and be similar
  • Jill St. Thomas, Executive Director of Tampa Bay Tech. She will cover:
    • Radical connectivity is the key to a thriving ecosystem
    • How connectivity can lead to jobs and opportunities
    • Connectivity between organizations can benefit everybody, it is not a zero-sum game
    • Continually getting and staying connected can be the difference between success and failure
  • Brian Kornfeld, CEO and Co-Founder of Synapse. He’ll moderate and will also cover
    • How to effectively build a network and promote yourself
    • What to look for when networking
    • How to continually add value to the community, and how that brings value to yourself
    • Things to be prepared for when building a network
    • When is the right time to make an ask

This session is online and free to attend, but you have to pre-register! Pre-register here.