Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
# HOLE 1
#
# Given a number the function returns “Fizz” if it is a multiple of 3,
# “Buzz” if it is a multiple of 5 and “FizzBuzz” if it is a multiple of
# 15. If the number is not a multiple of 3 or 5 then the number is
# returned as a string.
# Original answer
def fizzbuzz( n )
b,m="Buzz",n%5==0;n%3==0?"Fizz#{b if m}":m ?b:n
end
# Props to Cyrus for this optimization -- <1 instead of ==0
def fizzbuzz2( n )
b,m="Buzz",n%5<1;n%3<1?"Fizz#{b if m}":m ?b:n
end
# HOLE 2
#
# Implement a Caesar Shift Cipher
# Example: caeser("hello",3) => "khoor"
# You should also be able to produce negative shifts.
def caesar( s , h )
h+=26 if h<0;h%=26;s.chars.map{|c|h.times{c.succ!};c[-1..-1]}.join
end
# Based on various solutions and tips -- join with `*`
def caesar( s , h )
h+=26 if h<0;h%=26;s.chars.map{|c|h.times{c.succ!};c[-1..-1]}*""
end
# HOLE 3
#
# Write a simple method that ‘plays’ this game, where the
# player enters their ‘move’ as an argument to the method.
# If the player enters an invalid option then the result
# should be ‘lose’. The computer should choose its move at
# random. The output gives the computer’s ‘move’ and the
# result as a comma-separated string. Example:
#
# play("Rock") => "Rock,Draw"
# play("Paper") => "Rock,Win"
# play("Scissors") => "Rock,Lose"
# play("Soap") => "Paper,Lose"
def play( m )
a,b,c="Rock","Paper","Scissors";d=Hash[[a,b,c].zip [c,a,b]];e=d.keys[rand 3];f=m==e ?"Draw":d[m]==e ?"Win":"Lose";e+","+f
end
# Based on various solutions and tips -- array w/ short syntax
def play2( m )
a,b,c=%w(Rock Paper Scissors);d=Hash[[a,b,c].zip [c,a,b]];e=d.keys[rand 3];f=m==e ?"Draw":d[m]==e ?"Win":"Lose";e+","+f
end
# HOLE 4
#
# Write a method that when given a string and substring, returns the
# number of times the substring occurs in that string (ignoring case).
# Example:
#
# count("Banana","a") => 3
# count("RubySource provides advice, tutorials, commentary, and insight into the Ruby and Rails ecosystem","ruby") => 2
def count( p , s )
p.scan(/#{s}/i).count
end
# Based on comments/solutions from Christian Guenther and Anton Lindqvist -- use `size` instead of `count`
def count2( p , s )
p.scan(/#{s}/i).size
end
# HOLE 5
#
# Write a function that replaces ‘putting your keys in a tin’.
# The argument to the function is an array of arrays that contain
# two objects. The function returns a new array where the pairs
# of objects have been mixed up. An object should not end up with
# it’s original ‘partner’. Example:
#
# swingers([
# ["Homer","Marge"],
# ["Micky","Minnie"],
# ["Fred","Wilma"],
# ["Peter","Lois"],
# ["George","Judy"]
# ])
# => [
# ["Homer","Wilma"],
# ["Micky","Lois"],
# ["Fred","Judy"],
# ["Peter","Marge"],
# ["George","Minnie"]
# ]
def swingers( a )
a.map(&:first).zip a.map(&:last).rotate
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.