jimmychu0807
BAN USER#! /usr/bin/env ruby
solutions = []
# inputs = [ %w(0 1 1), %w(1 1 1), %w(1 1 1) ]
inputs = [ %w(0 0 1 0 0 1 0),
%w(1 0 1 0 1 0 1),
%w(1 1 1 1 1 1 1),
%w(0 0 1 0 0 0 0),
%w(0 0 0 0 0 0 0) ]
def get_largest_plus(inputs, row, col, try_len = 1)
unless ((row - try_len) >= 0 && inputs[row - try_len][col] == '1' && #top
(row + try_len) < inputs.size && inputs[row + try_len][col] == '1' && #bottom
(col - try_len) >= 0 && inputs[row][col - try_len] == '1' && #left
(col + try_len) < inputs[row].size && inputs[row][col + try_len] == '1') #right
return try_len - 1
end
return get_largest_plus(inputs, row, col, try_len + 1)
end
(0...inputs.size).each do |row|
(0...inputs[row].size).each do |col|
next unless inputs[row][col] == '1'
plus_size = get_largest_plus(inputs, row, col)
next unless plus_size > 0
solutions.push([[row, col], plus_size])
end
end
largest_i = nil
size = 0
solutions.each_with_index do |sol, i|
next if largest_i && size > sol[1]
largest_i = i
size = sol[1]
end
puts "#{size}, #{solutions[largest_i][0]}"
#! /usr/bin/env ruby
def get_tv_len(inputs)
on_arr = Array.new
inputs.sort! { |x, y| x[0] <=> y[0] }
inputs.each do |latest|
if on_arr.empty?
on_arr.push(latest)
next
end
last_el = on_arr[-1]
if last_el[1] < latest[0]
on_arr.push(latest)
elsif last_el[1] < latest[1]
last_el[1] = latest[1]
end
end
on_len = on_arr.map { |on| on[1] - on[0] }
return on_len.reduce(0, :+)
end
inputs = [[1,4], [6,8], [2,4], [7,9], [10, 15]]
p get_tv_len(inputs)
- jimmychu0807 January 11, 2017