Skip to content

Instantly share code, notes, and snippets.

@nibanez80
nibanez80 / Calculate Letter Grade
Created February 8, 2013 05:52
Exercise: Calculate the letter grade of a series of grades Create a method get_grade that accepts an Array of test scores. Each score in the array should be between 0 and 100, where 100 is the max score. Compute the average score and return the letter grade as a String, i.e., 'A', 'B', 'C', 'D', 'E', or 'F'.
def get_grade(array)
average = (array.inject(:+) / array.size)
if average >= 90; "A"
elsif average >= 80; "B"
elsif average >= 70; "C"
elsif average >= 60; "D"
else average >= 50; "F"
end
end
@nibanez80
nibanez80 / Calculate the mean of an array of numbers
Created February 8, 2013 06:52
Exercise: Calculating the mean of an array of numbers Write a method mean which takes an Array of numbers as its input and returns the average (mean) value as a Float.
def mean(array)
total = array.inject{|sum, x| sum + x}
total / array.length.to_f
end
@nibanez80
nibanez80 / median_of_array.rb
Created February 12, 2013 03:19
Calculate the median of an array
def median(array)
med = array.length / 2
index1 = array[med - 1]
index2 = array[med]
odd = (array.length + 1) / 2
if array.length % 2 == 0
return (index1.to_f + index2.to_f) / 2
else
return array[odd - 1]
@nibanez80
nibanez80 / calcuate_mode_array.rb
Created February 12, 2013 04:27
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array.
def mode(array)
counts = {}
mode = []
array.each do |number|
counts[number] = counts[number].to_i + 1
end
largest_count = counts.invert.sort.to_a.last.first
counts.each_pair do |number, count|
mode << number if count == largest_count
end
@nibanez80
nibanez80 / longest_string.rb
Created February 12, 2013 05:24
Find the longest string in an array
def longest_string(array)
var = array.sort_by { |x| x.length }
var.last
end
@nibanez80
nibanez80 / guessing_game.rb
Last active December 13, 2015 17:48
Exercise: Build a simple guessing game Create a GuessingGame class which is initialized with an integer called answer. Define an instance method GuessingGame#guess which takes an integer called guess as its input. guess should return the symbol :high if the guess is larger than the answer, :correct if the guess is equal to the answer, and :low i…
class GuessingGame
attr_accessor :answer, :high, :low, :correct, :guess, :last_guess, :last_result
def initialize(answer)
@answer = answer
@last_result = nil
@last_guess = nil
end
@nibanez80
nibanez80 / count_between.rb
Created March 7, 2013 02:48
Exercise: Count the numbers in an array between a given range Write a method count_between which takes three arguments as input: An Array of integers An integer lower bound An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array is empty.
def count_between(array, lower_bound, upper_bound)
array.find_all {|x| x >= lower_bound && x <= upper_bound}.count
end
@nibanez80
nibanez80 / factorial.rb
Created March 7, 2013 08:08
Exercise: Implement the factorial function Write a factorial method which takes as its input a non-negative integer and calculates the factorial of that number. The factorial of a number is the product of all integers from 1 up to that number.
def factorial(n)
total = 1
1.upto(n) {|x| total = total * x}
return total
end
@nibanez80
nibanez80 / rpncalculator.rb
Created March 8, 2013 18:43
Exercise: Implement a Reverse Polish notation calculator Create an RPNCalculator class which can evaluate expressions written in Reverse Polish notation. It should have an evaluate instance method which takes as its input a valid RPN expression and returns its evaluation. Your calculator only needs to handle addition, multiplication, and subtrac…
class RPNCalculator
def evaluate(expression)
array = expression.split(' ')
until array.length == 1
array.each_with_index do |item, index|
if item == "+" || item == "*" || item == "-"
array[index] = eval(array[index - 2] + array[index] + array[index - 1]).to_s