Created
August 19, 2011 20:38
-
-
Save jvsidler/1157946 to your computer and use it in GitHub Desktop.
Technical interview answer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ARGV.empty? | |
puts "\nPlease enter any two SEQUENTIAL random numbers separated by one space. i.e 16 21\n" | |
a, b = gets.strip.split(" ") | |
else | |
a, b = ARGV[0], ARGV[1] | |
end | |
if a.to_i > 0 && b.to_i == 0 | |
puts "Please give two numbers." | |
exit | |
elsif a.to_i > b.to_i | |
puts "Please make sure the two numbers are in sequential order." | |
exit | |
end | |
puts "\nOriginal Ranges:" | |
p array_of_ranges = [[1,3],[5,18],[24,28],[29,30],[33,49]] | |
new_range = [a.to_i,b.to_i] | |
if array_of_ranges.first.first > new_range.last | |
array_of_ranges.insert 0, new_range | |
elsif array_of_ranges.last.last < new_range.first | |
array_of_ranges.push new_range | |
else | |
array_of_ranges.each do |check| | |
current_range = check.first..check.last | |
unless new_range.first < check.first && array_of_ranges.index(check) == 0 | |
if current_range.include? new_range.first | |
@insert_range_left = false | |
@left = array_of_ranges.index(check) | |
elsif new_range.first < check.first && @left.nil? | |
@insert_range_left = true | |
@left = array_of_ranges.index(check) | |
end | |
else | |
@left = 0 | |
@insert_range_left = true | |
end | |
unless @left.nil? | |
if current_range.include? new_range.last | |
@insert_range_right = false | |
@right = array_of_ranges.index(check) | |
elsif new_range.last > check.last | |
@insert_range_right = true | |
@right = array_of_ranges.index(check) | |
end | |
end | |
end | |
unless @left.nil? || @right.nil? | |
if @insert_range_left && !@insert_range_right | |
new_range = [new_range.first,array_of_ranges[@right].last] | |
elsif !@insert_range_left && @insert_range_right | |
new_range = [array_of_ranges[@left].first,new_range.last] | |
elsif !@insert_range_left && !@insert_range_right | |
new_range = [array_of_ranges[@left].first,array_of_ranges[@right].last] | |
end | |
array_of_ranges.delete_if { |i| (@left..@right).include?( array_of_ranges.index(i) ) } | |
end | |
array_of_ranges.insert @left, new_range | |
array_of_ranges.each do |i| | |
unless i == array_of_ranges.last | |
if i.last == array_of_ranges[array_of_ranges.index(i) + 1].first - 1 | |
i.concat(array_of_ranges[array_of_ranges.index(i) + 1]).sort! | |
array_of_ranges.delete_at array_of_ranges.index(i) + 1 | |
end | |
end | |
if i.first == array_of_ranges[array_of_ranges.index(i) - 1].last + 1 | |
i.concat(array_of_ranges[array_of_ranges.index(i) - 1]).sort! | |
array_of_ranges.delete_at array_of_ranges.index(i) - 1 | |
end | |
i.replace([i.first,i.last]) | |
end | |
end | |
puts "\nResulting Ranges:" | |
p array_of_ranges | |
puts "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem was handed to me like this:
Each pair represents the beginning and ending value in an inclusive and ordered range. Write a program that will collapse or insert the new range appropriately. Ending result of the given numbers would be (1,3), (5,21), (24,30), (33,49)