Wednesday, March 25, 2015

FizzBuzz Revisited

I first blogged about using FizzBuzz in the classroom four years ago when I didn’t have a classroom and students of my own to use it with. (FizzBuzz–A Programming Question) Well times have changed and today I did assign the project to a room full of students.

Briefly stated the exercise is:

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Most of my students finished it easily during the shortened period we had today. A few in a very short period of time which is great. I should say that these are not yet experienced programmers. We are not even half way though a one semester course. Though we have covered loops and decision structures. So they know the concepts but haven’t had a lot of practice by any means. So I’m pleased with the results. What pleases me the most is that the solutions are not identical.

There are at least three different ways students set up the if statements to determine what to show when.

  • Check for evenly divisible by both 3 and 5 first.
  • Check for evenly divisible by 15 first
  • Check for divisible by 3 but not 5 first and 5 but not 3 second.

They all work of course. The last one, while more complicated, demonstrates a good grasp of compound comparisons. That is sort of a plus.

In my project instructions I asked students to display the results in a listbox. We’re not doing command line programs and haven’t covered file usage yet. We have used listsboxes though. A number of students wrote code that always adds the number to the listbox but then removes it if it is not necessary. Again a bit more complicated and probably slower than other solutions but interesting since I did not teach them how to use the Remove method for listboxes. Clearly someone (or several people) is looking things up. That also makes me happy. I like students to go beyond what is covered in lectures. Plus this is an implementation that for some reason never occurred to me. Who know but that may turn out to be helpful to me some day. I learned something knew and I love learning new things from my students.

Tomorrow we’ll talk about the various solutions and I will have the students explain why they choose the methods they did. Hopefully we can have some discussion about the performance aspects as well. Should be fun.

EDIT: I went looking for an image to post with this article and found one that suggested a visualization of FizzBuzz. I may assign this one next time.

image

Divisible by 3 is blue, divisible by 5 is red, and divisible by both 3 and 5 is green.

1 comment:

Dawn Dupriest said...

I love this activity. My middle school CS class will be learning about loops in a couple of weeks, so this activity would be great.
Divisibility is an underappreciated topic in higher math! Kids dive into it deeply in late elementary school, and in higher math it's used sometimes as a mean to an end, but it's much richer than we give it credit for. You can explore really interesting math and computer science around divisibility.
I've given my students a coding problem I call the "alien coinstar machine" that I use to explore variables and divisibility. It's hard - I've always had a difficult time getting a whole class to understand it, but I think it's a cool problem.
https://www.khanacademy.org/computer-programming/planet-zorg/5694408285749248

I think the fizzbuzz problem will be a good one for extending this idea.