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(x,y) | |
| x + y | |
| end | |
| def subtract(x,y) | |
| x - y | |
| end | |
| def multiply(x,y) | |
| x * y |
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 count_between(array, lower_bound, upper_bound) | |
| array.count{|x| x >= lower_bound && x <= upper_bound} | |
| 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 mode(array) | |
| mode = array.inject(Hash.new(0)) { |h,v| h[v] = h[v] + 1; h} | |
| mode.select{ |h,v| v == mode.values.max }.keys | |
| 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 longest_string(array) | |
| if array.empty? | |
| nil | |
| else | |
| long_string = array.group_by(&:size).max.last | |
| long_string[0] | |
| 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 print_triangle(rows) | |
| for j in 1..rows do | |
| for i in 1..j do | |
| print "*" * 1 | |
| end | |
| puts | |
| 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 reverse_words(str) | |
| words = str.split(' ') | |
| reverse_str = [] | |
| words.length.times do |i| | |
| reverse_str[i] = words[i].reverse | |
| end | |
| return reverse_str.join(" ") |
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) | |
| number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse | |
| 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 is_fibonacci?(i) | |
| sum = [0,1] | |
| x = 0 | |
| y = 0 | |
| while sum.max <= i | |
| sum.length.times do |a| | |
| x = sum[a] + sum[a-1] | |
| 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
| class Array | |
| def pad!(min_size, value = nil) | |
| x = min_size - self.count | |
| x.times {self << value} | |
| self | |
| end | |
| def pad(min_size, value = nil) | |
| self.clone.pad!(min_size, value) | |
| 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
| class Sudoku | |
| def initialize(string) | |
| string_of_array = string.split("") | |
| @board = Array.new(9) {string_of_array.shift(9)} | |
| end | |
| def solve! | |
| first_empty = find_empty_cell |
OlderNewer