share
Stack OverflowWhat are ten really good, short programs you can write to help become fluent with a new language?
[+60] [19] BeachRunnerJoe
[2010-02-17 21:10:03]
[ c# career-development self-improvement ]
[ http://stackoverflow.com/questions/2284425] [DELETED]

Possible Duplicate:
“Projects” for learning a new language [1]

Hello! I'm about to begin learning C# and ASP.Net and I was wondering if anyone had any suggestions for ten really good, short programs (i.e. Hello World, Towers of Hanoi, etc.) to write to help become fluent with a new language .

(6) This should be communiti wiki - William Pursell
(6) You don't become fluent in a language by writing ten programs. On a basic C programming course that I was teaching the students write 24 programs, learning new things all the time, without still feeling fluent after they finish. And C is a very simple language - most modern languages require even more practice. - Tronic
(5) @Tronic: "The journey of a thousand miles begins with a single step." -- Confucius - John Feminella
wowzers! this was a popular question, thanks so much everyone! - BeachRunnerJoe
(3) I think that this stackoverflow.com/questions/10936/… is the first instance of this question. But it is not the most recent by long way. Benn asked over and over and over again... stackoverflow.com/search?q=learning+projects - dmckee
[+42] [2010-02-17 21:37:53] John Feminella [ACCEPTED]

I actually made a list for myself of about sixty software exercises I try to do in new languages/frameworks. Here's a few I think would be useful:

Beginner

  • [Phonebook.] Generate a list of 5,000 random names of people and generate 0 to 3 random phone numbers for each person.

    • Write something which, when given a person's last name, will find all the people with that last name.
    • Write something which, when given a person's phone number, will find the person.
    • Given a person, find all their phone numbers.
  • [Primes.] Compute the 5,000th prime number.

  • [Syntax: Collections.] Write a method M which takes an array of type T and some sort of reference to another method m which operates on Ts. Call M on various arrays with different ms (e.g., print this item; add 5 to this item; put this item in another collection if it matches a condition; etc.) to see if you can produce useful results.

  • [Syntax: Big numbers.] 3^3 is 27; 3^3^3^2 is over 9,000 digits long. Compute the exact value of 3^3^3^2.

Intermediate

  • [Blackjack.] Play a simple interactive game of blackjack. Use a "real" deck, such that you don't shuffle until needed (instead of after every hand). Keep track of and report win/loss rate on demand. Allow user to stand, hit, double down, split (if two cards are the same, start two new hands that each use one of those cards), and surrender (recover half your bet if you don't want to continue the hand and you haven't made a move yet).

  • [Choose Your Own Adventure.] Make an interactive state machine which is essentially a choose-your-own-adventure game. Allow the user to save their state and restore it from a file.

  • [Integration.] Get the 10 most recent StackOverflow questions, the author of each one, and the number of votes that this question has.

  • [Sudoku Solver.] Given a 9x9 input consisting of the characters 1-9 or ".", determine what values must go in the "." to make a valid Sudoku solution, or return "error" if no solution is possible.

Advanced

  • [Blogging I.] Write a small blogging platform consisting of Posts and Comments. If you authenticate, you can edit posts. Anybody, authenticated or not, can leave Comments on a Post. Initially, users just have a user-id and plaintext password stored manually (there is no interface to add/remove users). There are five different views:

    • show every post and its comment count
    • show a particular post and its comments, and anonymously comment on that post
    • edit a particular post (authn required)
    • create a new post (authn required)
    • show every user (authn required)
  • [Blogging II.] Enhance the Blogging I exercise. Add a few more features:

    • new users can register
    • authenticated users get their name attached to each comment they make
    • forgot-my-password retrieval feature
    • store hashed + salted passwords instead of plaintext passwords
  • [Blogging III.] Enhance the Blogging II exercise. Integrate with external APIs:

    • when a new post is made, tweet and provide a URL-shortened link
    • when a new post is made, update your Facebook status

(7) Doing the blogging exercises should be pretty interesting in something like C or assembly.. interfacing with CGI in assembly.. heh - Earlz
Ha! Yes, these will have varying degrees of difficulty in different languages. - John Feminella
this sounds like an excellent list of exercises and exactly what I was looking for, thanks! - BeachRunnerJoe
Awesome list, thanks so much for sharing. - Epaga
(1) @Earlz: my (not very updated) blog, segfaulting.com, is written in C++.. I was bored. - Andreas Bonini
@Andreas: I applaud your dedication (or perhaps your masochism). - John Feminella
1
[+18] [2010-02-17 21:13:24] Brian MacKay

Rosetta Code

Rosetta Code [1] might be a good fit. It has all kinds of tasks and little games. It can show you completed versions of the problems in multiple languages, providing an extremely useful point of reference!

Here's a link to a solution for "print out all the words from the 99 bottles of beer on the wall song", in the key of F#: Solution [2]

Project Euler

You could also work through some of the problems on Project Euler [3].

Of course, Euler is pretty challenging, and as some commentors pointed out, it is perhaps overly focused on math.

[1] http://rosettacode.org
[2] http://rosettacode.org/wiki/99_Bottles_of_Beer#F.23
[3] http://projecteuler.net/

(5) I think project Euler helps more in a mathematical way of learning than a programming way - Samuel Carrijo
(1) That's true, but you're still coding. Once you have the problem figured out mathematically, you have to express it in code, right? Also, understanding how the language works with math is important. - Brian MacKay
(2) Yeah, but I might not learn OO through it, for instance. One can do all the problems using Java, but do it as procedural code (specially if he's a former C programmer) - Samuel Carrijo
Well it's not going to give him a CS degree or anything like that. :) I'm just saying it might be a fun way to get your feet wet with a new language. - Brian MacKay
I liked Rosetta Code suggestion. You get my upvote =) - Samuel Carrijo
I love Project Euler (52 and counting), and I think it's a great tool for CS people...but not for learning new languages. It's very focused on math (which is good), and on finding efficent algorithms and data structures (also good), but not so much on any features of the language it's being written in. I don't think I've created a meaningful object yet in solving those things, let alone explore more esoteric features of a language (which would be the point...minor syntactical differences don't require projects to learn.) - Beska
Ok, you guys win. :) I'm leaving it though, because it's at least an interesting option and some people voted it up. - Brian MacKay
2
[+9] [2010-02-17 21:29:02] No Refunds No Returns
  1. Create a file and write to it.
  2. Get input from the user and validate it.
  3. Connect to a data base and read/write data to/from it.
  4. Get data from a web site.
  5. The game of life to learn about arrays, math, etc.
  6. A sudoku solver.
  7. Anything that reads/writes to the users profile.

