Skip to content
7 changes: 5 additions & 2 deletions session1/challenge/1_arithmetic.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

# fill out the method below
# then test to see if you did them correctly with
# $ rake 1:1

# Given a number, return 20 less than, that number multiplied by 5
#
#
# arithmetic1(10) # => 30
# arithmeitc1(10.5) # => 32.5
# arithmeitc1(-6) # => -50

def arithmetic1(n)
# the code for this method goes in here
n * 5 - 20
end

arithmetic1(10)
5 changes: 5 additions & 0 deletions session1/challenge/2_arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
# arithmetic2(-6, -7) # => -3.5

def arithmetic2(a, b)
if a < b
return a/2.0
else
return b/2.0
end
end
6 changes: 5 additions & 1 deletion session1/challenge/3_simple_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
# ten_twenty(6) # => 10

def ten_twenty(n)
# your code goes here
if n.even?
return 10
elsif n.odd?
return 20
end
end
19 changes: 16 additions & 3 deletions session1/challenge/4_logic.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A grad student at a local university thinks he has discovered a formula to
# predict what kind of grades a person will get. He says if you own less than
# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C",
# predict what kind of grades a person will get. He says if you own less than
# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C",
# and if you own more than 20 books, you will get a "B".
# He further hypothesizes that if you actually read your books, then you will
# get a full letter grade higher in every case.
Expand All @@ -10,4 +10,17 @@
# grade(15, true) # => "B"

def grade(num_books, reads_books)
end
if num_books < 10 && !reads_books
return "D"
elseif num_books < 10 && reads_books
return "C"
elseif num_books (10..20) && !reads_books
return "C"
elseif num_books (10..20) && reads_books
return "B"
elsif num_books > 20 && !reads_books
return "B"
elsif num_books > 20 &&reads_books
return "A"
end
end
3 changes: 2 additions & 1 deletion session1/challenge/5_string.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Given a string, replace every instance of sad to happy
#
#
# add_more_ruby("The clowns were sad.") # => "The clowns were happy."
# add_more_ruby("The sad dad said sad stuff.") # => "The happy dad said happy stuff."
# add_more_ruby("Sad times are ahead!") # => "Happy times are ahead!"

def add_more_ruby(string)
string.gsub("sad", "happy").gsub("Sad", "Happy")
end
5 changes: 3 additions & 2 deletions session1/challenge/6_string.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# You'll get a string and a boolean.
# When the boolean is true, return a new string containing all the odd characters.
# When the boolean is false, return a new string containing all the even characters.
#
#
# If you have no idea where to begin, remember to check out the cheatsheets for string and logic/control
#
#
# odds_and_evens("abcdefg",true) # => "bdf"
# odds_and_evens("abcdefg",false) # => "aceg"

def odds_and_evens(string, return_odds)
string.chars.select.with_index{|_, i| return_odds ? i.odd? : i.even?}
end
16 changes: 15 additions & 1 deletion session1/challenge/7_string.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# given a string, return the character after every letter "r"
#
#
# pirates_say_arrrrrrrrr("are you really learning Ruby?") # => "eenu"
# pirates_say_arrrrrrrrr("Katy Perry is on the radio!") # => "rya"
# pirates_say_arrrrrrrrr("Pirates say arrrrrrrrr") # => "arrrrrrrr"

def pirates_say_arrrrrrrrr(string)
to_return = ""
add_next = false
string.size.times do |index|
current_char = string[index]
to_return << current_char if add_next
add_next = (current_char == "r" || current_char == "R")
end
to_return
end



# def pirates_say_arrrrrrrrr(string)
# string.gsub(/(?<!r)./i, '')
# end
11 changes: 11 additions & 0 deletions session2/challenge/10_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@
#

class Person

attr_accessor :name, :age

def initialize(name, age)
@name = name
@age = age
end

def birthday
@age += 1
end
end
43 changes: 42 additions & 1 deletion session2/challenge/11_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,50 @@
# Zero bottles of beer on the wall.
#
# Your program should not use ninety-nine output statements!
# Design your program with a class named BeerSong whose initialize method
# Design your program with a class named BeerSong whose initialize method
# receives a parameter indicating the number of bottles of beer initially on the wall.
# If the parameter is less than zero, set the number of bottles to zero. Similarly,
# if the parameter is greater than 99, set the number of beer bottles to 99
# Then make a public method called print_song that outputs all stanzas from the number of bottles of beer down to zero.
# Add any additional methods you find helpful.

class BeerSong
attr_accessor :beers

def initialize(beers)
beers = 0 if beers < 0
beers = 99 if beers > 99
self.beers = beers
end

def print_song
beers.downto 1 do |i|
print_stanza i
end
end

def print_stanza(n)
if n.zero?
String.new
else
puts "#{translate n} #{bottle n} of beer on the wall,"
"#{translate n} #{bottle n} of beer,"
"Take on down, pass it around,"
"#{translate n - 1} #{bottle n - 1} of beer on the wall."
end
end

def bottle(n)
if n == 1 then 'bottle' else 'bottles' end
end

def translate(n)
if 0 <= n && n <= 19
%w(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fiftenn sixteen seventeen eighteen nineteen)
elseif n % 10 == 0
%w(zero ten twenty thirty forty fifty sixty seventy eigthy ninety)[n/10]
else
"#{translate n/10*10}-#{translate n%10}".downcase
end.capitalize
end
end
10 changes: 7 additions & 3 deletions session2/challenge/1_input_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

