Ruby Warmup Sprint

Shake off the rust with some Ruby challenge problems.

Scroll down...

Content

Resources

Comments

This brief assignment will help you apply your Ruby skills to some more challenging problems. These problems start quite basic to make sure you've got everything down but will get increasingly complicated.

Setup: Ruby

Before we get started, let's make absolutely sure you're working with Ruby 2. It won't affect the outcomes here, but will be relevant later so we might as well get that out of the way.

  1. Go to the terminal and type $ ruby -v. The result should be something along the lines of ruby 2.x.x. If not, you may need to use RVM to update Ruby.

You can use RVM to see what Ruby versions you have installed and to switch between them:

# List available Rubies
$ rvm list

# rvm rubies

#    ruby-2.0.0-p353 [ x86_64 ]
#    ruby-2.0.0-p481 [ x86_64 ]
# =* ruby-2.1.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

# Switch to a new one
$ rvm use 2.0.0

# Set a particular version as the default
$ rvm --default use 2.1.2

# List available versions of Ruby to download
$ rvm list known

# Install a particular version of Ruby
$ rvm install ruby 2.0.0-p247 

# Install the latest version of Ruby
$ rvm install ruby --latest

See the RVM docs for more information.

Getting Started

This assignment will be stored on Github and submitted by Pull Request. To get started:

  1. Fork and clone the project repo on Github.
  2. Add your name to the README file, commit the change, and push to your fork.

Rolling Dice

Write a method roll_dice which returns the result of rolling N 6-sided dice where N is the input to the method (the default should be 1). For example:

> roll_dice(4)
#=> 18          # Rolled 4 dice
> roll_dice(2)
#=> 4           # Rolled 2 dice
> roll_dice
#=> 1           # Rolled 1 die

Rolling Dice Outcomes

Now write a method dice_outcomes which takes the number of dice to roll and the number of times to roll them, then outputs a visual chart of how many times each possible number comes up. For example:

> dice_outcomes(3,100)
3:  ##
4:  #
5:  #
6:  ####
7:  ###
8:  #################
9:  ###########
10: #########
11: ############
12: ##########
13: ##########
14: #########
15: ######
16: ##
17: ##
18: #
#=> {3=>2, 4=>1, 5=>1, 6=>4, 7=>3, 8=>17, 9=>11, 10=>9, 11=>12, 12=>10, 13=>10, 14=>9, 15=>6, 16=>2, 17=>2, 18=>1} 

Fibonacci

Write a method fibonacci which outputs the Fibonacci Sequence members out to the specified number of members. For example:

> fibonacci(7)
#=> [1, 1, 2, 3, 5, 8, 13]

Stock Picker

Write a method stock_picker which takes an array of stock prices (prices on days 0, 1, ...) and outputs the most profitable pair of days on which to first buy the stock and then sell the stock, for example:

> stock_picker([44, 30, 24, 32, 35, 30, 40, 38, 15]) 
#=> [2, 6]

Anagram Testing

Write a method anagrams which returns an array of anagrams that can be made with the specified string. Assume the input is only a single word (e.g. "loot" not "William Shakespeare").

Download enable.txt, a popular Scrabble dictionary, and load it in as your dictionary of choice. If you haven't encountered Ruby's file I/O yet, you can test your method by simply providing an array of known anagrams like those below to check that it works properly.

You can use Dir.pwd to output the current directory. If you can't find the dictionary, double check the directory you're executing your script from within.

Example:

# Note: "pears" doesn't appear in the output
> anagrams("pears")
# => ["APERS","APRES","ASPER","PARES","PARSE","PRASE","PRESA","RAPES","REAPS","SPARE","SPEAR"]

> anagrams("zygote")
#=> []

Finishing Up

  1. When you're finished with all tasks, push your changes up to your fork (aka $ git push origin master).
  2. To submit your assignment, create a pull request from your fork to the main upstream repository.

Solution

The solution will be available here on Github once you've submitted your pull request.

Pull request 300 Octocat 300


Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

There are no additional resources for this lesson just yet!

Sorry, comments aren't active just yet!

Next Lesson: Blocks, Procs and Lambdas