mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
Minor fixes
This commit is contained in:
parent
f3c42a5829
commit
8bb781f790
59 changed files with 471 additions and 501 deletions
0
360
Normal file
0
360
Normal file
|
@ -37,7 +37,7 @@
|
|||
# @return {String[]}
|
||||
def generate_parenthesis(n)
|
||||
parenthesis = []
|
||||
backtrack(parenthesis, "", 0, 0, n)
|
||||
backtrack(parenthesis, '', 0, 0, n)
|
||||
parenthesis
|
||||
end
|
||||
|
||||
|
@ -47,13 +47,9 @@ def backtrack(parenthesis, curr, open, close, max)
|
|||
return
|
||||
end
|
||||
|
||||
if open < max
|
||||
backtrack(parenthesis, curr + "(", open + 1, close, max)
|
||||
end
|
||||
backtrack(parenthesis, curr + '(', open + 1, close, max) if open < max
|
||||
|
||||
if close < open
|
||||
backtrack(parenthesis, curr + ")", open, close + 1, max)
|
||||
end
|
||||
backtrack(parenthesis, curr + ')', open, close + 1, max) if close < open
|
||||
end
|
||||
|
||||
n = 3
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
def binary_and(x, y)
|
||||
raise 'Input must only contain positive integers' if x < 0 or y < 0
|
||||
raise 'Input must only contain positive integers' if (x < 0) || (y < 0)
|
||||
|
||||
"0b" + (x & y).to_s(2)
|
||||
'0b' + (x & y).to_s(2)
|
||||
end
|
||||
|
||||
begin
|
||||
binary_and(-1, 0)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# Input must only contain positive integers
|
||||
|
|
|
@ -8,7 +8,7 @@ end
|
|||
|
||||
begin
|
||||
binary_count_setbits(-1)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# Input must be a positive integer
|
||||
|
|
|
@ -5,7 +5,7 @@ def binary_count_trailing_zeroes(x)
|
|||
|
||||
count = 0
|
||||
binary.chars.reverse_each do |char|
|
||||
break if char == "1"
|
||||
break if char == '1'
|
||||
|
||||
count += 1
|
||||
end
|
||||
|
@ -15,7 +15,7 @@ end
|
|||
|
||||
begin
|
||||
binary_count_trailing_zeroes(-1)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# Input must be a positive integer
|
||||
|
@ -32,5 +32,5 @@ puts binary_count_trailing_zeroes(1024)
|
|||
puts binary_count_trailing_zeroes(54)
|
||||
# 1
|
||||
|
||||
puts binary_count_trailing_zeroes(121024)
|
||||
puts binary_count_trailing_zeroes(121_024)
|
||||
# 6
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
def binary_or(x, y)
|
||||
raise 'Input must only contain positive integers' if x < 0 or y < 0
|
||||
raise 'Input must only contain positive integers' if (x < 0) || (y < 0)
|
||||
|
||||
"0b" + (x | y).to_s(2)
|
||||
'0b' + (x | y).to_s(2)
|
||||
end
|
||||
|
||||
begin
|
||||
binary_or(-1, 0)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# Input must only contain positive integers
|
||||
|
@ -21,4 +21,3 @@ puts binary_or(0, 1023)
|
|||
# 0b1111111111
|
||||
puts binary_or(16, 58)
|
||||
# 0b110010
|
||||
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
def binary_xor(x, y)
|
||||
raise 'Input must only contain positive integers' if x < 0 or y < 0
|
||||
raise 'Input must only contain positive integers' if (x < 0) || (y < 0)
|
||||
|
||||
binary_x = x.to_s(2)
|
||||
binary_y = y.to_s(2)
|
||||
|
||||
if binary_x.length > binary_y.length
|
||||
prefix = "0" * (binary_x.length - binary_y.length)
|
||||
prefix = '0' * (binary_x.length - binary_y.length)
|
||||
binary_y = prefix + binary_y
|
||||
elsif binary_y.length > binary_x.length
|
||||
prefix = "0" * (binary_y.length - binary_x.length)
|
||||
prefix = '0' * (binary_y.length - binary_x.length)
|
||||
binary_x = prefix + binary_x
|
||||
end
|
||||
result = "0b"
|
||||
result = '0b'
|
||||
binary_x.each_char.with_index do |x_char, i|
|
||||
y_char = binary_y[i]
|
||||
|
||||
if (x_char == "1" && y_char != "1") || (x_char != "1" && y_char == "1")
|
||||
result += "1"
|
||||
result += if (x_char == '1' && y_char != '1') || (x_char != '1' && y_char == '1')
|
||||
'1'
|
||||
else
|
||||
result += "0"
|
||||
'0'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -27,7 +27,7 @@ end
|
|||
|
||||
begin
|
||||
binary_xor(-1, 0)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# Input must only contain positive integers
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
def set_bit(x, position)
|
||||
raise "position must be >= 0" if position < 0
|
||||
raise 'position must be >= 0' if position < 0
|
||||
|
||||
x | (1 << position)
|
||||
end
|
||||
|
@ -18,14 +18,13 @@ puts set_bit(8, 4)
|
|||
|
||||
begin
|
||||
puts set_bit(8, -4)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# position must be >= 0
|
||||
|
||||
|
||||
def clear_bit(x, position)
|
||||
raise "position must be > 0" if position < 0
|
||||
raise 'position must be > 0' if position < 0
|
||||
|
||||
x & ~(1 << position)
|
||||
end
|
||||
|
@ -44,13 +43,13 @@ puts clear_bit(24, 4)
|
|||
|
||||
begin
|
||||
puts clear_bit(0, -4)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# position must be > 0
|
||||
|
||||
def flip_bit(x, position)
|
||||
raise "position must be > 0" if position < 0
|
||||
raise 'position must be > 0' if position < 0
|
||||
|
||||
x ^ (1 << position)
|
||||
end
|
||||
|
@ -69,13 +68,13 @@ puts flip_bit(24, 4)
|
|||
|
||||
begin
|
||||
puts flip_bit(0, -4)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# position must be > 0
|
||||
|
||||
def is_bit_set(x, position)
|
||||
raise "position must be > 0" if position < 0
|
||||
raise 'position must be > 0' if position < 0
|
||||
|
||||
((x >> position) & 1) == 1
|
||||
end
|
||||
|
@ -94,7 +93,7 @@ puts is_bit_set(24, 4)
|
|||
|
||||
begin
|
||||
puts is_bit_set(0, -4)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
# position must be > 0
|
||||
|
|
|
@ -5,48 +5,48 @@ module TemperatureConversion
|
|||
def self.celsius_to_kelvin(celsius_input)
|
||||
kelvin_output = (celsius_input + 273.15).round(2)
|
||||
puts "#{celsius_input}°C = #{kelvin_output}K"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# kelvin -> celsius = vale of kelvin - 273.15 => °C
|
||||
def self.kelvin_to_celsius(kelvin_input)
|
||||
celsius_output = (kelvin_input - 273.15).round(2)
|
||||
puts "#{kelvin_input}K = #{celsius_output}°C"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# celsius -> fahrenheit = (value of celsius * 9 / 5) + 32 => °F
|
||||
def self.celsius_to_fahrenheit(celsius_input)
|
||||
fahrenheit_output = ((celsius_input * 9 / 5) + 32).round(2)
|
||||
puts "#{celsius_input}°C = #{fahrenheit_output}°F"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# fahrenheit -> celsius = (value of fahrenheit - 32) * 5 / 9 => °C
|
||||
def self.fahrenheit_to_celsius(fahrenheit_input)
|
||||
celsius_output = ((fahrenheit_input - 32) * 5 / 9).round(2)
|
||||
puts "#{fahrenheit_input}°F = #{celsius_output}°C"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# fahrenheit -> kelvin = [(value of fahrenheit - 32) * 5 / 9] + 273.15 => K
|
||||
def self.fahrenheit_to_kelvin(fahrenheit_input)
|
||||
kelvin_output = ((fahrenheit_input - 32) * 5 / 9).round(2).round(2)
|
||||
puts "#{fahrenheit_input}°F = #{kelvin_output}K"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# kelvin -> fahrenheit = [(value of kelvin - 32) * 5 / 9] + 273.15 => K
|
||||
def self.kelvin_to_fahrenheit(kelvin_input)
|
||||
fahrenheit_output = (((kelvin_input - 273.15) * 9 / 5) + 32).round(2).round(2)
|
||||
puts "#{kelvin_input}K = #{fahrenheit_output}°F"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -55,18 +55,18 @@ TemperatureConversion.celsius_to_kelvin(20)
|
|||
TemperatureConversion.kelvin_to_celsius(20)
|
||||
|
||||
# Invalid input
|
||||
TemperatureConversion.kelvin_to_celsius("a")
|
||||
TemperatureConversion.kelvin_to_celsius('a')
|
||||
|
||||
# celsius <-> fahrenheit
|
||||
TemperatureConversion.celsius_to_fahrenheit(-20)
|
||||
TemperatureConversion.fahrenheit_to_celsius(68)
|
||||
|
||||
# Invalid input
|
||||
TemperatureConversion.celsius_to_fahrenheit("abc")
|
||||
TemperatureConversion.celsius_to_fahrenheit('abc')
|
||||
|
||||
# fahrenheit <-> kelvin
|
||||
TemperatureConversion.fahrenheit_to_kelvin(60)
|
||||
TemperatureConversion.kelvin_to_fahrenheit(-60)
|
||||
|
||||
# Invalid input
|
||||
TemperatureConversion.fahrenheit_to_kelvin("60")
|
||||
TemperatureConversion.fahrenheit_to_kelvin('60')
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
module WeightConversion
|
||||
# Kilogram -> Gram = (kilogram_value * 1000) grams
|
||||
def self.kilogram_to_gram(kilogram_input)
|
||||
raise StandardError unless Integer === kilogram_input
|
||||
raise StandardError unless kilogram_input.is_a?(Integer)
|
||||
|
||||
gram = kilogram_input * 1000
|
||||
|
||||
|
@ -40,7 +40,7 @@ module WeightConversion
|
|||
|
||||
# Pound -> Kilogram = (pound_input / 2.205) kg
|
||||
def self.pound_to_kilogram(pound_input)
|
||||
raise StandardError unless Integer === pound_input
|
||||
raise StandardError unless pound_input.is_a?(Integer)
|
||||
|
||||
kilogram = (pound_input / 2.205).round(2)
|
||||
|
||||
|
@ -70,37 +70,37 @@ puts WeightConversion.pound_to_kilogram(100)
|
|||
#
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("a")
|
||||
puts WeightConversion.kilogram_to_gram('a')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("3000")
|
||||
puts WeightConversion.kilogram_to_gram('3000')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("16")
|
||||
puts WeightConversion.kilogram_to_gram('16')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("x ")
|
||||
puts WeightConversion.kilogram_to_gram('x ')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("weight")
|
||||
puts WeightConversion.kilogram_to_gram('weight')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
begin
|
||||
puts WeightConversion.kilogram_to_gram("100")
|
||||
puts WeightConversion.kilogram_to_gram('100')
|
||||
rescue StandardError
|
||||
puts "Error: Please provide number only!"
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
|
|
@ -20,9 +20,7 @@
|
|||
# Time complexity: O(n)
|
||||
#
|
||||
def add_digits(num)
|
||||
if num.to_s.length < 2
|
||||
return num
|
||||
end
|
||||
return num if num.to_s.length < 2
|
||||
|
||||
digits_to_sum = num.to_s.split('')
|
||||
sum = 0
|
||||
|
|
|
@ -19,9 +19,8 @@ def num_identical_pairs(nums)
|
|||
target = num
|
||||
nums.each_with_index do |num, j|
|
||||
next if i >= j
|
||||
if num == target
|
||||
count += 1
|
||||
end
|
||||
|
||||
count += 1 if num == target
|
||||
end
|
||||
end
|
||||
count
|
||||
|
|
|
@ -25,6 +25,7 @@ def intersect(arr1, arr2)
|
|||
shorter.each do |matcher|
|
||||
longer.each do |number|
|
||||
next if number != matcher
|
||||
|
||||
result.push(number)
|
||||
break
|
||||
end
|
||||
|
|
|
@ -37,13 +37,13 @@ def remove_elements(nums, val)
|
|||
result_length = nums.length
|
||||
shift_length = 0
|
||||
nums.each_with_index do |num, i|
|
||||
if num == val
|
||||
next unless num == val
|
||||
|
||||
nums.delete_at(i)
|
||||
nums.unshift('removed')
|
||||
result_length -= 1
|
||||
shift_length += 1
|
||||
end
|
||||
end
|
||||
nums.shift(shift_length)
|
||||
result_length
|
||||
end
|
||||
|
|
|
@ -45,7 +45,7 @@ def find_richest_customer_wealth(accounts)
|
|||
summed_accounts.push(summed)
|
||||
end
|
||||
|
||||
summed_accounts.sort.pop()
|
||||
summed_accounts.sort.pop
|
||||
end
|
||||
|
||||
puts find_richest_customer_wealth([[1, 2, 3], [3, 2, 1]])
|
||||
|
|
|
@ -28,16 +28,14 @@
|
|||
# Time Complexity: O(n)
|
||||
# Space Complexity: O(1)
|
||||
|
||||
|
||||
def almost_palindrome_checker(string)
|
||||
p1 = 0
|
||||
p2 = string.length - 1
|
||||
array = string.split('')
|
||||
|
||||
while p1 < p2
|
||||
if array[p1] != array[p2]
|
||||
return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2)
|
||||
end
|
||||
return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2) if array[p1] != array[p2]
|
||||
|
||||
p1 += 1
|
||||
p2 -= 1
|
||||
end
|
||||
|
@ -47,9 +45,8 @@ end
|
|||
|
||||
def palindrome_checker(array, p1, p2)
|
||||
while p1 < p2
|
||||
if array[p1] != array[p2]
|
||||
return false
|
||||
end
|
||||
return false if array[p1] != array[p2]
|
||||
|
||||
p1 += 1
|
||||
p2 -= 1
|
||||
end
|
||||
|
|
|
@ -36,17 +36,15 @@ def find_jewels(jewels, stones)
|
|||
result = 0
|
||||
jewels_array.each do |jewel|
|
||||
stones_array.each do |stone|
|
||||
if jewel == stone
|
||||
result += 1
|
||||
end
|
||||
result += 1 if jewel == stone
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
puts find_jewels("aA", "aAAbbbb")
|
||||
puts find_jewels('aA', 'aAAbbbb')
|
||||
# => 3
|
||||
puts find_jewels("z", "ZZ")
|
||||
puts find_jewels('z', 'ZZ')
|
||||
# => 0
|
||||
|
||||
#
|
||||
|
@ -80,7 +78,7 @@ def find_jewels(jewels, stones)
|
|||
result
|
||||
end
|
||||
|
||||
puts find_jewels("aA", "aAAbbbb")
|
||||
puts find_jewels('aA', 'aAAbbbb')
|
||||
# => 3
|
||||
puts find_jewels("z", "ZZ")
|
||||
puts find_jewels('z', 'ZZ')
|
||||
# => 0
|
|
@ -26,9 +26,7 @@ def remove_vowels(s)
|
|||
start_array = s.split('')
|
||||
|
||||
start_array.each do |letter|
|
||||
if letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u'
|
||||
result_array.push(letter)
|
||||
end
|
||||
result_array.push(letter) if letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u'
|
||||
end
|
||||
|
||||
result_array.join('')
|
||||
|
|
|
@ -20,7 +20,8 @@ def is_valid_sudoku(board)
|
|||
board.each do |row|
|
||||
row_hash = Hash.new(0)
|
||||
row.each do |num|
|
||||
next if num == "."
|
||||
next if num == '.'
|
||||
|
||||
row_hash[num] += 1
|
||||
end
|
||||
rows << row_hash
|
||||
|
@ -28,10 +29,8 @@ def is_valid_sudoku(board)
|
|||
|
||||
# check each row hash for duplicated value
|
||||
rows.each do |row|
|
||||
row.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
row.each do |_k, v|
|
||||
return false if v > 1
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -39,7 +38,8 @@ def is_valid_sudoku(board)
|
|||
(0..8).each do |i|
|
||||
column_hash = Hash.new(0)
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
column_hash[row[i]] += 1
|
||||
columns << column_hash
|
||||
end
|
||||
|
@ -47,10 +47,8 @@ def is_valid_sudoku(board)
|
|||
|
||||
# check each column hash for duplicated value
|
||||
columns.each do |column|
|
||||
column.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
column.each do |_k, v|
|
||||
return false if v > 1
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -58,17 +56,20 @@ def is_valid_sudoku(board)
|
|||
(0..2).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
|
@ -76,17 +77,20 @@ def is_valid_sudoku(board)
|
|||
(3..5).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
|
@ -94,17 +98,20 @@ def is_valid_sudoku(board)
|
|||
(6..8).last(3).each do |i|
|
||||
grid_hash = Hash.new(0)
|
||||
board.first(3).each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board[3..5].each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
board.each do |row|
|
||||
next if row[i] == "."
|
||||
next if row[i] == '.'
|
||||
|
||||
grid_hash[row[i]] += 1
|
||||
grids << grid_hash
|
||||
end
|
||||
|
@ -112,63 +119,61 @@ def is_valid_sudoku(board)
|
|||
|
||||
# check each grid hash for duplicated value
|
||||
grids.each do |grid|
|
||||
grid.each do |k, v|
|
||||
if v > 1
|
||||
return false
|
||||
end
|
||||
grid.each do |_k, v|
|
||||
return false if v > 1
|
||||
end
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
board = [["5","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['5', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => true
|
||||
|
||||
board = [["8","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in column
|
||||
|
||||
board = [["8","3",".",".","7",".","3",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '3', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in row
|
||||
|
||||
board = [["8","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
[".",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['.', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in 3x3 grid
|
||||
|
@ -181,6 +186,7 @@ require 'set'
|
|||
def is_valid_sudoku(board)
|
||||
return false unless check_rows(board)
|
||||
return false unless check_cols(board)
|
||||
|
||||
row = -3
|
||||
while (row += 3) < 9
|
||||
col = - 3
|
||||
|
@ -198,8 +204,9 @@ def check_grid(board, start_point)
|
|||
ss = Set.new
|
||||
(row..(row + 2)).each do |cur_row|
|
||||
(col..(col + 2)).each do |cur_col|
|
||||
next if board[cur_row][cur_col] == "."
|
||||
next if board[cur_row][cur_col] == '.'
|
||||
return false if ss.member?(board[cur_row][cur_col])
|
||||
|
||||
ss.add board[cur_row][cur_col]
|
||||
end
|
||||
end
|
||||
|
@ -209,8 +216,9 @@ end
|
|||
def check_col(board, col)
|
||||
ss = Set.new
|
||||
(0..8).each do |row|
|
||||
next if board[row][col] == "."
|
||||
next if board[row][col] == '.'
|
||||
return false if ss.member?(board[row][col])
|
||||
|
||||
ss.add board[row][col]
|
||||
end
|
||||
true
|
||||
|
@ -219,8 +227,9 @@ end
|
|||
def check_row(board, row)
|
||||
ss = Set.new
|
||||
(0..8).each do |col|
|
||||
next if board[row][col] == "."
|
||||
next if board[row][col] == '.'
|
||||
return false if ss.member?(board[row][col])
|
||||
|
||||
ss.add board[row][col]
|
||||
end
|
||||
true
|
||||
|
@ -240,53 +249,53 @@ def check_cols(board)
|
|||
true
|
||||
end
|
||||
|
||||
board = [["5","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['5', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => true
|
||||
|
||||
board = [["8","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in column
|
||||
|
||||
board = [["8","3",".",".","7",".","3",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
["8",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '3', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in row
|
||||
|
||||
board = [["8","3",".",".","7",".",".",".","."],
|
||||
["6",".",".","1","9","5",".",".","."],
|
||||
[".","9","8",".",".",".",".","6","."],
|
||||
[".",".",".",".","6",".",".",".","3"],
|
||||
["4",".",".","8",".","3",".",".","1"],
|
||||
["7",".",".",".","2",".",".",".","6"],
|
||||
[".","6",".",".",".",".","2","8","."],
|
||||
[".",".",".","4","1","9",".",".","5"],
|
||||
[".",".",".",".","8",".",".","7","9"]]
|
||||
board = [['8', '3', '.', '.', '7', '.', '.', '.', '.'],
|
||||
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
|
||||
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
|
||||
['.', '.', '.', '.', '6', '.', '.', '.', '3'],
|
||||
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
|
||||
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
|
||||
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
|
||||
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
|
||||
['.', '.', '.', '.', '8', '.', '.', '7', '9']]
|
||||
print(is_valid_sudoku(board))
|
||||
# => false
|
||||
# explanation: duplicated value in 3x3 grid
|
|
@ -71,9 +71,7 @@ def two_sum(nums, target)
|
|||
target_difference = target - num
|
||||
|
||||
nums.each_with_index do |num, j|
|
||||
if i != j && num == target_difference
|
||||
return [i, j]
|
||||
end
|
||||
return [i, j] if i != j && num == target_difference
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,7 +38,7 @@ def is_anagram(s, t)
|
|||
counter[t[i]] -= 1
|
||||
end
|
||||
|
||||
counter.each do |k, v|
|
||||
counter.each do |_k, v|
|
||||
return false unless v == 0
|
||||
end
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ def arrays_intersection(arr1, arr2, arr3)
|
|||
add_to_hash(arr2, hash)
|
||||
add_to_hash(arr3, hash)
|
||||
|
||||
hash.select { |key, value| value == 3 }.keys
|
||||
hash.select { |_key, value| value == 3 }.keys
|
||||
end
|
||||
|
||||
def add_to_hash(arr, hash)
|
||||
|
|
|
@ -44,8 +44,8 @@ def common_characters(arr)
|
|||
result
|
||||
end
|
||||
|
||||
puts common_characters(["bella","label","roller"])
|
||||
puts common_characters(%w[bella label roller])
|
||||
# => ["e","l","l"]
|
||||
|
||||
puts common_characters(["cool","lock","cook"])
|
||||
puts common_characters(%w[cool lock cook])
|
||||
# => ["c","o"]
|
|
@ -45,20 +45,20 @@ def isomorphic_strings_check(s, t)
|
|||
else
|
||||
# return false if char2 is already mapped to a different character
|
||||
return false if set.include?(char2)
|
||||
|
||||
# checks passed: add new character map and track that char2 has been mapped
|
||||
map[char1] = char2
|
||||
set << char2
|
||||
end
|
||||
|
||||
end
|
||||
return true
|
||||
true
|
||||
end
|
||||
|
||||
puts isomorphic_strings_check("egg", "add")
|
||||
puts isomorphic_strings_check('egg', 'add')
|
||||
# => true
|
||||
|
||||
puts isomorphic_strings_check("foo", "bar")
|
||||
puts isomorphic_strings_check('foo', 'bar')
|
||||
# => false
|
||||
|
||||
puts isomorphic_strings_check("paper", "title")
|
||||
puts isomorphic_strings_check('paper', 'title')
|
||||
# => true
|
||||
|
|
|
@ -42,10 +42,8 @@ def find_richest_customer_wealth(accounts)
|
|||
end
|
||||
|
||||
highest_value = 0
|
||||
result_hash.each do |k, v|
|
||||
if v > highest_value
|
||||
highest_value = v
|
||||
end
|
||||
result_hash.each do |_k, v|
|
||||
highest_value = v if v > highest_value
|
||||
end
|
||||
|
||||
highest_value
|
||||
|
|
|
@ -48,9 +48,7 @@ def two_sum(nums, target)
|
|||
nums.each_with_index do |num, i|
|
||||
difference_target = target - num
|
||||
|
||||
if hash[difference_target] && hash[difference_target] != i
|
||||
return [i, hash[difference_target]]
|
||||
end
|
||||
return [i, hash[difference_target]] if hash[difference_target] && hash[difference_target] != i
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
# Time Complexitiy: O(n)
|
||||
|
||||
def find_uncommon_words(strA, strB)
|
||||
array = strA.concat(" ", strB).split(" ")
|
||||
array = strA.concat(' ', strB).split(' ')
|
||||
hash = Hash.new(0)
|
||||
result = []
|
||||
|
||||
|
@ -33,16 +33,14 @@ def find_uncommon_words(strA, strB)
|
|||
end
|
||||
|
||||
hash.each do |k, v|
|
||||
if v < 2
|
||||
result.push(k)
|
||||
end
|
||||
result.push(k) if v < 2
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
puts find_uncommon_words("this apple is sweet", "this apple is sour")
|
||||
puts find_uncommon_words('this apple is sweet', 'this apple is sour')
|
||||
# => ["sweet", "sour"]
|
||||
|
||||
puts find_uncommon_words("apple apple", "banana")
|
||||
puts find_uncommon_words('apple apple', 'banana')
|
||||
# => ["banana"]
|
||||
|
|
|
@ -38,8 +38,7 @@ class CircularQueue
|
|||
end
|
||||
|
||||
def add(x)
|
||||
raise "Queue is at max capacity" if size == max_size
|
||||
|
||||
raise 'Queue is at max capacity' if size == max_size
|
||||
|
||||
queue[back] = x
|
||||
@back = (back + 1) % max_size
|
||||
|
@ -47,7 +46,7 @@ class CircularQueue
|
|||
end
|
||||
|
||||
def pop
|
||||
raise "Queue is empty" if size == 0
|
||||
raise 'Queue is empty' if size == 0
|
||||
|
||||
temp = queue[front]
|
||||
queue[front] = nil
|
||||
|
@ -62,7 +61,7 @@ queue = CircularQueue.new(3)
|
|||
|
||||
begin
|
||||
queue.pop
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
|
||||
|
@ -72,7 +71,7 @@ queue.add(3)
|
|||
|
||||
begin
|
||||
queue.add(4)
|
||||
rescue => e
|
||||
rescue StandardError => e
|
||||
puts e.message
|
||||
end
|
||||
|
||||
|
|
|
@ -19,9 +19,6 @@
|
|||
# Constraints:
|
||||
# 1 <= n <= 45
|
||||
|
||||
|
||||
|
||||
|
||||
# Dynamic Programming, Recursive Bottom Up Approach - O(n) Time / O(n) Space
|
||||
# Init memoization hash (only 1 parameter)
|
||||
# Set base cases which are memo[0] = 1 and memo[1] = 1, since there are only 1 way to get to each stair
|
||||
|
@ -32,16 +29,15 @@
|
|||
# return memo[n] if memo[n] exists.
|
||||
# otherwise, memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
|
||||
|
||||
|
||||
# @param {Integer} n
|
||||
# @return {Integer}
|
||||
def climb_stairs(n)
|
||||
memo = Hash.new
|
||||
memo = {}
|
||||
|
||||
memo[0] = 1
|
||||
memo[1] = 1
|
||||
|
||||
return memo[n] if n == 0 || n == 1
|
||||
return memo[n] if [0, 1].include?(n)
|
||||
|
||||
(2..n).each do |n|
|
||||
recurse(n, memo)
|
||||
|
@ -50,14 +46,12 @@ def climb_stairs(n)
|
|||
memo[n]
|
||||
end
|
||||
|
||||
|
||||
def recurse(n, memo)
|
||||
return memo[n] if memo[n]
|
||||
|
||||
memo[n] = recurse(n - 1, memo) + recurse(n - 2, memo)
|
||||
end
|
||||
|
||||
|
||||
puts climb_stairs(2)
|
||||
# => 2
|
||||
|
||||
|
|
|
@ -69,6 +69,7 @@ end
|
|||
|
||||
def generate(num_rows)
|
||||
return [[1]] if num_rows < 1
|
||||
|
||||
result = [[1], [1, 1]]
|
||||
|
||||
(2...num_rows + 1).each do |row|
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
|
||||
def abs_max(x, y)
|
||||
num = x - y
|
||||
max_value = ((x + y + num.abs()) / 2)
|
||||
max_value = ((x + y + num.abs) / 2)
|
||||
"The Abs Max of #{x} and #{y} is #{max_value}."
|
||||
rescue
|
||||
"Error: Provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Provide number only!'
|
||||
end
|
||||
|
||||
# Valid inputs
|
||||
|
@ -20,11 +20,11 @@ puts abs_max(9, -121)
|
|||
# The Abs Max of 9 and -121 is 9.
|
||||
|
||||
# Invalid inputs
|
||||
puts abs_max(2, "-1")
|
||||
puts abs_max(2, '-1')
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_max("3", "5")
|
||||
puts abs_max('3', '5')
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_max("a", "5")
|
||||
puts abs_max('a', '5')
|
||||
# Error: Provide number only!
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
|
||||
def abs_min(x, y)
|
||||
num = x - y
|
||||
min_value = ((x + y - num.abs()) / 2)
|
||||
min_value = ((x + y - num.abs) / 2)
|
||||
"The Abs Min of #{x} and #{y} is #{min_value}."
|
||||
rescue
|
||||
"Error: Provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Provide number only!'
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -24,11 +24,11 @@ puts abs_min(9, -121)
|
|||
#
|
||||
# Invalid inputs
|
||||
#
|
||||
puts abs_min(2, "-1")
|
||||
puts abs_min(2, '-1')
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_min("3", "5")
|
||||
puts abs_min('3', '5')
|
||||
# Error: Provide number only!
|
||||
|
||||
puts abs_min("a", "5")
|
||||
puts abs_min('a', '5')
|
||||
# Error: Provide number only!
|
||||
|
|
|
@ -6,8 +6,8 @@ def add(*array)
|
|||
sum = 0
|
||||
array.each { |a| sum += a }
|
||||
puts "The sum of following elements #{array} is #{sum}"
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -27,8 +27,8 @@ puts add(25, 45)
|
|||
# Invalid inputs
|
||||
#
|
||||
|
||||
puts add("1", 2, 3)
|
||||
puts add('1', 2, 3)
|
||||
# Error: Please provide number only!
|
||||
|
||||
puts add("a", 1)
|
||||
puts add('a', 1)
|
||||
# Error: Please provide number only!
|
||||
|
|
|
@ -11,7 +11,7 @@ def armstrong_number(number)
|
|||
num = number
|
||||
sum = 0
|
||||
len = number.digits.count
|
||||
while(number != 0)
|
||||
while number != 0
|
||||
remainder = number % 10
|
||||
sum += remainder**len
|
||||
number /= 10
|
||||
|
@ -22,8 +22,8 @@ def armstrong_number(number)
|
|||
else
|
||||
"The number #{num} is not an Armstrong number."
|
||||
end
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
#
|
||||
|
@ -50,8 +50,8 @@ puts armstrong_number(123)
|
|||
#
|
||||
# Invalid inputs
|
||||
#
|
||||
puts armstrong_number("153")
|
||||
puts armstrong_number('153')
|
||||
# Error: Please provide number only!
|
||||
|
||||
puts armstrong_number("a")
|
||||
puts armstrong_number('a')
|
||||
# Error: Please provide number only!
|
||||
|
|
|
@ -12,8 +12,8 @@ module AverageMean
|
|||
else
|
||||
raise
|
||||
end
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -24,5 +24,5 @@ AverageMean.average_mean(3, 2, 2, 2)
|
|||
|
||||
# Invalid inputs
|
||||
AverageMean.average_mean(2, 3, 1, 5)
|
||||
AverageMean.average_mean(2, 3, "a")
|
||||
AverageMean.average_mean("a", 1, 2)
|
||||
AverageMean.average_mean(2, 3, 'a')
|
||||
AverageMean.average_mean('a', 1, 2)
|
||||
|
|
|
@ -6,7 +6,7 @@ module AverageMedian
|
|||
if n.instance_of? Integer
|
||||
if n == array.size
|
||||
array.sort
|
||||
if array.size % 2 == 0
|
||||
if array.size.even?
|
||||
mid_element_1 = array.size / 2
|
||||
mid_element_2 = mid_element_1 + 1
|
||||
puts "The average median of the following elements #{array} is #{(array[mid_element_1 - 1] + array[mid_element_2 - 1]) / 2}."
|
||||
|
@ -20,8 +20,8 @@ module AverageMedian
|
|||
else
|
||||
raise
|
||||
end
|
||||
rescue
|
||||
puts "Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
puts 'Error: Please provide number only!'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -48,7 +48,7 @@ puts AverageMedian.average_median(1, 5)
|
|||
puts AverageMedian.average_median(2, 3, 1, 5)
|
||||
# Provide the required 2 elements properly!
|
||||
|
||||
puts AverageMedian.average_median(2, 3, "a")
|
||||
puts AverageMedian.average_median(2, 3, 'a')
|
||||
# Traceback (most recent call last):
|
||||
# 4: from /Users/voliveira/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `<main>'
|
||||
# 3: from /Users/voliveira/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `load'
|
||||
|
@ -56,5 +56,5 @@ puts AverageMedian.average_median(2, 3, "a")
|
|||
# 1: from (irb):30
|
||||
# NameError (undefined local variable or method `verageMedian' for main:Object)
|
||||
|
||||
puts AverageMedian.average_median("a", 1, 2)
|
||||
puts AverageMedian.average_median('a', 1, 2)
|
||||
# Error: Please provide number only!
|
||||
|
|
|
@ -26,7 +26,6 @@
|
|||
# Input: n = 33
|
||||
# Output: 66045
|
||||
|
||||
|
||||
#
|
||||
# Approach: Math
|
||||
#
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
=begin
|
||||
A ruby program calculate factorial of a given number.
|
||||
Mathematical Explanation: The factorial of a number is the product of all the integers from 1 to that number.
|
||||
i.e: n! = n*(n-1)*(n-2)......*2*1
|
||||
=end
|
||||
# A ruby program calculate factorial of a given number.
|
||||
# Mathematical Explanation: The factorial of a number is the product of all the integers from 1 to that number.
|
||||
# i.e: n! = n*(n-1)*(n-2)......*2*1
|
||||
|
||||
#
|
||||
# Approach: Interative
|
||||
|
|
|
@ -8,15 +8,15 @@
|
|||
|
||||
def factorial(number)
|
||||
if number < 0
|
||||
"Please check your input number! The given number is a negative number."
|
||||
'Please check your input number! The given number is a negative number.'
|
||||
elsif number == 0
|
||||
"The factorial of #{number} is 1."
|
||||
else
|
||||
result = (1..number).inject(:*)
|
||||
"The factorial of #{number} is #{result}."
|
||||
end
|
||||
rescue
|
||||
"Error: Please provide integer only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide integer only!'
|
||||
end
|
||||
|
||||
# Valid inputs
|
||||
|
@ -36,8 +36,8 @@ puts factorial(-5)
|
|||
# Please check your input number! The given number is a negative number.
|
||||
|
||||
# Invalid inputs
|
||||
puts factorial("a")
|
||||
puts factorial('a')
|
||||
# Error: Please provide integer only!
|
||||
|
||||
puts factorial("2")
|
||||
puts factorial('2')
|
||||
# Error: Please provide integer only!
|
||||
|
|
|
@ -4,27 +4,25 @@
|
|||
def find_max(*array)
|
||||
max = array[0]
|
||||
array.each do |a|
|
||||
if a >= max
|
||||
max = a
|
||||
end
|
||||
max = a if a >= max
|
||||
end
|
||||
"The Max of the following elements #{array} is #{max}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Max method will return the maximum element from the set of input elements provided
|
||||
def predefined_max(*array)
|
||||
"The Max of the following elements #{array} is #{array.max}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Sort method will sort the elements in ascending order. Last method will return the end element out of the array
|
||||
def predefined_sort_last_max(*array)
|
||||
"The Max of the following elements #{array} is #{array.sort.last}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
"The Max of the following elements #{array} is #{array.max}."
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Using find_max
|
||||
|
@ -36,7 +34,7 @@ puts find_max(-221, -852, -1100, -10)
|
|||
# The Max of the following elements [-221, -852, -1100, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts find_max(5, "95", 2)
|
||||
puts find_max(5, '95', 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_max
|
||||
|
@ -48,7 +46,7 @@ puts predefined_max(-11, -51, -10, -10)
|
|||
# The Max of the following elements [-11, -51, -10, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_max("x", 5, 95, 2)
|
||||
puts predefined_max('x', 5, 95, 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_sort_last_max
|
||||
|
@ -60,5 +58,5 @@ puts predefined_sort_last_max(-21, -52, -100, -1)
|
|||
# The Max of the following elements [-21, -52, -100, -1] is -1.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_sort_last_max(5, 95, 2, "a")
|
||||
puts predefined_sort_last_max(5, 95, 2, 'a')
|
||||
# Error: Please provide number only!
|
||||
|
|
|
@ -4,27 +4,25 @@
|
|||
def find_min(*array)
|
||||
min = array[0]
|
||||
array.each do |a|
|
||||
if a <= min
|
||||
min = a
|
||||
end
|
||||
min = a if a <= min
|
||||
end
|
||||
"The Min of the following elements #{array} is #{min}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Min method will return the minimum element from the set of input elements provided
|
||||
def predefined_min(*array)
|
||||
"The Min of the following elements #{array} is #{array.min}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Sort method will sort the elements in ascending order. First method will return the beginning element out of the array
|
||||
def predefined_sort_first_min(*array)
|
||||
"The Min of the following elements #{array} is #{array.sort.first}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
"The Min of the following elements #{array} is #{array.min}."
|
||||
rescue StandardError
|
||||
'Error: Please provide number only!'
|
||||
end
|
||||
|
||||
# Using find_min
|
||||
|
@ -36,7 +34,7 @@ puts find_min(-221, -852, -1100, -10)
|
|||
# The Min of the following elements [-221, -852, -1100, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts find_min(5, "95", 2)
|
||||
puts find_min(5, '95', 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_min
|
||||
|
@ -48,7 +46,7 @@ puts predefined_min(-11, -51, -10, -10)
|
|||
# The Min of the following elements [-11, -51, -10, -10] is -10.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_min("x", 5, 95, 2)
|
||||
puts predefined_min('x', 5, 95, 2)
|
||||
# Error: Please provide number only!
|
||||
|
||||
# Using predefined_sort_first_min
|
||||
|
@ -60,5 +58,5 @@ puts predefined_sort_first_min(-21, -52, -100, -1)
|
|||
# The Min of the following elements [-21, -52, -100, -1] is -1.
|
||||
|
||||
# Invalid inputs
|
||||
puts predefined_sort_first_min(5, 95, 2, "a")
|
||||
puts predefined_sort_first_min(5, 95, 2, 'a')
|
||||
# Error: Please provide number only!
|
||||
|
|
|
@ -13,9 +13,9 @@
|
|||
|
||||
def lucas(number)
|
||||
golden_ratio = (1 + 5**0.5) / 2
|
||||
((golden_ratio**number).round()).to_i
|
||||
rescue
|
||||
"Error: Provide number only!"
|
||||
(golden_ratio**number).round.to_i
|
||||
rescue StandardError
|
||||
'Error: Provide number only!'
|
||||
end
|
||||
|
||||
puts lucas(4)
|
||||
|
@ -24,7 +24,7 @@ puts lucas(4)
|
|||
puts lucas(3)
|
||||
# 4
|
||||
|
||||
puts lucas("3")
|
||||
puts lucas('3')
|
||||
# Error: Provide number only!
|
||||
|
||||
puts lucas(2)
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
def is_power_of_two(n)
|
||||
if n == 1
|
||||
true
|
||||
elsif n % 2 == 0
|
||||
elsif n.even?
|
||||
is_power_of_two(n / 2)
|
||||
else
|
||||
false
|
||||
|
@ -67,9 +67,7 @@ puts is_power_of_two(n)
|
|||
# Time Complexity: O(n)
|
||||
#
|
||||
def is_power_of_two(n)
|
||||
while n % 2 == 0 && n != 0
|
||||
n /= 2
|
||||
end
|
||||
n /= 2 while n.even? && n != 0
|
||||
n == 1
|
||||
end
|
||||
|
||||
|
|
|
@ -3,14 +3,14 @@
|
|||
|
||||
# Prime number check function
|
||||
def prime_number(number)
|
||||
if number <= 1
|
||||
non_prime_flag = true
|
||||
non_prime_flag = if number <= 1
|
||||
true
|
||||
elsif number == 2
|
||||
non_prime_flag = false
|
||||
elsif number % 2 == 0
|
||||
non_prime_flag = true
|
||||
false
|
||||
elsif number.even?
|
||||
true
|
||||
else
|
||||
non_prime_flag = (2..Math.sqrt(number)).any? { |i| number % i == 0 }
|
||||
(2..Math.sqrt(number)).any? { |i| number % i == 0 }
|
||||
end
|
||||
|
||||
if !non_prime_flag
|
||||
|
|
|
@ -36,13 +36,13 @@
|
|||
# Because only a constant number of single-value variables are used, the space complexity is O(1).
|
||||
|
||||
ROM_NUMS = {
|
||||
"I" => 1,
|
||||
"V" => 5,
|
||||
"X" => 10,
|
||||
"L" => 50,
|
||||
"C" => 100,
|
||||
"D" => 500,
|
||||
"M" => 1000
|
||||
'I' => 1,
|
||||
'V' => 5,
|
||||
'X' => 10,
|
||||
'L' => 50,
|
||||
'C' => 100,
|
||||
'D' => 500,
|
||||
'M' => 1000
|
||||
}
|
||||
|
||||
# Now, recall that each symbol adds its own value, except for when a smaller
|
||||
|
@ -73,24 +73,24 @@ def roman_to_int(s)
|
|||
res
|
||||
end
|
||||
|
||||
s = "III"
|
||||
s = 'III'
|
||||
puts roman_to_int(s)
|
||||
# Output: 3
|
||||
|
||||
s = "IV"
|
||||
s = 'IV'
|
||||
puts roman_to_int(s)
|
||||
# Output: 4
|
||||
|
||||
s = "IX"
|
||||
s = 'IX'
|
||||
puts roman_to_int(s)
|
||||
# Output: 9
|
||||
|
||||
s = "LVIII"
|
||||
s = 'LVIII'
|
||||
puts roman_to_int(s)
|
||||
# Output: 58
|
||||
# Explanation: L = 50, V= 5, III = 3.
|
||||
|
||||
s = "MCMXCIV"
|
||||
s = 'MCMXCIV'
|
||||
puts roman_to_int(s)
|
||||
# Output: 1994
|
||||
# Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#
|
||||
# Constraints: The given dates are valid dates between the years 1971 and 2100.
|
||||
|
||||
|
||||
#
|
||||
# Approach 1: Using Ruby built-in feature Date.parse
|
||||
# Time complexity: O(1)
|
||||
|
@ -28,8 +27,8 @@ def number_of_days(date1, date2)
|
|||
(end_date - beginning_date).to_i.abs
|
||||
end
|
||||
|
||||
puts number_of_days("2019-06-29", "2019-06-30")
|
||||
puts number_of_days('2019-06-29', '2019-06-30')
|
||||
# => 1
|
||||
|
||||
puts number_of_days("2020-01-15", "2019-12-31")
|
||||
puts number_of_days('2020-01-15', '2019-12-31')
|
||||
# => 15
|
||||
|
|
|
@ -25,9 +25,6 @@
|
|||
# 1 <= m, n <= 300
|
||||
# grid[i][j] is '0' or '1'.
|
||||
|
||||
|
||||
|
||||
|
||||
# DFS, Recursive Bottom Up Approach - O(n*m) Time / O(1) Space
|
||||
# Init num_of_islands = 0, return if the grid is empty
|
||||
# Start a double loop with index to iterate through each plot (each value is a plot of either water or land in this case)
|
||||
|
@ -52,13 +49,13 @@ def num_islands(grid)
|
|||
grid.each_with_index do |row, x|
|
||||
row.each_with_index do |plot, y|
|
||||
# if the plot is water, start a dfs
|
||||
if plot == "1"
|
||||
next unless plot == '1'
|
||||
|
||||
dfs(grid, x, y)
|
||||
# add 1 to islands once all connected land plots are searched
|
||||
islands += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# return ans
|
||||
islands
|
||||
|
@ -66,10 +63,10 @@ end
|
|||
|
||||
def dfs(grid, x, y)
|
||||
# don't search if out of bounds, or if it's already water
|
||||
return if x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == "0"
|
||||
return if x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] == '0'
|
||||
|
||||
# set the plot to water
|
||||
grid[x][y] = "0"
|
||||
grid[x][y] = '0'
|
||||
|
||||
# search each adjacent plot
|
||||
dfs(grid, x - 1, y) # up
|
||||
|
|
|
@ -11,7 +11,10 @@ def cocktail_sort(array)
|
|||
end
|
||||
end
|
||||
break unless swapped
|
||||
start, finish, way = swapped, start, -way
|
||||
|
||||
finish = start
|
||||
start = swapped
|
||||
way = -way
|
||||
end
|
||||
array
|
||||
end
|
||||
|
|
|
@ -9,4 +9,3 @@ class TestInsertionSort < Minitest::Test
|
|||
cocktail_sort(input)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
def comb_sort(array)
|
||||
gap = array.length
|
||||
swaps = true
|
||||
while gap > 1 or swaps
|
||||
while (gap > 1) || swaps
|
||||
gap = [1, (gap / 1.25).to_i].max
|
||||
swaps = false
|
||||
0.upto(array.length - gap - 1) do |i|
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
def pancake_sort(array)
|
||||
return array if array.length <= 1
|
||||
|
||||
(array.length - 1).downto(1) do |index|
|
||||
max_index = array[0..index].index(array[0..index].max)
|
||||
next if max_index == index
|
||||
|
|
Loading…
Reference in a new issue