Skip to content

Instantly share code, notes, and snippets.

@gerardogc2378
Created April 12, 2017 23:25
Show Gist options
  • Save gerardogc2378/5f9e158c8b1198ef5ae8ed6a0ff1427e to your computer and use it in GitHub Desktop.
Save gerardogc2378/5f9e158c8b1198ef5ae8ed6a0ff1427e to your computer and use it in GitHub Desktop.
Ooyala Second Test
# 1
def reverse(val = "")
val.split.reverse.join(" ")
end
# 2
def first_missing(val = [])
return 1 if val.select {|x| x > 0}.empty?
return val.select {|x| x > 0}.inject(&:+) + 1 if val.select {|x| x > 0}.size == 1
counter = val.uniq.select {|x| x > 0}.sort.min
val.uniq.select {|x| x > 0}.sort.each do |item|
return counter if item != counter
counter+=1
end
val.uniq.select {|x| x > 0}.sort.max + 1
end
# 3
# val = [[0,0,1], [1,0,1], [1,1,0]]
def island_size(val = [])
max = 0
counter = 0
return 0 if val.empty?
val.each_with_index do |row, index|
for col in 0..val.size - 1
counter = 0 if row[col] == 0
if row[col] == 1
counter += 1 + north(val, col, index - 1) + south(val, col, index + 1)
max = counter if counter > max
end
end
counter = 0
end
max
end
def north(val = [], col = 0, index = 0)
return 0 if index < 0
return 0 if val[index][col] == 0
return val[index][col] + north(val, col, index - 1) if val[index][col] == 1
return 0
rescue => e
0
end
def south(val = [], col = 0, index = 0)
return 0 if index > val.size - 1
return 0 if val[index][col] == 0
return val[index][col] + south(val, col, index + 1) if val[index][col] == 1
return 0
rescue => e
0
end
# 4
def largest_combination(val = [])
val.permutation(val.size).to_a.map {|x| x.join.to_i}.max
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment