Jake Worth

Jake Worth

Exercism Raindrops in Python

Published: December 06, 2020 2 min read

  • python

I’ve been learning a bit of Python this Fall to facilitate conversations with a mentee. In this post, I’m going to share the first Python function I’ve ever written, a solution to the Exercism challenge ‘Raindrops’.

Is this good Python code? I don’t know; that’s why I’m posting it here. I’ve read $ python3 -c 'import this' yet am still gloriously ignorant. Below I’ll share the problem, my solution, and a code self-review via praise and criticism.

Problem Description

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.

The rules of raindrops are that if a given number:

  • has 3 as a factor, add ‘Pling’ to the result.
  • has 5 as a factor, add ‘Plang’ to the result.
  • has 7 as a factor, add ‘Plong’ to the result.
  • does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

My Solution

def convert(number):
    result = str(number) + " "
    if number % 3 == 0:
        result += "Pling"

    if number % 5 == 0:
        result += "Plang"

    if number % 7 == 0:
        result += "Plong"

    return result.split()[-1]

Praise

Starting with a very low bar, I like that this solution is compilable Python code. I think the solution is very readable. My return statement is a bit clever; it takes the last item in the string which is either some variant of the Raindrops string, or the number itself as a string. I’ve done this exercise in a few languages and it’s a take I’ve never thought of before, and so I enjoy the novelty there.

Criticism

I don’t like that my function argument is called number; I took that from the setup code but I prefer to avoid argument names that carry their type. In this case, the input really is a meaningless number, so I’m not sure what a better alternative would be.

I try to avoid conditionals and there are three here. I’d prefer a data structure that we can fetch from whenever possible. For the sake of speed and simplicity, I didn’t go that route, but I am sure that I will as I get more comfortable with Python.

As mentioned in the previous section, return result.split()[-1] is clever, in both the good and bad meanings of that word. I can imagine someone who is newer to string manipulation struggling with that a bit. It isn’t the most beginner-friendly code I’ve ever written.

Conclusion

I hope this beginner Python code was entertaining to someone. It lives online at this permalink.

What are your thoughts on this? Let me know!


Join 100+ engineers who subscribe for advice, commentary, and technical deep-dives into the world of software.