Skip to content

Instantly share code, notes, and snippets.

@fabianuribe
fabianuribe / sudoku.rb
Last active December 20, 2015 00:59
Sudoku solver version 1
sudoku_string = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
sudoku_array = sudoku_string.split('')
rows_array = Array.new(9){ sudoku_array.shift(9) }
rows_array.each{|row| p row}
box_rows_array = []
# p rows_array
sudoku_string = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
sudoku_array = sudoku_string.split('')
rows_array = Array.new(9){ sudoku_array.shift(9) }
rows_array.each{|row| p row}
box_rows_array = []
# p rows_array
@fabianuribe
fabianuribe / gist:5988845
Last active December 19, 2015 17:08
Number to English Number
#Returns a number in english language
def english_number (number)
raise ArgumentError.new("Argument must be a positive integer") if !(number.is_a? Integer) || number < 0
return 'zero' if number == 0
result = ""
@fabianuribe
fabianuribe / gist:5902718
Last active December 19, 2015 05:09
Valid triangle in one line
# Triangle side lengths
# 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) # => false, because a triangle can't have 0-length sides
@fabianuribe
fabianuribe / gist:5902340
Created July 1, 2013 16:26
Recursive RPN Calculator without iteration (inject, each, map, etc. not allowed)
def evaluate(string, answer=[])
return answer[0] if string.empty?
expressions = string.split(' ')
obj = expressions.shift
if obj.to_s[/\d+/]
answer << obj.to_f
else ["*","+","-","/"].include?(obj)
operands = answer.pop(2)
@fabianuribe
fabianuribe / gist:5901803
Created July 1, 2013 15:23
RPN Calculator
class RPNCalculator
def initialize
end
def evaluate(rpn)
expressions = rpn.split(' ')
ans = []
expressions.each do |obj| # example: (5 1 2 + 4 * + 3 -)
if obj.to_s[/[0-9]+/]
@fabianuribe
fabianuribe / gist:5901672
Last active December 19, 2015 05:08
Reverse a string using recursion
def recursive_reverse string
return '' if string.empty?
string.slice!(-1) + recursive_reverse(string)
end