zapatify
12/6/2015 - 4:43 PM

Ruby Exercises.md

Ruby Exercises

Updated: Feb 1, 2017

Problems are arranged easiest to hardest. Ruby I (easiest) to Ruby IV (hardest)'

Problem 1 (Ruby I)

Given the array,

words = ["cool","Ruby","programming"]

Using concatenation, output the following:

Programming in Ruby is very cool

and, using interpolation, output this:

Indeed, Ruby programming is cool

Problem 2 (Ruby I)

Create a Ruby program that asks for your last name. Depending on the length of the last name is the output you give. Here are the ranges:

  • Greater than 10 say, "Wow! Your last name is ______ characters long. That's a long name"

  • Between 10 and 5 say, "Your last name is ______ characters long"

  • Less than 5 say, "Your last name is ______ characters long. That's a short name"

Test the following scenarios: 3 characters, 5 characters, 7 characters, 10 characters, and 13 characters

Problem 3 (Ruby I)

Write a program that asks for two numbers (one at a time) and adds them together.

Hint: what data type does gets produce?

Example output:

$ ruby prob3.rb
What is the first number?
4
What is the second number?
10
The sum of the numbers 4 and 10 is 14.
$

Problem 4 (Ruby I)

Write a method that asks for a weight in kilograms and returns the output in pounds (metric to imperical).

Hint: 1 kg = 2.2 lbs

Example output:

$ ruby prob4.rb
What is the weight in kilograms?
100
100 kg = 220 lbs.
$

Problem 5 (Ruby I)

Write a program that asks for a score (an integer), and assigns a letter grade based on the score. Letter grades are assigned as follows:

RangeGrade
100-90A
89-80B
79-70C
69-60D
Less than 60F
More than 100"Wrong score"

Example output:

$ ruby prob4.rb
Enter the score:
84
With a score of 84, you scored a B.
$ ruby prob4.rb
Enter the score:
55
With a score of 55, you scored an F.

Hint: See problem 3

Problem 6 (Ruby I)

rand() is a function that you can use to generate a random number. You can pass it the max number, e.g. rand(10), and it will pick a random number between 0 and 10. You can also specify a range, e.g. rand(2..9) and it will randomly pick a number between 2 and 9. Using rand(10) and a loop, find how many times it takes for your program to randomly pick the number 7. Print the numbers that is selected as you loop.

Example output:

$ ruby prog5.rb
0
4
9
5
8
3
8
1
4
It took 9 times to find 7
$ ruby prog5.rb
0
4
9
5
8
3
It took 6 times to find 7

Problem 7 (Ruby I)

Think of a password and enter it into a variable.
Write a program that asks the user for a password. Until the password you stored is entered, keep asking for the password. They have a maximum of 3 attempts. If they guess the password, welcome them with a message.

Hint: Use a loop to iterate and count your attempts

Example output:

$ Password:
supper
Wrong! You get 2 more attempt(s)
Password:
dinner
Wrong! You get 1 more attempt(s)
Password:
chow
$

Problem 8 (Ruby II)

Build the very beginning of a text-based adventure game with history.

We're going to keep it simple. The possible move choices are: L, R, F, H and E (which stands for left, right, forward, history and exit).

While the user doesn't enter "E", the program should keep asking for moves. However, if they enter "H", the program should output the moves they've made so far, and ask for another move.

