# Puzzles for n00b Programmers.March 19, 2010 3:29 PM   Subscribe

I'm looking for puzzles for novice programmers similar to The FizzBuzz Question.

I'm in the middle of my first semester of C++, and I'm looking for extracurricular activities to keep me sharp and interested. I've been chewing on the FizzBuzz problem, and enjoying it even though I haven't found a solution yet. Anyone have any suggestions for other puzzles I can play with?

Are there "classic" puzzles? Are there any sites online with a list of such "classic" puzzles?

In before P=NP.
posted by lekvar to Computers & Internet (19 answers total) 19 users marked this as a favorite

Have you seen pythonchallenge.com?
posted by neilkod at 3:39 PM on March 19, 2010

Here's a broad range of programming problems (I previously used this as an answer here.)
posted by Zed at 3:45 PM on March 19, 2010

Writing a program to determine whether a given year is a leap year is a common homework exercise.
posted by ambulatorybird at 3:47 PM on March 19, 2010

Among that broad range, let me emphasize the Perl quiz of the week and Ruby quiz of the week, (whose problems are usually not language-specific.)
posted by Zed at 3:47 PM on March 19, 2010

Project Euler has a very nice list of problems with varying levels of difficulty.
posted by axiom at 4:07 PM on March 19, 2010 [2 favorites]

I've been chewing on the FizzBuzz problem, and enjoying it even though I haven't found a solution yet.

Not to be harsh or anything, but solve the FizzBuzz before looking for more to solve. Because FizzBuzz is trivially easy, and if it's causing you a problem, you need more concentration/resolve not, more puzzles.
posted by orthogonality at 4:50 PM on March 19, 2010 [4 favorites]

Not to be harsh or anything, but solve the FizzBuzz before looking for more to solve. Because FizzBuzz is trivially easy, and if it's causing you a problem, you need more concentration/resolve not, more puzzles.

This indeed. The whole thing with FizzBuzz is that it's not a thinking puzzle. It's a test of basic understanding of how to write a program. It's the next baby step up from Hello World.

Once you understand syntax and the concept of loops and conditionals, there are no shortage of good puzzles.

One that I remember quite fondly from my first year programming course was: "Write a program that takes a single integer as an input and prints out a Towers of Hanoi solution for a game with that many disks."
posted by 256 at 4:57 PM on March 19, 2010

Not to be harsh or anything, but solve the FizzBuzz before looking for more to solve. Because FizzBuzz is trivially easy...

That's totally fair, but for the sake of perspective, I'm not even done with my first semester. My instructor just revealed 'if' statements to my class. I've been reading ahead and scrounging info from outside sources because the class is so. painfully. sloooooooow.

I'm hoping these puzzles will give me a benchmark to judge my extracurricular activities, more than anything. Even a failure is a valid input.
posted by lekvar at 6:19 PM on March 19, 2010 [1 favorite]

Ha! I just worked FizzBuzz out! Watch out, P=NP, you're next!
posted by lekvar at 6:51 PM on March 19, 2010 [3 favorites]

Rosettacode can have some interesting challenges, with even more interesting solutions. It can be enlightening to read, even without attempting to write the code yourself. Also seconding pythonchallenge and project euler as good general-purpose "here's a list of problems, now do them" sites.

You might want to hit up a good programming tutorial along the way. Fizzbuzz is not supposed to be a challenge. At all.
posted by Bobicus at 7:53 PM on March 19, 2010

Useful "puzzles":

Write some data types.

1. A String class. ctor, copy ctor, dtor, catenation operator, to C string operator, index operator. Bad arguments result in throws.

2. Linked list implementations, one intrusive, one not, for lists of int. Then make each type a template, and create linked lists of your String class.

3. Create classes or functions to convert utf-16 (ucs-2) to/from utf-8.

4. Make your string class templated on utf encoding type. Add template member functions that convert strings back and forth between encodings, implemented in terms of the converters.

5. Make your String implement Copy-on-write.
posted by orthogonality at 9:16 PM on March 19, 2010

