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 recursive_reverse string | |
return '' if string.empty? | |
string.slice!(-1) + recursive_reverse(string) | |
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
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]+/] | |
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 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) |
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
# 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 |
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
#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 = "" |
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
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 |
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
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 |
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
# 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 |
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 Vehicle | |
attr_reader :status, :wheels | |
def initialize(args) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@status = :stopped | |
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 '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 |
OlderNewer