Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /opt/local/bin/ruby
- class Difference
- def initialize(array)
- @array = array.map(&:to_i)
- end
- def find_max_difference
- global_min, global_max = *Array.new(2) { @array.first }
- start_index, end_index = *Array.new(2) { 0 }
- @array.each_with_index do |element, i|
- if element < global_min
- global_min = element
- end_index = i
- else
- if element > global_max
- global_max = element
- start_index = i
- end
- end
- end
- global_diff = global_max - global_min
- { :global_diff => global_diff, :start_index => start_index, :end_index => end_index }
- end
- end
- puts "Enter a number of integers seperated by commas and spaces:"
- array = gets.split(", ")
- puts "\nFinding differences\n"
- # 0, -1, -20, 5, 15, 50, 1, -50 as example should return 50, -50: 100 diff
- #
- difference = Difference.new(array).find_max_difference
- puts "Global Difference: #{difference[:global_diff]}\nStarting Index: #{difference[:start_index]} - Value: #{array[difference[:start_index]]}
- Ending Index: #{difference[:end_index]} - Value: #{array[difference[:end_index]]}\n"
Add Comment
Please, Sign In to add comment