# Write a program that reads in two integers typed on the keybaord
# and outputs their sum, difference, and product
#
#
# Standard input will be like "9 2\n" and will expect you to print
# "11\n7\n18\n" to standard output.


def sum_difference_product
# your code goes here
end
a , b = gets.split.map { |num| num.to_i }
puts a + b
puts a - b
puts a * b
end
18 changes: 14 additions & 4 deletions session2/challenge/2_input_output_control.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Prompt the user for a number, then read it in and print out "hi" that many times
#
#
# Repeat this process until the user submits "bye", then say "goodbye" and end the program
# HINT: Check out example 2 if you get stuck

Expand All @@ -18,14 +18,24 @@
# remember you can try your program out with $ ruby 2_input_output_control.rb
# and when you think it is correct, you can test it with $ rake 2:2

def prompt
puts "Enter a number or bye"
end

def hi_hi_goodbye
# your code here
prompt
while (line = gets) && (line !~ /bye/)
line.to_i.times { print 'hi ' }
puts prompt
end
puts "Goodbye"
end





# This will just invoke the method if you run this program directly
# This way you can try it out by running "$ ruby 2_input_output_control.rb"
# This way you can try it out by running "$ ruby 2_input_output_control.rb"
# but it will still work for our tests
hi_hi_goodbye if $0 == __FILE__
hi_hi_goodbye if $0 == __FILE__
5 changes: 5 additions & 0 deletions session2/challenge/3_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@

class String
def every_other_char
to_return = ''
each_char.with_index do |char, index|
to_return << char if index.even?
end
to_return
end
end
4 changes: 4 additions & 0 deletions session2/challenge/4_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
# get_squares [25, 4, 9, 6, 50, 16, 5] # => [4, 5]

# This time you will have to define the method, it's called: get_squares

def get_squares(numbers)
numbers.select { |n| numbers.include? n*n }.sort
end
8 changes: 6 additions & 2 deletions session2/challenge/5_array.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Write a function named mod_three which takes an array of numbers,
# Write a function named mod_three which takes an array of numbers,
# and return a new array consisting of their remainder when divided by three.
# Exclude any numbers which are actually dividible by three.
#
#
# EXAMPLES:
# mod_three [0] # => []
# mod_three [1] # => [1]
Expand All @@ -13,3 +13,7 @@
# mod_three [7] # => [1]
#
# mod_three [0,1,2,3,4,5,6,7] # => [1, 2, 1, 2, 1]

def mod_three(numbers)
numbers.select { |number| number % 3 != 0 }.map { |number| % 3 }
end
24 changes: 19 additions & 5 deletions session2/challenge/6_array.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# Write a method named prime_chars? which takes array of strings
# and returns true if the sum of the characters is prime.
#
# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself.
#
# and returns true if the sum of the characters is prime.
#
# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself.
#
# Examples of length three
# prime_chars? ['abc'] # => true
# prime_chars? ['a', 'bc'] # => true
# prime_chars? ['ab', 'c'] # => true
# prime_chars? ['a', 'b', 'c'] # => true
#
#
# Examples of length four
# prime_chars? ['abcd'] # => false
# prime_chars? ['ab', 'cd'] # => false
# prime_chars? ['a', 'bcd'] # => false
# prime_chars? ['a', 'b', 'cd'] # => false

class Integer
def prime?
return false if self < 2
2.upto Math.sqrt(self) do |i|
return false if self % i == 0
end
true
end
end

def prime_chars?(strings)
strings.join.length.prime?
end
15 changes: 14 additions & 1 deletion session2/challenge/7_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
# In order to not have to write an actual language parser, there won't be any punctuation too complex.
# There will be no "'" that is not part of a contraction.
# Assume each of these charactsrs are not to be considered: ! @ $ # % ^ & * ( ) - = _ + [ ] : ; , . / < > ? \ |
#
#
# Examples
# alternate_words("Lorem ipsum dolor sit amet.") # => ["Lorem", "dolor", "amet"]
# alternate_words("Can't we all get along?") # => ["Can't", "all", "along"]
# alternate_words("Elementary, my dear Watson!") # => ["Elementary", "dear"]

def alternate_words(sentence)
# this will get better when we learn regular expressions :)
'!@$#%^&*()-=_+[]:;,./<>?\\|'.split(//).each do |char|
sentence = sentence.gsub(char, ' ')
end
words = sentence.split
solution = []
words.each_with_index do |word, index|
solution << word if index.even?
end
solution
end
9 changes: 8 additions & 1 deletion session2/challenge/8_array.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Given an array of elements, return true if any element shows up three times in a row
#
#
# Examples:
# got_three? [1, 2, 2, 2, 3] # => true
# got_three? ['a', 'a', 'b'] # => false
# got_three? ['a', 'a', 'a'] # => true
# got_three? [1, 2, 1, 1] # => false

def got_three?(elements)
elements.each_cons 3 do |a, b, c|
return true if a == b && b == c
end
false
end
16 changes: 15 additions & 1 deletion session2/challenge/9_input_output_logic_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@
# GRANDMA: HUH?! SPEAK UP, SONNY!
# USER: BYE

def deaf_grandma
def prompt
puts "Say hi to your grandma!"
end

def deaf_grandma
prompt
while line = gets
line.chomp!
break if line == "BYE"
if line == line.upcase && line != ""
puts "NO, NOT SINCE 1938!"
else
puts "HUH?! SPEAK UP, SONNY!"
end
end
end






# This will call your code so you can run it from the terminal.
# But not call it otherwise, so that it will work with our tests.
deaf_grandma if $0 == __FILE__