Skip to content

Instantly share code, notes, and snippets.

@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 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 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 / 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 / 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 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 / 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 / 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 / Define a method that returns a formatted address
Created December 23, 2012 05:35
Exercise: Define a method that returns a formatted address Create a method called make_address that accepts parameters for the street, city , state, and zip and returns a single address string. For example if we call the method as follows: make_address("717 California St.", "San Francisco", "CA", "94111") The return value would be "You live at 7…
def make_address(street,city,state,zip)
"You live at #{street} in the beautiful city of #{city}, #{state}. Your zip is #{zip}."
end
make_address("6056 Plumas St.", "Reno", "NV","89519")
@kmandreza
kmandreza / gist:3612955
Created September 3, 2012 20:05
Sort an Array
#Given a random array, create a method that sorts the array so the numbers are returned in order.
a= [5, 8, 3, 2, 7, 4, 1, 0, 9, 6, 10, 55]
#Final Answer
def singlesort(a)
(a.size - 1).times do |i|
if a[i] > a[i+1]
x=a[i]
a[i] = a[i+1]