8-10: I'll have to owe you.


(1) Number 1 is absolutely the first thing to do. And make sure you learn how to correctly handle the I/O errors. - William Pursell
(3) Number 1 isn't always the first thing to do. It depends on the language. It certainly wouldn't be in Haskell! - David Johnstone
3
[+8] [2010-02-17 21:25:54] Daniel C. Sobral

I'd go to Rosetta Code [1] and pick ten tasks that looks to play into that language's strength.

But there are two problems with the question, as stated. First, some languages play to some strengths and some to others, so no set of ten problems will ever be a good exercise for a language.

But there's a even more serious problem with this, even assuming that problem is overcome. Once you did a problem in one language -- in fact, once you did a problem in three or four languages that share a similar style, you'll tend to approach that problem the same way with every new language, which is exceedingly counter-productive when trying to learn a new language.

In my opinion, doing familiar problems is a good way to learn the new syntax of things you already know in a new language. In a sense, it is a good fast forward, even if it often leaves holes in one's knowledge that one won't even notice are there.

But to truly know what makes a language different, what pushed someone to create a new language, you have to approach it with a problem you don't know how to solve.

[1] http://rosettacode.org/wiki/Main_Page

(1) Wow. I love this answer. I want to marry it. Easy +1. - Beska
4
[+6] [2010-02-18 00:10:15] Norman Ramsey

I've lost count of the number of programming languages I've learned over the years, so if you haven't learned many languages, my advice might not be right for you. For me the key is that if I'm learning a new language, I must be doing it for a reason. I find it motivating to get to a new problem as soon as possible that will justify learning the new language. Examples:

  • Somebody I admire had been bugging me for years to learn Icon, but I waited until I had a problem that would really stretch Icon's backtracking.

  • I learned Tcl because I really wanted to learn the Tk toolkit.

  • I learned Modula-3 because of the prospect of writing systems code (a debugger) in a language that was mostly safe but would still allow me to do unsafe things.

  • Although I taught Haskell several times, I didn't really become solid in it until a problem captured my imagination that would have been nearly impossible without Haskell's type classes.

  • I will learn Scheme on the day that I really, really want to do something that absolutely has to have macros.

So my advice is this:

Find a problem that is better suited to your chosen language than to any other language, and build a solution to that problem.

If there is no such problem, why are you learning the language in the first place? If other people are forcing you to learn it, find out what problems they think are so important that the language is worth learning...


5
[+3] [2010-02-17 23:18:14] John

Not exactly 10 problem but try S-99: Ninety-Nine Scala Problems [1]. It's applicable to other languages as well, although the answers are only given in Scala.

[1] http://aperiodic.net/phil/scala/s-99/

6
[+3] [2010-02-17 21:17:47] Mick Walker

I dont have 10, but one I always use is, I generally write a program to calculate the date of easter for any given year as a first program when starting a new language.

The reason I do this is, it not only helps you understand the language syntax, it also helps understand the way mathematical operations occur within the given language.

