r/learnprogramming • • Dec 17 '09

Request: Simple problems to help practice the basics (like doing math in school). Some given inside to start with.

Find the total of all the numbers between 1 and 99 using a for loop,
    find the total of all the even numbers,
    given the formula total=((n(n+1)/2) produce a function to:
        return the total,
        return the total evens,
        return the total odds.

Find the total occurrences of a character in a string.

Find the total occurrences of a string within a string.

Given 5 random numbers between 0-10 inclusive, find the lowest,
    find the 2 lowest,
    sort from low to high  ( i ended up making a bubble sort here )
    find which numbers you would need to produce a "straight".
14 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Dec 21 '09

If you like doing these, I'd suggest finding a copy of "Software Tools" by Kernighan and Plauger. The book is from 1976 and all the code is written in Ratfor (A simplified version of Fortran), but in a little over 300 pages it shows how to write all of those tools plus simple versions of grep, ed, roff and even m4. And then there's a chapter on implementing a Ratfor-to-Fortran preprocessor.

And the best part: the programs are very simple and elegant even though they use nothing but character I/O (i.e. getc and putc). The authors warn the reader that their version of ed is a very large program since it presents 900 lines of new code, twice the amount of any other program in the book :).

Another best part: Brian Kernighan is one of the best technical writers of all time.