For the past month or so I’ve been spending most of my free time learning how to program. It’s something I’ve wanted to do since college, especially after I came across this essay, but until now I’ve simply lacked the time to really buckle down and do the work.
Well no more.
I’m not sure to what extent I’ll blog my learning process; it’s actually surprisingly difficult to write about the steps taken when solving a programming problem, mainly because there are so many tiny changes made that it’s hard to remember what you did when you finally get the damn thing running. Plus I hate stopping to write something down when I’m on a roll, and I wanted to wait until I have code worth showing.
All of this notwithstanding, I’m posting some code today. I’m pulling the trigger because I know that I often over-think things and I want to avoid that this time.
This snippet is a solution to the infamous FizzBuzz problem, a fairly basic coding challenge which apparently stumps quite a few programming job applicants. It took me about three minutes to write it:
# Python 3.3 for i in range (1, 101): if i % 5 == 0 and i % 3 == 0: print('fizzbuzz') elif i % 5 == 0: print('buzz') elif i % 3 == 0: print('fizz') else: print(i)
This isn’t anything special. The for loop iterates over every integer from 1 to 100, meaning i is first equal to 1, then 2, then 3, …, 100. Keep in mind that range stops one integer shy of the upper bound, in this case 101 . We want the output to be “fizzbuzz” if a given integer is a multiple of both 5 and 3, “buzz” if a given integer is a multiple of 5, and “fizz” if an integer is a multiple of 3. If a number is not a multiple of 5 or 3, we just want it to output the number.
The ‘%’ operator simply means “remainder”, so i % 5 means “the remainder of i divided by 5”, and 5 % 3 would be 2. This is handy because it gives us a way of expressing the concept “is a multiple of”; if 15 is a multiple of 5, then the remainder of 15/5 is zero. Ergo if i % 5 == 0 (don’t forget to use double equals for equivalence), i is a multiple of five and the program should print “buzz”.
With that, I’ve accomplished my mission of putting some code out there. It isn’t much, but it’s something. Mastery has to start somewhere, right?
And if my reader happens to be a prospective employer some months or years down the line, at least you can see I know how to solve the Fizzbuzz problem 🙂
While rereading this post, I discovered that I’d made a coding mistake. My original post featured two code snippets, the first of which was this one:
# Python 3.3 def fizzbuzz(): for i in range (1, 101): if i % 5 == 0 and i % 3 == 0: print('fizzbuzz') if i % 5 == 0: print('buzz') if i % 3 == 0: print('fizz') else: print(i)
If you run this code, it almost works; the error is that on an integer like 5, it prints both ‘buzz’ and the number 5, which is not what I want. The reason is because I used ‘if’ in the two conditional statements:
if i % 5 == 0: print('buzz') if i % 3 == 0: print('fizz')
rather than ‘elif’:
elif i % 5 == 0: print('buzz') elif i % 3 == 0: print('fizz')
I also thought that there was no difference in output when this change was made, but obviously ‘elif’ is more exclusionary than ‘if’. In hindsight I should’ve been a lot more careful in double-checking my code’s output, but the point of the exercise was to learn, and that’s what I’ve done.