Check out http://www.assa.org.au/edm.html for more information regarding calculating the date of easter.


7
[+2] [2010-02-17 21:15:16] froadie

Simple card games. e.g. Blackjack, War... And once you have the basics you can build up from there - try Poker, add networking, etc.

Also, check out this SO thread [1] for some interesting suggestions...

[1] http://stackoverflow.com/questions/2192808/simple-but-interesting-task-for-c-newbie/2193081#2193081

8
[+2] [2010-02-17 21:41:02] JRL

Look up questions tagged code-golf on stackoverflow and do them (not worrying about character count though).


or rosetta-stone, or something containing toilet - Earlz
9
[+2] [2010-02-17 22:18:10] Jason Roberts
10
[+1] [2010-02-17 21:51:47] David Johnstone

Questions like this have been asked many times before. Even I have had a go at asking this question [1], and you may find the answers there helpful (and there are plenty of links to other similar questions too, which also probably contain useful information).

As other people have pointed out, this practice does have some issues that you need to be aware of:

  • It is tempting to write the program in the same style each time, even though this is not the correct style for the language.
  • Different languages focus on different things, so it may be inappropriate to solve some problems with some languages.

Still, this is not to say that this is a bad idea. I do actually think that having these standard problems is a good idea, as it means that there will be a few programs that you understand how to solve, so the challenge is in coding the solution and working out the correct language constructs to use.

I think the important thing for the problems is that you must find them interesting. One idea that I once heard was to write a simple Twitter client for each new programming language you attempt to learn. Or, you could write a program to solve Boggle [2] or any number of simple games like this.

[1] http://stackoverflow.com/questions/1031926/standard-programming-problems-for-learning-a-new-language
[2] http://en.wikipedia.org/wiki/Boggle

11
[+1] [2010-02-17 21:27:56] stoneyowl

I generally try to implement a Cribbage hand evaluator, and then determine what an average Cribbage hand value is (for, say, 10000 hands). AS stated, this exposes you to the math functions, lists, arrays, output, and input. If I am feeling ambitious I will do some sort of statistics on the spread of hand values, standard deviations, means, etc.


(1) This requires you to actually know what a Cribbage Hand Evaluator is however.. something I doubt many beginning programmers(or non-math majors) know. - Earlz
Actually, most widwest (farm country - Iowa, Nebraska, etc) people have at least heard of the card game of cribbage :) . It has very little to do with Math except being able to count to 31. - stoneyowl
12
[+1] [2010-02-17 21:29:01] cagreen

By far the best application to write to become fluid in a language is.....Hello World, of course!

Just kidding :)


(2) "Hello World" is astonishingly useful: you learn about compiling a program, executing a program, representing String literals, and the basics of the I/O system. - mfx
In java, you even get to learn some oop. What else has System.out.println? Even in Ruby where everything is an object there is a simple puts - Earlz
true, it's a useful first example, but it won't make you fluent. - cagreen
13
[+1] [2010-02-17 21:16:45] Aaron

I'm a big fan of math functions. An easy one to start with is the Sieve of Eratosthenes.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Doing some easy math functions not only helps in learning syntax but also helps in learning some of the basics of the language like how to print output to the console, gather user input, etc.


14
[0] [2010-02-17 21:19:23] jose

Maybe Minesweeper. Or something which explores the data structures and specific logic for the language (like a ticking system with lists, stacks, etc)


15
[0] [2010-02-17 22:22:29] mvl

While this isn't perhaps a direct answer, I might start with a program or programs to answer your own question. What i mean is, that I learn the most from trying to solve problems that I'm actually interested in solving, not simply going though a list of arbitrary problems. You might start by figuring out some basics, like:

  • How to store the problems.
  • Should they be sorted or ranked, and if so, how?
  • Can I write something to gather programming challenges from one or more web sites.

There are endless possibilities, and you would no doubt encounter many features of a language by going down this route. By the time you got through just a bit of it, you'd probably feel like you had a good grasp of the language.

The down side here is that there isn't a clear right/wrong solution, but that's often the case in real world programs. :)


16
[0] [2010-02-17 22:42:16] Duncan

I read somewhere about someone who implements Tetris in every new language he learns.


17
[0] [2010-02-18 06:39:52] Ali

If you are interested in Microsoft Languages then probably you should start from their learning site. http://msdn.microsoft.com/en-us/beginner/default.aspx

hope this helps.


18
[0] [2010-02-18 10:45:52] James Brooks

I try to write a very simple lisp interpreter for every language I learn. It gives you a nice range of skills. It can be broken down into 10 stages if you want :)

  • string manipulation (lexing/tokenising)
  • parsing
  • structures
  • file access
  • coping with errors
  • ...

I like the idea of having the same set of things to learn. They become like a kata practised in different ways. I gained a new understanding of lisp with each new implementation.


19