This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GuessingGame | |
attr_reader :answer | |
def initialize(answer) | |
@answer = answer | |
end | |
def guess(guess) | |
last_guess = guess.to_i | |
if last_guess > @answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Die | |
def initialize(labels) | |
@labels = labels | |
end | |
def sides | |
@labels.length | |
end | |
def roll | |
if @labels == [] | |
raise ArgumentError.new("Labels list is empty") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def valid_triangle?(a, b, c) | |
(a + b) > c && (b + c) > a && (a + c) > b | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_grade(average) | |
case average | |
when 90..100 | |
"A" | |
when 80..90 | |
"B" | |
when 70..80 | |
"C" | |
when 60..70 | |
"D" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def leap_year?(year) | |
year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def welcome(address) | |
address.include?("CA") ? "Welcome to California" : "You should move to California" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_between(array, lower_bound, upper_bound) | |
array.count { |i| upper_bound >= i && i >= lower_bound } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(n) | |
(1..n).reduce(1, :*) | |
end |
OlderNewer