Skip to content

Commit

Permalink
add recent solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongxiao37 committed Jul 28, 2020
0 parents commit 010eb06
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 0 deletions.
59 changes: 59 additions & 0 deletions 4kyuSnail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
def snail(array)
array_size = array.size
return [] if array.any?(&:empty?)
return [] if array_size != array[0].size

top, left, right, bottom = 0, 0, array_size-1, array_size-1
i, j = 0, 0
dir = :right
list = []

loop do
list << array[i][j]
# move ahead
if dir == :right
if j < right
j += 1
else
dir = :down
top += 1
i += 1
break if i > bottom
end
elsif dir == :down
if i < bottom
i += 1
else
dir = :left
right -= 1
j -= 1
break if j <= left
end
elsif dir == :left
if j > left
j -= 1
else
dir = :up
bottom -= 1
i -= 1
break if i < top
end
elsif dir == :up
if i > top
i -= 1
else
dir = :right
left += 1
j += 1
break if j >= right
end
end
end

list << array[i][j] if array[i] && array[i][j]
list
end

p snail([[1]])
p snail([[1,2,3],[4,5,6],[7,8,9]])
p snail([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
13 changes: 13 additions & 0 deletions 4kyuStripComments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def solution(input, markers)
input.split("\n").map do |row|
line = row
markers.each do |marker|
marker = '\\' + marker
regex = /#{marker}.*\z/
line.gsub!(regex, '')
end
line.strip
end.join("\n")
end

p solution("apples, plums % and bananas\npears\noranges !applesauce", ["%", "!"])
31 changes: 31 additions & 0 deletions 4kyuSudokuSolutionValidator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def validSolution(board)
# check rows
valid = board.all? { |row| row.uniq.size == row.size }
return false unless valid

# check column
valid = board.transpose.all? { |row| row.uniq.size == row.size }
return false unless valid

0.step(by: 3, to: 8) do |row_idx|
0.step(by: 3, to: 8).each do |col_idx|
temp = board[row_idx..row_idx+2].map { |row| row[col_idx..col_idx+2] }.flatten
valid = temp.size == temp.uniq.size
return false unless valid
end
end

valid
end



p validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
[6, 7, 2, 1, 9, 5, 3, 4, 8],
[1, 9, 8, 3, 4, 2, 5, 6, 7],
[8, 5, 9, 7, 6, 1, 4, 2, 3],
[4, 2, 6, 8, 5, 3, 7, 9, 1],
[7, 1, 3, 9, 2, 4, 8, 5, 6],
[9, 6, 1, 5, 3, 7, 2, 8, 4],
[2, 8, 7, 4, 1, 9, 6, 3, 5],
[3, 4, 5, 2, 8, 6, 1, 7, 9]])
21 changes: 21 additions & 0 deletions 5kyuDirectionsReduction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def dirReduc(arr)
s = arr.join('#')
new_size = s.size
loop do
s.gsub!(/WEST#+EAST/, '')
s.gsub!(/EAST#+WEST/, '')
s.gsub!(/NORTH#+SOUTH/, '')
s.gsub!(/SOUTH#+NORTH/, '')
break if s.size == new_size
new_size = s.size
end
s.split(/#+/).reject { |e| e.empty? }
end

a = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]

p dirReduc(a)

u=["NORTH", "WEST", "SOUTH", "EAST"]

p dirReduc(u)
52 changes: 52 additions & 0 deletions 5kyuIterativeRotationCipher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
def encode(n, s)
s_arr = s.chars
p space_index = s_arr.size.times.select { |e| s_arr[e] == ' ' }
n.times do
p s.gsub!(' ', '')
k = n > s.size ? n % s.size : n
p s = s[-k..-1] + s[0..(-k-1)]
space_index.each do |i|
s = s[0...i] + ' ' + s[i..-1]
end
p s
s = s.split(/ +/).map do |e|
n.times { e = e[-1] + e[0..-2] }
p e
e
end.join
space_index.each do |i|
s = s[0...i] + ' ' + s[i..-1]
end
p s
end
s
end

def decode(s)
m = /\A(\d+) (.*)\z/m.match(s)
n = m[1].to_i
s = m[2]
s_arr = s.chars
p space_index = s_arr.size.times.select { |e| s_arr[e] == ' ' }
n.times do
s = s.split(/ +/).map do |e|
n.times { e = e[1..-1] + e[0] }
p e
e
end.join
space_index.each do |i|
s = s[0...i] + ' ' + s[i..-1]
end
p s

p s.gsub!(' ', '')
k = n > s.size ? n % s.size : n
p s = s[k..-1] + s[0..(k-1)]
space_index.each do |i|
s = s[0...i] + ' ' + s[i..-1]
end
p s
end
s
end

23 changes: 23 additions & 0 deletions 5kyuMaximumsubarraysum.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def max_sequence(arr)
p arr
return 0 if arr.empty? || arr.all?(&:negative?)

i = 0
i += 1 until arr[i].positive?
j = i
max_num = arr[i]

while j < arr.size
temp = arr[i..j].sum
max_num = temp if temp > max_num
if arr[i..j].sum < 0
i = j + 1
j = i
else
j += 1
end
end
max_num
end

p max_sequence([24, -20, -15, 2, -11, -18, -18, -24, 23, 12, -12, 11, 12, -23, 7, -14, -15, -10, -2, 14, 9, -9, -15, -17])
7 changes: 7 additions & 0 deletions 5kyuRGBToHexConversion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def rgb(r, g, b)
"%2s%2s%2s" % [r, g, b].map do |c|
c = c < 0 ? 0 : c
c = c > 255 ? 255 : c
c.to_s(16).upcase.rjust(2, "0")
end
end
11 changes: 11 additions & 0 deletions 5kyuRot13.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def rot13(string)
lower_dict = ('a'..'z').to_a
upper_dict = ('A'..'Z').to_a
string.chars.map do |s|
n = lower_dict[(lower_dict.index(s) + 13) % 26] if lower_dict.include?(s)
n = upper_dict[(upper_dict.index(s) + 13) % 26] if upper_dict.include?(s)
n || s
end.join
end

p rot13('test')
7 changes: 7 additions & 0 deletions 6kyuAretheythesame.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def comp(array1, array2)
return false if array1.nil? || array2.nil?
array1.sort!
array2.sort!
return false if array1.size != array2.size
(0..(array1.size-1)).all? { |i| array2[i] == array1[i] ** 2 }
end
3 changes: 3 additions & 0 deletions 6kyuCountthesmileyfaces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def count_smileys(arr)
arr.count { |e| /[:;][-~]?[\)D]/ =~ e }
end

0 comments on commit 010eb06

Please sign in to comment.