[LRUG] practice problems (1)

John Scholes j3s1c4 at googlemail.com
Thu May 17 09:18:23 PDT 2007


There seems to have been a good deal of discussion about how people (who 
are new to it) can get to grips with Ruby, both at the last meeting and 
in the subsequent chat. I noticed that some of those asking did not seem 
to be experienced programmers, so they may also need practice at 
programming generally.

Some people may be familiar with the Ruby Quiz book, but the problems in 
that are generally fairly substantial and require some background 
reading. I was wondering whether anyone would be interested in some 
simpler (but still reasonably challenging) problems, posted on here.

For example:

======== start problem 1 =========

Start with the integer n. If n is even divide by 2; or if n is odd, 
multiply by 3 and add 1. Repeat until you reach 1. For example 10, 5, 
16, 8, 4, 2, 1. You always do reach 1 (although no one has proved it 
yet). Let f(n) be the no. of integers in the list (you generate to get 
to 1) eg f(1) = 1, f(2) = 2, f(10) = 7.

Input: the input is a sequence of integers, two per line, with a space 
between the integers on the same line. All integers in the sequence are 
guaranteed to be greater than 0 and less than 1000000.

Output: for each input pair i, j, output the smaller of i, j, then the 
larger, then the max f(k) for k between i and j (and including i and j). 
These three numbers should be separated by a space and each triple 
should appear on a new line.

eg input
10 1
7 7

output
1 10 20
7 7 17

======== end problem 1 =========

This is fairly basic.

If this kind of thing is any use, I could post one a week ...

This is in the tradition of programming competitions, which tend to be 
in a special environment, which automatically accepts and checks your 
answer, all against the clock ... The problems in such things vary quite 
a lot in difficulty. The hardest ones can be quite challenging.

Of course, this kind of thing does not take any advantage of the special 
features of Ruby, let alone Rails. But it is not meant to. It is meant 
to get people familiar with the basics (once they have got their 
environment up and can make a program run). But
(a) to be more challenging (and hence more interesting) than pure "5 
finger exercises";
(b) to require some basic programming skills (and the opportunity to 
acquire them through doing), which are needed in any language.

John




More information about the Chat mailing list