This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 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") |
This file contains hidden or 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
| #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] |