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
#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
# 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
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
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 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
# Flatten an Array Recursively | |
# Imagine we have an array that potentially contains other arrays, e.g., | |
# ["bananas", [1,2,3], ["apple", "cheese", [100, 20]], [true], [4.0, 7, 32]] | |
# Write a recursive method flatten that takes as its input an array and returns a fully flattened array. | |
# | |
# For example, using the above array flatten should work thus: | |
# array = ["bananas", [1,2,3], ["apple", "cheese", [100, 20]], [true], [4.0, 7, 32]] |
NewerOlder