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) | |
leng=array.length | |
case leng | |
when leng%2==0 | |
i=leng/2 | |
med=(array[i-1].to_f + array[i].to_f) / 2 | |
return med | |
else | |
med=array[(leng/2)] | |
return med |
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
class Position | |
attr_reader :x, :y | |
@cache_of_positions = {} | |
def self.new_with_memo(x,y) | |
@cache_of_positions[[x,y]] || Position.new(x,y).tap do |instance| | |
@cache_of_positions[[x,y]] = instance | |
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
require 'logger' | |
ActiveRecord::Base.logger = Logger.new(STDOUT) |
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
class SudokuBoard | |
def initialize(board_str) | |
@board = board_str.split("").map { |value| Cell.new(value.to_i) } | |
@groups = [] | |
(generate_rows + generate_cols + generate_grids).each do |group_member_indices_ar| | |
group_cell_members = group_member_indices_ar.map { |index| @board[index] } | |
@groups << Group.new(group_cell_members) | |
end | |
puts to_s |
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
class Integer | |
def in_words | |
if self < 20 | |
{0 => "\b", 1 => "one", 2 => "two", 3 => "three", 4 => "four", 5 => "five", 6 => "six", 7 => "seven", 8 => "eight", 9 => "nine", 10 => "ten", 11 => "eleven", 12 => "twelve", 13 => "thirteen", 14 => "fourteen", 15 => "fifteen", 16 => "sixteen", 17 => "seventeen", 18 => "eighteen", 19 => "nineteen"}[self] | |
elsif self < 100 | |
"#{{20 => "twenty", 30 => "thirty", 40 => "forty", 50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty", 90 => "ninety"}[self - (self % 10)]} #{(self % 10).in_words}" | |
elsif self < 1_000 | |
"#{(self / 100).in_words} hundred #{(self % 100).in_words}" | |
else | |
{10 ** 12 => "trillion", 10 ** 9 => "billion", 10 ** 6 => "million", 10 ** 3 => "thousand"}.each do |number, word| |