Guest User

Untitled

a guest
Jul 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #! /opt/local/bin/ruby
  2.  
  3. class Difference
  4. def initialize(array)
  5. @array = array.map(&:to_i)
  6. end
  7.  
  8. def find_max_difference
  9. global_min, global_max = *Array.new(2) { @array.first }
  10. start_index, end_index = *Array.new(2) { 0 }
  11. @array.each_with_index do |element, i|
  12. if element < global_min
  13. global_min = element
  14. end_index = i
  15. else
  16. if element > global_max
  17. global_max = element
  18. start_index = i
  19. end
  20. end
  21. end
  22. global_diff = global_max - global_min
  23. { :global_diff => global_diff, :start_index => start_index, :end_index => end_index }
  24. end
  25.  
  26. end
  27.  
  28. puts "Enter a number of integers seperated by commas and spaces:"
  29.  
  30. array = gets.split(", ")
  31.  
  32. puts "\nFinding differences\n"
  33.  
  34. # 0, -1, -20, 5, 15, 50, 1, -50 as example should return 50, -50: 100 diff
  35. #
  36. difference = Difference.new(array).find_max_difference
  37.  
  38. puts "Global Difference: #{difference[:global_diff]}\nStarting Index: #{difference[:start_index]} - Value: #{array[difference[:start_index]]}
  39. Ending Index: #{difference[:end_index]} - Value: #{array[difference[:end_index]]}\n"
Add Comment
Please, Sign In to add comment