Ruby Basics Practice
For this assignment, you'll complete reps and practice problem solving with Ruby to get familiar with the language.
Getting Started
- Create a Ruby Repl.it for this lab
Resources
Ruby Documentation
The Ruby documentation is excellent, take advantage of it! Load the following pages in your browser so that you can search for any useful Ruby methods to help you solve the problems (look through the 'methods' column on the left)
Deliverables
Complete all problems below in the ruby repl.it you created and submit using the usual homework submission tracker.
Get Started With a Few Reps
Hello World
- Print "Hello World" to the console
After you have printed Hello World:
adjective = "Big Bad"- Interpolate the
adjectivevariable into the Hello World string using#{}
Expected output:
=> Hello Big Bad World- Save "Hello World" to a variable. Without changing the letters in your code manually, permanently change "Hello World" to all uppercased letters.
Nums Array and Enumerables
With the following array:
nums = [5, 5, 6, 7, 2, 4, 3, 2, 1, 9, 7, 6, 0, 0, 35, 19, 66, 3, 22, 21, 4, 4, 4, 0, 1, 2, 3, 4, 12, 15]
- Use
.uniqto print the array with all duplicate entries removed - Next, use
.pushand.pop,.shift.unshiftand.lengthon the array as you would with javaScript (if you need to add a number, add 5) - Use
.include?to check if the array contains the number 8 - Use
.find_allto find all the numbers greater than 10 - use
.all?to check if all the numbers are greater than 0? - use
any?to check if there are any numbers that are divisible by 8 - use
.countto let you know how many numbers are greater than 4 - use
.each_with_indexto print each item times its index .findthe number that is divisible by 7 and 5 and is greater than 0.find_indexof the number that is divisible by 5 and 7 and is greater than 0- return the
.first3 numbers - return the
.last5 numbers .group_bythe modulo of 3 (% 3)- use
minmaxto return the smallest and largest number - use
.rejectto return all the numbers that are NOT divisible by 3 - use
.selectto return all the numbers divisible by 5
Color Array
With the following array:
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
- Print out a random color.
- Print out the
colorsarray in reverse order. - Print out all of the colors in the array with all capital letters.
Methods
Write a method named find_area that finds the area of a rectangle when given values for width and height
- REMEMBER: In Ruby, the keyword
returnis implied and can be omitted!
def find_area height, width
endWrite a method named multiply_each_by_five that will loop over the given nums array below and print each number multiplied by 5
nums = [5, 5, 6, 7, 2, 4, 3, 2, 1, 9, 7, 6, 0, 0]
def multiply_each_by_five arr
endMethods With a Hash
Use the following given hashes to solve the problems below
# Hashes
book = {
title: 'The Great Gatsby',
author: 'F Scott Fitzgerald',
year: 1925,
price: 10
}
lamp = {
type: 'reading',
brand: 'Ikea',
price: 25
}
table = {
type: 'bed side',
brand: 'Crate & Barrel',
color: 'birch',
price: 50
}- Write a method named
print_pricethat will take in any hash and return the price of the item. - Write a method named
print_item_sumsthat will take in two hashes and will return the sum of the prices for the items in the hashes.
Solving Problems with Ruby
Euler Problem 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Primes
- Write a method called
check_prime?that will test whether a number is Prime. The method will return true if Prime, false if not. - Write another method called
get_primesthat will print all the Primes up to an arbitrary limit. For example, if you invoke your method withget_primes 100, it will print all the Prime numbers up to and including 100. - This method can call on the previous check_prime? method.
Check out the documentation on Ruby's Prime class
Reminders:
A Prime number is a number that is not evenly divisible by another number except 1 and itself. To test whether a number is Prime, you only need to test as far as the square root of that number. This is advisable for optimization and testing large numbers.
Hungry For More?
Pandigital Numbers
A number of length n is 1-to-n pandigital if it makes use of all the digits 1 to n exactly once.
- The number
15234is 1-to-5 pandigital. - The number
333is not 1-to-n pandigital. - The number
0is not 1-to-n pandigital. - The number
10is not 1-to-n pandigital. - The number
987654321is 1-to-9 pandigital.
Write a method that takes an argument n and returns true if the number is 1-to-n pandigital, and false if it is not.
Word Frequency
Write a method that will find the word that appears in a given sentence with the greatest frequency. If there is a tie, either of the words will do as a result.
More?
- Finish the bonus MBTA lab from earlier.
- Solve this problem in both Ruby & JavaScript. You can sign in to Project Euler to submit your answer and check if it's correct.
- Do a kata or two on Codewars