Skip to content

Instantly share code, notes, and snippets.

@fabianuribe
fabianuribe / gist:5901672
Last active Dec 19, 2015
Reverse a string using recursion
View gist:5901672
def recursive_reverse string
return '' if string.empty?
string.slice!(-1) + recursive_reverse(string)
end
View gist:5901803
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 Jul 1, 2013
Recursive RPN Calculator without iteration (inject, each, map, etc. not allowed)
View gist:5902340
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 Dec 19, 2015
Valid triangle in one line
View gist:5902718
# 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 Dec 19, 2015
Number to English Number
View gist:5988845
#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 = ""
View gist:6041795
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 Dec 20, 2015
Sudoku solver version 1
View sudoku.rb
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
View rpn.rb
# 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
View P5: OO Inheritance.rb
class Vehicle
attr_reader :status, :wheels
def initialize(args)
@color = args[:color]
@wheels = args[:wheels]
@status = :stopped
end
View scrapper_test.rb
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