Fall 2023 Ruby Exam
Problem 1
Write a main script that accepts a word as a command-line argument (via ARGV
) and prints the word with letters gradually replaced by underscores. One letter is replaced per line, and the letters are replaced in lexicographic (dictionary) order. In case of ties, the earlier letter is replaced first. For example:
uncouth
un_outh
un_out_
un__ut_
u___ut_
u___u__
____u__
_______
Problem 2
Define a class CountedSet
that keeps an inventory of how many times an item has been added to it. It has the following methods:
-
add
, which accepts one parameter: the item to add. It increments the item's quantity by 1. -
count
, which accepts one parameter: an item. It reports how many instances of the given item are in the inventory. If the item has never been added, its count is 0. -
remove
, which accepts one parameter: an item to remove. If the set contains the item, it decrements the item's quantity by 1. Otherwise it ignores the request.
Problem 3
Define method moots
that accepts an array of people hashes. Each person has two keys: name
and partner
. Both keys are symbols, and their associated values are strings. It returns an array of just the names of people whose partners reciprocate the partnership, with the names in the same order as the original array. For example:
p moots([
{name: 'Valentina', partner: 'Bert'},
{name: 'Bert', partner: 'Valentina'},
{name: 'Wei', partner: 'Valentina'},
])
# prints ['Valentina', 'Bert']
Write a succinct higher-order expression rather than a long series of statements. Do not use loops or each
.
Problem 4
A noodd is a creature that traverses a two-dimensional integer grid but refuses to enter cells whose row and column are both odd. Define class Noodd
to model a noodd, which starts its life at (0, 1). It has the following methods:
-
Getters for the noodd's
x
andy
properties. -
Predicate
permits?
that accepts two parameters: an x-position and a y-position of a cell. It returns true if and only if the noodd would consent to moving into the designated cell, regardless of its current location. -
Method
move
that accepts a direction parameter and moves the noodd one unit in the given direction. The direction is one of:up
,:down
,:left
, and:right
. If the noodd is asked to move to a prohibited cell, it silently refuses.
Problem 5
Define method encode
to apply a substitution cypher to a message. It accepts two parameters in the following order: a string to be encoded and a codebook hash describing how characters are substituted. It returns the encoded string. For example:
p encode("Health", {'h' => 'p', 'e' => 'y'})
# prints "Pyaltp"
The codebook says that Hs are substituted with Ps, and Es with Ys. If a character doesn't have a substitution in the codebook, it remains the same. Case is preserved across the substitutions, but only lowercase versions of letters are explicitly included in the codebook.
Write a succinct higher-order expression rather than a long series of statements. Do not use loops or each
.
Problem 6
Define method mean_pets
that accepts an array of people hashes. Each person has two keys: name
, pet_count
, and birth_year
. Both keys are symbols. It returns a hash that maps each birth year present in the array to the average number of pets owned by people born in that year. For example:
p mean_pets([
{name: 'Ali', pet_count: 5, birth_year: 1992},
{name: 'Bro', pet_count: 10, birth_year: 1992},
{name: 'Cam', pet_count: 3, birth_year: 2001},
])
# prints {1992 => 7.5, 2001 => 3}