public
Last active

  • Download Gist
hole1_fizzbuzz.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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
hole2_caesar.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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
hole3_rps.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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
hole4_string_counter.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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
hole5_swingers.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.