
Learning Ruby with Ninety-Nine Problems (1-10)
February 11, 2008Hello everyone
I’ve recently started learning Ruby. After reading Ben Orenstein’s post On the fundamentals of programming, I decided to start with Dr. Werner Hett’s list of Ninety-Nine Prolog Problems.
My hope is that people with a wide range of talent can get something out of this blog. You are welcome to learn along with me, post your own solutions, discuss benefits and drawbacks, or even educate us on Ruby details that we may have missed.
On to the problems
A few things to remember:
- I am not trying to write a guide; I am just learning myself so your participation is essential.
- Don’t click the links until you have solved the problem on your own and are ready for spoilers.
- The problems are marked with the difficulty level. Detailed information on the difficulty ratings and the problems themselves can be found here
- I’ve modified these from the originals to use more specific Ruby terms and examples.
- Solutions are marked with initials and a number for easy reference while commenting.
- I will take solutions from the comments and put them in the post so they are formatted and easier to read
P01 (*) Find the last element of an array
# example: def my_last(l) # your implementation here end list = [1, 3, 5, 8, 11, 13] my_last list => 13
P02 (*) Find the last but one element of an array
# example: def my_last_but_one(l) # your implementation here end list = [1, 3, 5, 8, 11, 13] my_last_but_one list => 11
P03 (*) Find the K’th element of an array
# example: def element_at(l, k) # your implementation here end list = [1, 3, 5, 8, 11, 13] element_at list, 3 => 5
P04 (*) Find the number of elements of an array
# example: def count_elements(l) # your implementation here end list = [1, 3, 5, 8, 11, 13] count_elements list => 6
P05 (*) Reverse an array
# example: def my_reverse(l) # your implementation here end list = [1, 3, 5, 8, 11, 13] my_reverse list => [13, 11, 8, 5, 3, 1]
P06 (*) Find out whether an array is a palindrome.
A palindrome can be read forward or backward; e.g. %w(x a m a x).
# example: def is_palindrome(l) # your implementation here end list = %w(fall leaves after leaves fall) is_palindrome list => true
P07 (**) Flatten a nested array structure
Transform an array, possibly holding arrays as elements into a "flat" array by replacing each array with its elements (recursively).
# example: def my_flatten(l) # your implementation here end list = [1, [2, [3, 4], 5]] my_flatten list => [1, 2, 3, 4, 5]
P08 (**) Eliminate consecutive duplicates of array elements.
If an array contains repeated elements, they should be replaced with a single copy of the element. The order of the elements should not be changed
# example: def compress(l) # your implementation here end list = [1,1,1,2,3,3,1,1,4,5,5,5,5] compress list => [1, 2, 3, 1, 4, 5]
P09 (**) Pack consecutive duplicates of array elements in sub arrays
If an array contains repeated elements they should be placed in separate sub arrays
# example: def pack(l) # your implementation here end list = [1,1,1,2,3,3,1,1,4,5,5,5,5] pack list => [[1, 1, 1], [2], [3, 3], [1, 1], [4], [5, 5, 5, 5]]
P10 (*) Run-length encoding of an array
Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as terms [N,E] where N is the number of duplicates of the element E.
# example: def encode(l) # your implementation here end list = [1,1,1,2,3,3,1,1,4,5,5,5,5] encode list => [[3, 1], [1, 2], [2, 3], [2, 1], [1, 4], [4, 5]]
Nice layout!
Looking good so far, keep it up.
-Ben
Thanks for this. I’ve been meaning to run some exercises such as these.
It wouldn’t be a bad idea to set these up as tests. That way, when a new point release of ruby comes out you could just run the test suite and see if any changes have affected what we’ve learned here. That’s my plan!
Cheers.
Very nice!
I’m just learning Ruby (and Rails ) too. Got all the books just haven’t started actually…reading. So i’ll be checking this blog for motivatioN! Thanks! Keep it up!
Quick question: which plugin do you use to display code? Thx
Paulo: It’s a built in feature of WordPress. Put everything inside \[sourcecode language="ruby"\]your code \[/sourcecode\]
(Remove the backslashes. I put them in to stop the comment from being formatted)
Thanks. It works automatically on sites hosted at WordPress. I found in their FAQ what they used to implement it.
The 99 problems are being converted to Python at
http://aroberge.blogspot.com/2008/02/99-problems-looking-for-volunteers.html
Cheers
Great idea, nicely executed! This will be an excellent reference for the aspiring Rubyist.
Great posts. I’ve had a go at the first 10 in Python:
http://davesquared.blogspot.com/2008/03/99-problems-with-python-1-10.html
after seeing your post referenced by an F# version:
http://www.frickinsweet.com/99problems/post/Starting-things-off.aspx
Thanks for this. I recommend this site to my students of the Free Online Ruby Programming Course.
Hi, your solution to problem 3 does not work like the prolog solution:
| ?- element_at(1,[1,2,3],X).
X = 1 ?
yes
| ?- element_at(Z,[1,2,3],3).
Z = 3 ?
You can specify the index or the element and it will unify the other.
And once you’re done with these 99 toy problems, you could move to the more challenging puzzles from `The First 10 Prolog Programming Contests’ @ http://www.cs.kuleuven.be/~dtai/ppcbook
And now for increased rubyism and challenge:
-write those methods to be instance methods of Enumerable
-change is_palindrome to palindrome?
Thanks for the list of problems, nice work
regards
Would be great if the comments could be moderated and accepted sooner. I’d love to get feedback from other submitters.
thanks