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(array) | |
| sum=array.inject(0){|sum,i| sum + i} | |
| avg = sum/array.length | |
| if avg >=90 | |
| puts "A" | |
| return | |
| elsif avg >= 80 | |
| puts "B" |
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 times_table(rows) | |
| (1..(rows)).each do |r| | |
| line = "" | |
| (1..(rows)).each{ |c| line += "#{r * c}\t"} | |
| puts line | |
| end | |
| end | |
| times_table(100) |
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 separate_comma(number) | |
| if number < 1000 | |
| puts number | |
| end | |
| begin | |
| parts = number.to_s.split('.') | |
| parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") | |
| parts.join separator | |
| rescue | |
| return number |
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) | |
| results =0 | |
| mean = 0 | |
| array.length.times do |i| | |
| results = results + array[i] | |
| puts mean = results.to_i/ array.length | |
| end | |
| results | |
| mean | |
| 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 factorial(n) | |
| if n <= 0 | |
| return 1 | |
| else | |
| return n * factorial(n-1) | |
| end | |
| end | |
| factorial(56) |
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 reverse_words(str) | |
| new_str= str.split.reverse.join(' ') | |
| return new_str.reverse | |
| end | |
| reverse_words("I cant wait to get home") |
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
| ef print_triangle(rows) | |
| if 1.upto (rows) do |i| | |
| puts "*" * i | |
| end | |
| else | |
| return nil | |
| end | |
| end | |
| print_triangle(22) |
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) | |
| results =0 | |
| array.length.times do |i| | |
| results = results + array[i] | |
| end | |
| results | |
| end | |
| total ([5,6,7,8]) |
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 shortest_string(array) | |
| i=0 | |
| if array.length > i | |
| return (array.min_by {|x| x.length }) | |
| else | |
| return nil | |
| end | |
| end | |
| puts shortest_string(["cat","dog","horse","pig","elephant","rabbit"]) |
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 largest_string(array) | |
| i=0 | |
| if array.length > i | |
| return (array.max_by {|x| x.length }) | |
| else | |
| return nil | |
| end | |
| end | |
| puts largest_string(["cat","dog","horse","pig","elephant","rabbit"]) |