Created
September 9, 2013 19:28
-
-
Save davidvthecoder/6500315 to your computer and use it in GitHub Desktop.
Handy Non-recursive way to find the contiguous natural number ranges given a list of numbers. For example:
A list of numbers 1,2,3,6,7,8,9,14,15,22
the contiguous ranges would be: 1-3, 6-9, 14-15, 22-22 Potential issues: since it finds all the missing Numbers, ranges with huge gaps of missing numbers can hinder performance.
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
public List<Range> findRanges(List<int> numbers) { | |
List<Range> ranges = new List<Range>(); // create a list of ranges that will eventually be populated. | |
numbers = numbers.OrderBy(x=>x).ToList<int>(); // order the list of numbers coming in. | |
int min = numbers.Min(); // get the smallest number | |
int max = numbers.Max(); // get the largest number | |
List<int> missingNumbers = new List<int>(); // create an empty list of missing numbers | |
int i = min; // create a counter for stepping through all the numbers starting with the smallest number | |
while (i < max) { // step through each possible number from min to max. | |
if (!numbers.Contains(i)) { // if the number doesnt exist from our list, its a missing number | |
// found a gap in the number (missing number) | |
missingNumbers.Add(i); // add the number to the list of missing numbers. | |
} | |
i++; // increase the counter by 1. | |
} | |
int tempLow = min; // create a tempLow starting with the min since its our first low value. | |
int countInc = 1; // create a count incrementer. This is for checking for the last possible range. last found number to max. | |
foreach (int num in missingNumbers) { // step through the list of missing numbers | |
if (tempLow != num) { // if tempLow is not the same as the missing number create a new range | |
Range r = new Range { // new rang is always the tempLow to the missing number - 1 | |
low = tempLow, // temp low which is the first valid number after the last high value | |
high = num - 1 // last high value is the last valid number before the current missing number | |
}; | |
// example 1,2,5,6 - 3,4 are both missing numbers. one less than missing number is a high number and the low number was the last valid high number | |
tempLow = num + 1; // add 1 to the last misisng number in attempt to find the next lowest number- note will not be used if its a missing number with the if statement above. | |
ranges.Add(r); // add the range to the list of ranges. | |
} else { // if tempLow is accidentally set to a missing number, increase it by 1 until is not a missing number | |
tempLow += 1; // tempLow should be increase until it is a valid number ( not a missing number) | |
} | |
if (missingNumbers.Count == countInc) { // if this is the last item in the foreach of missing numbers ( then create the last range | |
Range lastRange = new Range{ | |
low = tempLow, // last valid number | |
high = max // the last and highest value | |
}; | |
if(!ranges.Contains(lastRange)){ // if the last valid number has not been added to the ranges, do so. | |
ranges.Add(lastRange); | |
} | |
} | |
countInc += 1; // increase the foreach counter by 1. | |
} | |
return ranges; // return the list of ranges. | |
} |
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
public class Range { | |
public int low { get; set; } // low value for the range | |
public int high { get; set; } // high value for the range | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment