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(grade) | |
average = grade.to_i | |
case average | |
when 90..100 | |
"A" | |
when 80...90 | |
"B" | |
when 70...80 | |
"C" |
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) | |
count = 0 | |
array.each do |num| | |
if ((num >= lower_bound) && (num <= upper_bound)) | |
count += 1 | |
end | |
end | |
return count | |
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 super_fizzbuzz(array) | |
new_arr = [] | |
array.each do |num| | |
if ((num % 3 == 0) && (num % 5 == 0)) | |
new_arr << "FizzBuzz" | |
elsif num % 3 == 0 | |
new_arr << "Fizz" | |
elsif num % 5 == 0 | |
new_arr << "Buzz" | |
else |
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 print_triangle(rows) | |
1.upto(rows) { |i| puts "*" * i } | |
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 reverse_words(str) | |
words = str.split(" ") | |
rev = words.reverse_each | |
print rev | |
end | |
---------- | |
def reverse_words(str) | |
words = str.split(" ") | |
word = words.each.to_s | |
print word.reverse! |
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) | |
array.group_by do |e| | |
e | |
end.values.max_by(&:size).first | |
end | |
--------------------------------- | |
def mode(array) | |
array.inject(Hash.new(0)){ |h,i| h[i] += 1; h }.max{ |a,b| a[1] <=> b[1] }.first | |
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 is_fibonacci?(i) | |
n = i * Math.sqrt(5) | |
n1 = Math.sqrt((5*(i**2)+4)) | |
n2 = Math.sqrt((5*(i**2)-4)) | |
if (i == 1 || i == 0) | |
return true | |
elsif (n1 % 1 == 0) || (n2 % 1 == 0) | |
return true | |
else |
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 times_table(rows) | |
1.upto(rows).each do |line| | |
1.upto(rows).each do |num| | |
print "#{line*num} " | |
end | |
print "\n" | |
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
class Die | |
attr_accessor :labels | |
def initialize(labels) | |
unless labels.length > 0 | |
raise ArgumentError.new("Where's your strings?") | |
end | |
@labels = labels | |
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
class GuessingGame | |
def initialize(answer) | |
@answer = answer | |
end | |
def guess(guess) | |
@guess = guess | |
result = nil | |
case guess |
OlderNewer