Skip to content

Instantly share code, notes, and snippets.

@daneb
Last active June 26, 2017 21:25
Show Gist options
  • Save daneb/41faeab3122380a714dd6160932105ce to your computer and use it in GitHub Desktop.
Save daneb/41faeab3122380a714dd6160932105ce to your computer and use it in GitHub Desktop.
Ruby
def get_sum(input)
input.inject(0) { |total, x| total + x }
end
if __FILE__ == $0
count = 0
total = 0
input = []
$stdin.each_line do |line|
if count.zero?
total = line.to_i
else
input << line.to_i
end
break if count == total
count += 1
end
answer = get_sum(input)
puts answer
end
def get_decreasing_increasing_point(input_array)
input_array.uniq!
increasing = false
descreasing = false
size = input_array.size - 1
location = size
input_array[0] < input_array[1] ? increasing = true : descreasing = true
(0..size).each do |index|
next if index.zero?
break if index == size
location = index if increasing && input_array[index] > input_array[index + 1]
location = index if descreasing && input_array[index] < input_array[index+ 1]
break if location != size
end
location
end
if __FILE__ == $0
array_input_error = 'Please input an array matching input size'
input_array = []
input_count = 2
array_size = 0
$stdin.each_line do |line|
array_size = line.to_i if input_count == 2
if input_count == 1
input_array = line.split.map(&:to_i)
puts array_input_error if input_array.length != array_size
end
input_count -= 1
break if input_count.zero?
end
answer = get_decreasing_increasing_point(input_array)
puts answer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment