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) | |
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 |
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 add(num1, num2) | |
num1 + num2 | |
end | |
def subtract(num1, num2) | |
num1 - num2 | |
end | |
def multiply(num1, num2) | |
num1 * num2 |
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(num) | |
if num >= 90 | |
return "A" | |
end | |
if num >= 80 && num <= 89 | |
return "B" | |
end | |
if num >= 70 && num <= 79 |
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) | |
if address.match("CA") | |
return "Welcome to California" | |
else | |
return "You should move to California" | |
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 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" |
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 total(array) | |
total = 0 | |
array.length.times do |i| | |
total = total + array[i] | |
end | |
total | |
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 mean(array) | |
(array.inject { |sum,x| sum + x }).to_f / array.length | |
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) #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 |
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) | |
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 |
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
# 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) |