Skip to content

Instantly share code, notes, and snippets.

@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
@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: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: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: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 = ""
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 / 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
# Create an RPNCalculator class which can evaluate expressions written in Reverse Polish notation.
# It should have an evaluate instance method which takes as its input a valid RPN expression and returns its evaluation. Your calculator only needs to handle addition, multiplication, and subtraction (not division).
# Operators and numbers should be separated by a single space.
# For example,
# calc = RPNCalculator.new
class Vehicle
attr_reader :status, :wheels
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@status = :stopped
end
@fabianuribe
fabianuribe / scrapper_test.rb
Created July 24, 2013 22:31
Scrapper Incomplete
require 'nokogiri'
doc = Nokogiri::HTML(File.open('post.html'))
def extract_usernames(doc)
doc.search('.comhead > a:first-child').map do |element|
p element.inner_text
end