Skip to content

Instantly share code, notes, and snippets.

@kmandreza
kmandreza / Triangle
Created December 28, 2012 04:28
Write a method valid_triangle? which takes as its input three non-negative numbers. It should return true if the three numbers could form the side lengths of a triangle and false otherwise. The arguments don't correspond to specific sides. Don't worry about handling negative inputs — garbage in, garbage out. For example, valid_triangle?(0,0,0) #…
def valid_triangle?(a,b,c)
ab = a == b
bc = b == c
ac = a == c
if a >= b + c || b >= a + c || c >= a + b || a == 0 || b == 0 || c == 0
return false
elsif ab && bc && ac
return true
elsif ab || bc || ac
@kmandreza
kmandreza / Defining basic arithmetical methods
Created December 28, 2012 04:40
Define four methods which correspond to the four basic arithmetic operations: add subtract multiply divide They should accept either integers or floating point numbers as input. divide should perform floating point division. For example, add(10,2) # => 12 subtract(10,2) # => 8 multiply(10,2) # => 20 divide(10,2) # => 5.0 (*not* 5)
def add(num1, num2)
num1 + num2
end
def subtract(num1, num2)
num1 - num2
end
def multiply(num1, num2)
num1 * num2
@kmandreza
kmandreza / Calculate a letter grade
Created December 28, 2012 19:13
Create a method get_grade that accepts an average in the class and returns the letter grade as a String. It should only return one of 'A', 'B', 'C', etc. Don't worry about + and - grades. For example, get_grade(89) # => returns "B", *not* "B+" get_grade(70) # => returns "C"
def get_grade(num)
if num >= 90
return "A"
end
if num >= 80 && num <= 89
return "B"
end
if num >= 70 && num <= 79
@kmandreza
kmandreza / Detecting simple substrings
Created December 28, 2012 23:19
Create a method called welcome that accepts an address as a single string. It returns "Welcome to California" if the address includes "CA" and "You should move to California" otherwise.
def welcome(address)
if address.match("CA")
return "Welcome to California"
else
return "You should move to California"
end
end
@kmandreza
kmandreza / Calculate the letter grade of a series of grades
Created December 30, 2012 19:20
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'. For example, # How studious! get_grade([100, 100, 100]) # => 'A'
def get_grade(test_scores)
x = test_scores.inject{|sum,x| sum + x } #sum of all test scores
y = x/test_scores.length #divide by number of test scores
if y >= 90
return "A"
elsif y >=80
return "B"
elsif y>= 70
return "C"
@kmandreza
kmandreza / Calculating the array total
Created December 31, 2012 20:22
Write a method total which takes an Array of numbers as its input and returns their total (sum). For example, total([1,2,3]) # => 6 total([4.5, 0, -1]) # => 3.5 total([-100, 100]) # => 0
def total(array)
total = 0
array.length.times do |i|
total = total + array[i]
end
total
end
@kmandreza
kmandreza / Calculating the mean of an array of numbers
Created December 31, 2012 23:55
Write a method mean which takes an Array of numbers as its input and returns the average (mean) value as a Float. For example, mean([1,2,3]) # => 2.0 mean([4.5, 0, -1]) # => 1.1666666666666667 mean([-100, 100]) # => 0.0
def mean(array)
(array.inject { |sum,x| sum + x }).to_f / array.length
end
@kmandreza
kmandreza / Calculating the median of an array of numbers
Created January 1, 2013 19:48
Write a method median which takes an Array of numbers as its input and returns the median value. You might want to look up the definition of "median."" For example, median([1,2,3]) # => 2 median([4.5, 0, -1]) # => 0 median([-100, 100]) # => 0.0
def median(array) #To find the Median, place the numbers you are given in value order and find the middle number.
sorted = array.sort
if array.length % 2 != 0
array[array.length/2]
else
((array[array.length/2] - array[(array.length/2)-1]).to_f / 2) + array[(array.length/2)-1]
end
end
@kmandreza
kmandreza / Calculating the array mode
Created January 2, 2013 03:30
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. For example, mode([1,2,3,3]) # => [3] mode([4.5, 0, 0]) # => [0] mode([1.5, -1, 1, 1.5]) # => [1.5] mode([1,1,2,2]) # => [1,2] mode([1,2,3]) # => [1,2,3], b…
def mode(array)
count = Hash.new(0)
array.each { |element| count[element] += 1 } #count how many times an element[s] appears the most
# element of the array => number of times element occured in an array
# ex. {5 => 2, 6 =>2, 7 =>1}
max = count.values.max
count.keep_if { |key, val| val == max}
# count is {5 => 2, 6 =>2}
count.keys #return an array of those elements
end
@kmandreza
kmandreza / Find the largest integer in an array
Created January 3, 2013 04:33
Write a method largest_integer which takes as its input an Array of integers and returns the largest integer in the Array. For example: largest_integer([-10, 0, 10]) # => 10 largest_integer([-10, -20, -30]) # => -10
# largest_integer is a method that takes an array of integers as its input
# and returns the largest integer in the array
#
# +array+ is an array of integers
# largest_integer(array) should return the largest integer in +array+
#
# If +array+ is empty the method should return nil
#using sort will go through the array a bunch of times, which is less efficient
def largest_integer(array)