Hints:

  • Initialize an empty array (e.g `movements = []`)
  • This exercise tests arrays, if/else statements, puts, gets, and while loops therefore you should have all of those in your program
  • The program should accept both upper and lower case letters.
  • Example output:

    $ ruby prob6.rb
    Enter a move: (L)EFT, (R)IGHT, (F)ORWARD, (H)ISTORY, or (E)XIT:
    l
    Enter a move:
    l
    Enter a move:
    l
    Enter a move:
    f
    Enter a move:
    r
    Enter a move:
    h
    Your moves so far are:
    Left
    Left
    Left
    Forward
    Right
    Enter a move:
    e
    Bye!
    

    Problem 9 (Ruby II)

    Using the following array, fruits = ["apple","pear","plum","grape","banana"] , write a program that prints the number of fruit in the array, then randomly prints out all the fruit. Every time you call the program, it should print the list in a different order.

    Example output:

    $ ruby prob7.rb
    length: 5
    apple
    banana
    plum
    grape
    pear
    $ ruby prob7.rb
    length: 5
    pear
    banana
    plum
    grape
    apple
    $ ruby prob7.rb
    length: 5
    plum
    apple
    grape
    banana
    pear
    

    Problem 10 (Ruby II)

    Using the following array, fruits = ["apple","pear","plum","grape","banana"] ,write a program that deletes one fruit from the array as you loop (doesn't matter which), until the array is empty.

    Hint: Use a while loop and test the array length as a .each will not work.

    Example output:

    $ ruby prob8.rb
    length: 5
    apple
    banana
    plum
    grape
    pear
    length: 4
    pear
    plum
    grape
    apple
    length: 3
    plum
    apple
    pear
    

    Extra credit: Why will a .each not work in this case?

    Problem 11 (Ruby III)

    Using the following array, santas = %w(Rich Terrence Jennifer Laura Hilary Tao Claire Mandy), write a program that sets up the matches for secret santa. Your program should first test to see if there are an odd number of people, and if such throw an error. If there are even number of players, match people at random, but you cannot match the same person (Rich cannot gift to Rich)

    Example output:

    Hilary is matched with Laura
    Terrence is matched with Mandy
    Rich is matched with Jennifer
    Claire is matched with Tao
    

    Problem 12 (Ruby III)

    Create the first round matches for a tournament using seeds. In an 8-team tournament, the #1 seed is matched with the #8 seed, #2 with #7, #3 with #6, and finally #4 with #5.

    Use this hash for your tournament:

    team_seeds = {1 => "Wisconsin", 2 => "Ohio State", 3 => "Minnesota", 4 => "Michigan", 5 => "Northwestern", 6 => "Indiana", 7 => "Purdue", 8 => "Iowa"}
    

    Create the seeds for the tournament.

    Example output:

    (1)Wisconsin against (8)Iowa
    (2)Ohio State against (7)Purdue
    (3)Minnesota against (6)Indiana
    (4)Michigan against (5)Northwestern
    

    Now, add another team to the hash 9 => "Rutgers". This gives the top seed (#1) a bye, and therefore the matchups should start with #2.

    Example output:

    (2)Ohio State against (9)Rutgers
    (3)Minnesota against (8)Iowa
    (4)Michigan against (7)Purdue
    (5)Northwestern against (6)Indiana
    

    Problem 13 (Ruby IV)

    Create a Ruby program that given a speed in miles per hour, and a distance to cover, it will display the speed in miles per minute, and the time needed to cover that distance. Do your error checking (don't accept characters, letters, etc)

    Example output:

    How fast are you going (in MPH):
    21
    How many miles do you want to travel?:
    10
    You will be travelling at 0.35 miles per minute
    You will cover that distance in 28.571 minutes
    

    Problem 14 (Ruby V)

    A gym has hired you to solve a problem. Most of their clients are fairly new to weightlifting, and don’t know what to put on the barbell for a particular weight. The weights they have made available are in 45, 35, 25, 15, 10 and 5 lbs. increments. Build them a Ruby application that will tell them, for a given weight, what plates to put on each side of the bar. Bear in mind that the bar itself weighs 45 lbs.

    For example, if they want to load 225 lbs. on the bar, they will need to put two 45 lbs. plates on each side of the bar. 45x4 = 180 lbs. Plus 45 lbs. for the barbell itself equals a total of 225 lbs.

    Example output:

    Enter the desired weight (in lbs.) to load on bar:
    225
    For 225 lbs you will need (on each side)
    2 plates of 45
    
    Enter the desired weight (in lbs.) to load on bar:
    335
    For 335 lbs you will need (on each side)
    3 plates of 45
    1 plate of 10
    
    Enter the desired weight (in lbs.) to load on bar:
    235
    For 235 lbs you will need (on each side)
    2 plates of 45
    1 plate of 5
    

    Additional Requirements: • All input weight must be in 5 lbs. increments. So, if the user enters 214 lbs. you should handle that error.

    Example:

    Enter the desired weight (in lbs.) to load on bar:
    214
    Enter a weight in increments of 5 lbs:
    

    • Some weight combinations are impossible to configure due to the fact that the minimum weight you can load is 5 lbs. Therefore a weight combination that is not acceptable should round up to the nearest acceptable weight.

    Example: 
    Enter the desired weight (in lbs.) to load on bar:
    240
    That weight is not possible with our current setup. 
    Let me bump it up 5 lbs to 245
    For 245 lbs you will need (on each side)
    2 plates of 45
    1 plate of 10