Skip to content

Instantly share code, notes, and snippets.

View tarynsauer's full-sized avatar

Taryn Sauer tarynsauer

  • Madison, WI
View GitHub Profile
@tarynsauer
tarynsauer / gist:5858128
Last active December 18, 2015 22:59
Exercise: Calculating the array mode
def mode(array)
freq = []
modes = []
max_array = []
array.each do |i|
freq << array.count(i)
end
# Combining array and frequencies into a hash
hash = Hash[array.zip(freq.map)]
# Should get max value (frequency) of hash
@tarynsauer
tarynsauer / gist:5875974
Last active December 19, 2015 01:29
Guessing Game
class GuessingGame
attr_reader :answer
def initialize(answer)
@answer = answer
end
def guess(guess)
last_guess = guess.to_i
if last_guess > @answer
class Die
def initialize(labels)
@labels = labels
end
def sides
@labels.length
end
def roll
if @labels == []
raise ArgumentError.new("Labels list is empty")
@tarynsauer
tarynsauer / gist:5893187
Created June 29, 2013 23:55
Triangle side lengths
def valid_triangle?(a, b, c)
(a + b) > c && (b + c) > a && (a + c) > b
end
@tarynsauer
tarynsauer / gist:5893213
Created June 30, 2013 00:05
Calculate a letter grade
def get_grade(average)
case average
when 90..100
"A"
when 80..90
"B"
when 70..80
"C"
when 60..70
"D"
@tarynsauer
tarynsauer / gist:5893240
Created June 30, 2013 00:21
Detecting leap years
def leap_year?(year)
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
end
@tarynsauer
tarynsauer / gist:5893328
Created June 30, 2013 00:58
Detecting simple substrings
def welcome(address)
address.include?("CA") ? "Welcome to California" : "You should move to California"
end
@tarynsauer
tarynsauer / gist:5893389
Created June 30, 2013 01:23
Count the numbers in an array between a given range
def count_between(array, lower_bound, upper_bound)
array.count { |i| upper_bound >= i && i >= lower_bound }
end
@tarynsauer
tarynsauer / gist:5893466
Created June 30, 2013 01:54
Calculating the median of an array of numbers
def median(array)
array.sort
if array.length%2 == 0
(array[array.length/2 - 1] + array[array.length/2])/2.0
else
array[array.length/2]
end
end
@tarynsauer
tarynsauer / gist:5893788
Last active December 19, 2015 03:58
Factorial with reduce enumerator
def factorial(n)
(1..n).reduce(1, :*)
end