Assignment title: Information
Programming Assignment 5: Getting your feet wet with Ruby
1 Overview
The key to working with Ruby is understanding the underlying protocol for the different data types. You will
experience this in this assignment by implementing a set of functions that add additional functions for the array
data type in Ruby. Note that you will need to write additional helper functions for each of these problems and that
you should take advantage of predefined methods instead of trying to implement these functions by hand.
2 Problem 1
Create a Ruby script that defines a function occurrences(list,key) that takes an array list and object key and returns
the number of elements in list that are equal to key. Include sufficient test code to test your program (either by
hand or using Ruby's unit test framework).
3 Problem 2
Create a Ruby script that defines a function numSmaller(list,item) that takes an array list of size 1 or larger and
integer item as parameters. It should return the number of elements in the list that are strictly less than item.
Again, include sufficient test code for your solution.
4 Problem 3
Crate a Ruby script that defines a funciton secondSmallest(list) that takes an array and returns the position of the
second smallest element in the list. In case the smallest entry occurs more than once, the second occurrance of the
item would be considered the second smallest entry. For example, in the list [1,1,1,2,3] , the position returned
would be 1.
5 Problem 4
Implement Selection Sort for Ruby arrays. Make certain to include both mutable and immutable versions of the
sorting function (i.e. if you are using ssort for your function name, then you should implement a ssort!(list) and
sort(list) versions of the functions.
6 Problem 5
A list can be recursively defined as: the empty list () a pair (value rest), where value is some value (which may be
another list), and a list rest that is the rest of the list.
Define a class List in Ruby that implements this definition. Your class needs to provide three methods: (1) insert
which inserts a value into a list, (2) delete which removes a value from the list, and (3) traverse which traverses
and prints the values in a list.
Notes:
Use a Ruby arrays as your backing store for the list. Note that value can be any valid type in Ruby, including being
another List. Remember that recursion is your friend.