Try looking at algorithms and pick one to implement? FizzBuzz isn't really a puzzle; it's just a simple algorithm. If you've got a science background there are tons of easy numerical methods to implement.

But you're looking for sharp and interesting, right?

The really sharp and interesting parts of programming are the implementation details. What's the user interface? Command line? iPhone app? Blingy web 2.0 site? How is data stored? Simply writing structs to disk (hint: ouch)? XML? SQL? ORM? NoSQL? Google those. Do you have your source code in an SCM like Subversion (hint: yes)? Is your code organized in layers? Do you unit test? Can you use a debugger? Do you understand threading? This is not meant to be discouraging, but exactly the opposite. If you are stressed by the slow pace of your class, I bet you will get an intellectual kick out of this stuff!

So my advice to look for puzzles is to pick a real-world problem you are interested in or pick an and implement it for real. Write a main method in C++ that does the bare bones algorithm. Try using getopt to parse command line options. Maybe pick a GUI technology you're interested in and try bolting that on. Try setting up Subversion and play with checking your code into a repository. Try saving your state to XML files. Try writing a schema for your XML files. Get frustrated and start over. Yay!

I tried to keep the links vaguely c++-specific but if you really want your mind blown, try learning another language like Java (very employable) or Ruby (very elegant) on the side. The moooooore you knoooow...
posted by mindsound at 9:17 PM on March 19, 2010

orthogonality: ooh, copy-on-write, good idea. Seconded! I once got to make changes to a ten-year-old thread-safe copy-on-write string class. It blew my mind how much STUFF was in that "simple" string class.
posted by mindsound at 9:20 PM on March 19, 2010

Off topic slightly maybe, and I don't know where the author of that FizzBuzz problem got his information, but I just want to point out that in the UK, Fizz Buzz is a drinking game (ie not played by schoolchildren).

But clearly very much aimed at programmers.
posted by Xhris at 9:27 PM on March 19, 2010

TechInterview.org has a bunch of puzzles that are related to programming in varying degrees.
posted by MesoFilter at 10:44 PM on March 19, 2010

The FizzBuzz(n) variant I've seen used in an Australian primary school classroom goes like this: everybody stands up. Teacher nominates a play order, then starts play by saying "1". Each player has to mentally increment the previous player's number, then say "Fizz" if the resulting number is a multiple of n, "Buzz" if it contains the digit n, "FizzBuzz" if both apply, or the number itself if neither does. Anybody who gets it wrong has to sit down. Last player standing gets table points.

Here's a sh one-liner to emit the first 100 correct answers for n=7. Anyone care to offer similar for your own local command interpreter?

`n=7; for i in \$(seq 100); do say=; case \$i in *\$n*) say=Buzz ;; esac; test \$((\$i % \$n)) -eq 0 && say=Fizz\$say; test \$say || say=\$i; echo \$say; done`
posted by flabdablet at 12:23 AM on March 20, 2010

Here's the solution in the worst command interpreter ever created, windows CMD shell:

@ECHO OFF
REM FizzBuzz for Windows CMD Shell, milnak 2010-03-20
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%v IN (1,1,100) DO (
SET o=
SET /A t=%%v%%3
SET /A f=%%v%%5
IF !t! EQU 0 SET o=Fizz
IF !f! EQU 0 SET o=!o!Buzz
IF "!o!"=="" SET o=%%v
ECHO !o!
)
posted by milnak at 1:31 AM on March 20, 2010

I don't think cmd.exe is by no means the worst interpreter ever created; I think that distinction belongs to its predecessor. I will agree that its extensions to command.com's abilities have been grafted on in what's probably the least elegant way ever devised by the mind of man.

Also: your code plays by Jeff Atwood's rules, not our classroom ones. Care to have a crack at those?
posted by flabdablet at 3:44 AM on March 20, 2010

Try writing a Sudoku puzzle solver.
posted by revrii at 11:56 AM on March 20, 2010

« Older Looking for freeware that will tell me the overall...   |   Has Woody Allen seen Miami Rhapsody? Newer »
This thread is closed to new comments.