public class MinMaxRange {
public static void main(String[] args) {
int[][] zipRanges1 = new int[][] { new int[] { 94133, 94133 },
new int[] { 94226, 94399 },
new int[] { 94200, 94299 }
};
MinMaxRange minMaxRange = new MinMaxRange();
int[][] normalizedZipRanges = minMaxRange.getTheNormalizedZipRanges(zipRanges1);
System.out.println("Normalized range:");
minMaxRange.printTheZipRanges(normalizedZipRanges);
}
/**
* Method to normalize a given zipcode range.
*
*
* @param zipRangesToNormalize
* @return
*/
private int[][] getTheNormalizedZipRanges(int[][] zipRangesToNormalize)Range {
//create an array to hold the new normalized zipcode range list.
int[][] newZipRanges = new int[zipRangesToNormalize.length][];
//this array will keep track of which ranges are removed from the original list
boolean[] rangePurged = new boolean[zipRangesToNormalize.length];
//variable to hold the index for the newZipRange array
int newZipIndex = 0;
//sort the zipcode range list based on the lower bound in each range
this.sortZipCodeRangeArray(zipRangesToNormalize);
//loop through the given zipcode range array
public static forvoid main(int i = 0; i < zipRangesToNormalize.length;String[] i++args) {
/*
* if the range at the current position is merged/combined into the previous range,
* skip to the next one in the list
*/
if (rangePurged[i])
continue;
/*int[][] Ranges1 = new int[][] { new int[] { 94133, 94133 },
* If we are at the last range in the list, then * just add the range to the new listint[] { 94226, 94399 },
*/
if (i == (zipRangesToNormalize.length - 1) ) new int[] { 94200, 94299 }
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
break;
};
/*
* Logic to normalize the zipcode range
*
* Take the upperbound from the current range and compare it with
* the lowerbound from the next element.
*
* If the upperbound of the current range is lower than the lowerbound of the
* next range, then there are no overlaps. Add the current range to the new list and continue.
*
* If the upperbound of the current range is equal are greater than the lowerbound of the
* next range, then there are two possible options,
* 1). if the upperbound of the current rage is greater or equal to the upperbound of the next range,
* then create a new range with the lower bound = lowerbound of the current range and upperbound = upperbound of current range
* 2). Otherwise, create a new range with lower bound = lowerbound of the current range and upperbound = upperbound of the next range
*
*/
intRange minZipRange = zipRangesToNormalize[i][0];new Range();
int maxZip int[][] normalizedRanges = zipRangesToNormalize[i][1];
Range.getTheNormalizedRanges(Ranges1);
int nextMinZip = zipRangesToNormalize[i + 1][0]; System.out.println("Normalized range:");
intRange.print(normalizedRanges);
nextMaxZip = zipRangesToNormalize[i + 1][1];
}
if (maxZip >= nextMinZip private int[][] getTheNormalizedRanges(int[][] RangesToNormalize) {
int[][] newrange = new ifint[RangesToNormalize.length][];
(maxZip >= nextMaxZip)
boolean[] rangePurged = new boolean[RangesToNormalize.length];
newZipRanges[newZipIndex] = new int[] { minZip, maxZip }; int newIndex = 0;
else
newZipRanges[newZipIndex] = new int[] { minZip, nextMaxZip } this.sortRange(RangesToNormalize);
//keep track of the ranges that arefor purged(int fromi the= original0; listi < RangesToNormalize.length; i++) {
rangePurged[i+1]if =(rangePurged[i])
true; continue;
} else if (i == (RangesToNormalize.length - 1) ){
newrange[newIndex] = RangesToNormalize[i];
break;
}
//if there is no overlap, add the current range to the new list
newZipRanges[newZipIndex]int min = zipRangesToNormalize[i];RangesToNormalize[i][0];
int max = RangesToNormalize[i][1];
int nextMin = RangesToNormalize[i + 1][0];
int nextMax = RangesToNormalize[i + 1][1];
if (max >= nextMin) {
if (max >= nextMax)
newrange[newIndex] = new int[] { min, max };
else
newrange[newIndex] = new int[] { min, nextMax };
//keep track of the ranges that are purged from the original list.
rangePurged[i+1] = true;
} else {
//if there is no overlap, add the current range to the new list
newrange[newIndex] = RangesToNormalize[i];
}
newIndex++;
}
newZipIndex++;return newrange;
}
return newZipRanges;
}
/**
* method to print the zipcode range to the console
*
* @param zipRangesToPrint A 2d array representing the zipcode ranges to print
*/
private void printTheZipRanges(int[][] zipRangesToPrint){
if(zipRangesToPrint == null || zipRangesToPrint.length <=0)
throw new IllegalArgumentException("zipRangesToPrint is empty or null");
private forvoid print(int i = 0; i < zipRangesToPrint.length;int[][] i++printRange){
//I'm checking for null condition here because of the way I've created this array.
//I've initially assigned it the same length as the
if(printRange == null || printRange.length <=0)
throw new IllegalArgumentException("printRange is empty or null");
iffor (zipRangesToPrint[i] != null)
int i = 0; i < printRange.length; i++) {
if (printRange[i] != null)
{
System.out.println("["+ zipRangesToPrint[i][0]printRange[i][0] + ", "
+ zipRangesToPrint[i][1]+"]"printRange[i][1]+"]");
}
}
}
}
private void sortRange(int[][] rangeToSort){
Arrays.sort(rangeToSort, new Comparator<int[]>() {
/**
* Method to sort the zipcode range list based on the lower bound.
* @Override
* @param zipCodeRangesToSort A 2d integer array representing the zipcodepublic rangesint thatcompare(int[] needrange1, toint[] berange2) sorted{
* @return int range1Min = range1[0];
*/
private void sortZipCodeRangeArray(int[][] zipCodeRangesToSort){
Arrays.sort(zipCodeRangesToSort,int newrange2Min Comparator<int[]>()= {
range2[0];
@Override
public intif compare(int[] range1,range1Min int[]> range2range2Min) {
int range1Min = range1[0];
return 1;
int range2Min = range2[0];
if (range1Min >< range2Min)
return 1;
return -1;
if (range1Min < range2Min)
return -1;
0;
return}
0;
});
});
}
}
public class MinMaxRange {
public static void main(String[] args) {
int[][] zipRanges1 = new int[][] { new int[] { 94133, 94133 },
new int[] { 94226, 94399 },
new int[] { 94200, 94299 }
};
MinMaxRange minMaxRange = new MinMaxRange();
int[][] normalizedZipRanges = minMaxRange.getTheNormalizedZipRanges(zipRanges1);
System.out.println("Normalized range:");
minMaxRange.printTheZipRanges(normalizedZipRanges);
}
/**
* Method to normalize a given zipcode range.
*
*
* @param zipRangesToNormalize
* @return
*/
private int[][] getTheNormalizedZipRanges(int[][] zipRangesToNormalize) {
//create an array to hold the new normalized zipcode range list.
int[][] newZipRanges = new int[zipRangesToNormalize.length][];
//this array will keep track of which ranges are removed from the original list
boolean[] rangePurged = new boolean[zipRangesToNormalize.length];
//variable to hold the index for the newZipRange array
int newZipIndex = 0;
//sort the zipcode range list based on the lower bound in each range
this.sortZipCodeRangeArray(zipRangesToNormalize);
//loop through the given zipcode range array
for (int i = 0; i < zipRangesToNormalize.length; i++) {
/*
* if the range at the current position is merged/combined into the previous range,
* skip to the next one in the list
*/
if (rangePurged[i])
continue;
/*
* If we are at the last range in the list, then * just add the range to the new list
*/
if (i == (zipRangesToNormalize.length - 1) ){
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
break;
}
/*
* Logic to normalize the zipcode range
*
* Take the upperbound from the current range and compare it with
* the lowerbound from the next element.
*
* If the upperbound of the current range is lower than the lowerbound of the
* next range, then there are no overlaps. Add the current range to the new list and continue.
*
* If the upperbound of the current range is equal are greater than the lowerbound of the
* next range, then there are two possible options,
* 1). if the upperbound of the current rage is greater or equal to the upperbound of the next range,
* then create a new range with the lower bound = lowerbound of the current range and upperbound = upperbound of current range
* 2). Otherwise, create a new range with lower bound = lowerbound of the current range and upperbound = upperbound of the next range
*
*/
int minZip = zipRangesToNormalize[i][0];
int maxZip = zipRangesToNormalize[i][1];
int nextMinZip = zipRangesToNormalize[i + 1][0];
int nextMaxZip = zipRangesToNormalize[i + 1][1];
if (maxZip >= nextMinZip) {
if (maxZip >= nextMaxZip)
newZipRanges[newZipIndex] = new int[] { minZip, maxZip };
else
newZipRanges[newZipIndex] = new int[] { minZip, nextMaxZip };
//keep track of the ranges that are purged from the original list.
rangePurged[i+1] = true;
} else {
//if there is no overlap, add the current range to the new list
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
}
newZipIndex++;
}
return newZipRanges;
}
/**
* method to print the zipcode range to the console
*
* @param zipRangesToPrint A 2d array representing the zipcode ranges to print
*/
private void printTheZipRanges(int[][] zipRangesToPrint){
if(zipRangesToPrint == null || zipRangesToPrint.length <=0)
throw new IllegalArgumentException("zipRangesToPrint is empty or null");
for (int i = 0; i < zipRangesToPrint.length; i++){
//I'm checking for null condition here because of the way I've created this array.
//I've initially assigned it the same length as the
if (zipRangesToPrint[i] != null)
{
System.out.println("["+ zipRangesToPrint[i][0] + ", "
+ zipRangesToPrint[i][1]+"]");
}
}
}
/**
* Method to sort the zipcode range list based on the lower bound.
*
* @param zipCodeRangesToSort A 2d integer array representing the zipcode ranges that need to be sorted
* @return
*/
private void sortZipCodeRangeArray(int[][] zipCodeRangesToSort){
Arrays.sort(zipCodeRangesToSort, new Comparator<int[]>() {
@Override
public int compare(int[] range1, int[] range2) {
int range1Min = range1[0];
int range2Min = range2[0];
if (range1Min > range2Min)
return 1;
if (range1Min < range2Min)
return -1;
return 0;
}
});
}
}
public class Range {
public static void main(String[] args) {
int[][] Ranges1 = new int[][] { new int[] { 94133, 94133 },
new int[] { 94226, 94399 },
new int[] { 94200, 94299 }
};
Range Range = new Range();
int[][] normalizedRanges = Range.getTheNormalizedRanges(Ranges1);
System.out.println("Normalized range:");
Range.print(normalizedRanges);
}
private int[][] getTheNormalizedRanges(int[][] RangesToNormalize) {
int[][] newrange = new int[RangesToNormalize.length][];
boolean[] rangePurged = new boolean[RangesToNormalize.length];
int newIndex = 0;
this.sortRange(RangesToNormalize);
for (int i = 0; i < RangesToNormalize.length; i++) {
if (rangePurged[i])
continue;
if (i == (RangesToNormalize.length - 1) ){
newrange[newIndex] = RangesToNormalize[i];
break;
}
int min = RangesToNormalize[i][0];
int max = RangesToNormalize[i][1];
int nextMin = RangesToNormalize[i + 1][0];
int nextMax = RangesToNormalize[i + 1][1];
if (max >= nextMin) {
if (max >= nextMax)
newrange[newIndex] = new int[] { min, max };
else
newrange[newIndex] = new int[] { min, nextMax };
//keep track of the ranges that are purged from the original list.
rangePurged[i+1] = true;
} else {
//if there is no overlap, add the current range to the new list
newrange[newIndex] = RangesToNormalize[i];
}
newIndex++;
}
return newrange;
}
private void print(int[][] printRange){
if(printRange == null || printRange.length <=0)
throw new IllegalArgumentException("printRange is empty or null");
for (int i = 0; i < printRange.length; i++) {
if (printRange[i] != null)
{
System.out.println("["+ printRange[i][0] + ", "
+ printRange[i][1]+"]");
}
}
}
private void sortRange(int[][] rangeToSort){
Arrays.sort(rangeToSort, new Comparator<int[]>() {
@Override
public int compare(int[] range1, int[] range2) {
int range1Min = range1[0];
int range2Min = range2[0];
if (range1Min > range2Min)
return 1;
if (range1Min < range2Min)
return -1;
return 0;
}
});
}
}
public class MinMaxRange {
public static void main(String[] args) {
int[][] zipRanges1 = new int[][] { new int[] { 94133, 94133 },
new int[] { 94226, 94399 },
new int[] { 94200, 94299 }
};
MinMaxRange minMaxRange = new MinMaxRange();
int[][] normalizedZipRanges = minMaxRange.getTheNormalizedZipRanges(zipRanges1);
System.out.println("Normalized range:");
minMaxRange.printTheZipRanges(normalizedZipRanges);
}
/**
* Method to normalize a given zipcode range.
*
*
* @param zipRangesToNormalize
* @return
*/
private int[][] getTheNormalizedZipRanges(int[][] zipRangesToNormalize) {
//create an array to hold the new normalized zipcode range list.
int[][] newZipRanges = new int[zipRangesToNormalize.length][];
//this array will keep track of which ranges are removed from the original list
boolean[] rangePurged = new boolean[zipRangesToNormalize.length];
//variable to hold the index for the newZipRange array
int newZipIndex = 0;
//sort the zipcode range list based on the lower bound in each range
this.sortZipCodeRangeArray(zipRangesToNormalize);
//loop through the given zipcode range array
for (int i = 0; i < zipRangesToNormalize.length; i++) {
/*
* if the range at the current position is merged/combined into the previous range,
* skip to the next one in the list
*/
if (rangePurged[i])
continue;
/*
* If we are at the last range in the list, then
* just add the range to the new list
*/
if (i == (zipRangesToNormalize.length - 1) ){
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
break;
}
/*
* Logic to normalize the zipcode range
*
* Take the upperbound from the current range and compare it with
* the lowerbound from the next element.
*
* If the upperbound of the current range is lower than the lowerbound of the
* next range, then there are no overlaps. Add the current range to the new list and continue.
*
* If the upperbound of the current range is equal are greater than the lowerbound of the
* next range, then there are two possible options,
* 1). if the upperbound of the current rage is greater or equal to the upperbound of the next range,
* then create a new range with the lower bound = lowerbound of the current range and upperbound = upperbound of current range
* 2). Otherwise, create a new range with lower bound = lowerbound of the current range and upperbound = upperbound of the next range
*
*/
int minZip = zipRangesToNormalize[i][0];
int maxZip = zipRangesToNormalize[i][1];
int nextMinZip = zipRangesToNormalize[i + 1][0];
int nextMaxZip = zipRangesToNormalize[i + 1][1];
if (maxZip >= nextMinZip) {
if (maxZip >= nextMaxZip)
newZipRanges[newZipIndex] = new int[] { minZip, maxZip };
else
newZipRanges[newZipIndex] = new int[] { minZip, nextMaxZip };
//keep track of the ranges that are purged from the original list.
rangePurged[i+1] = true;
} else {
//if there is no overlap, add the current range to the new list
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
}
newZipIndex++;
}
return newZipRanges;
}
/**
* method to print the zipcode range to the console
*
* @param zipRangesToPrint A 2d array representing the zipcode ranges to print
*/
private void printTheZipRanges(int[][] zipRangesToPrint){
if(zipRangesToPrint == null || zipRangesToPrint.length <=0)
throw new IllegalArgumentException("zipRangesToPrint is empty or null");
for (int i = 0; i < zipRangesToPrint.length; i++) {
//I'm checking for null condition here because of the way I've created this array.
//I've initially assigned it the same length as the
if (zipRangesToPrint[i] != null)
{
System.out.println("["+ zipRangesToPrint[i][0] + ", "
+ zipRangesToPrint[i][1]+"]");
}
}
}
/**
* Method to sort the zipcode range list based on the lower bound.
*
* @param zipCodeRangesToSort A 2d integer array representing the zipcode ranges that need to be sorted
* @return
*/
private void sortZipCodeRangeArray(int[][] zipCodeRangesToSort){
Arrays.sort(zipCodeRangesToSort, new Comparator<int[]>() {
@Override
public int compare(int[] range1, int[] range2) {
int range1Min = range1[0];
int range2Min = range2[0];
if (range1Min > range2Min)
return 1;
if (range1Min < range2Min)
return -1;
return 0;
}
});
}
}
public class MinMaxRange {
public static void main(String[] args) {
int[][] zipRanges1 = new int[][] { new int[] { 94133, 94133 },
new int[] { 94226, 94399 },
new int[] { 94200, 94299 }
};
MinMaxRange minMaxRange = new MinMaxRange();
int[][] normalizedZipRanges = minMaxRange.getTheNormalizedZipRanges(zipRanges1);
System.out.println("Normalized range:");
minMaxRange.printTheZipRanges(normalizedZipRanges);
}
/**
* Method to normalize a given zipcode range.
*
*
* @param zipRangesToNormalize
* @return
*/
private int[][] getTheNormalizedZipRanges(int[][] zipRangesToNormalize) {
//create an array to hold the new normalized zipcode range list.
int[][] newZipRanges = new int[zipRangesToNormalize.length][];
//this array will keep track of which ranges are removed from the original list
boolean[] rangePurged = new boolean[zipRangesToNormalize.length];
//variable to hold the index for the newZipRange array
int newZipIndex = 0;
//sort the zipcode range list based on the lower bound in each range
this.sortZipCodeRangeArray(zipRangesToNormalize);
//loop through the given zipcode range array
for (int i = 0; i < zipRangesToNormalize.length; i++) {
/*
* if the range at the current position is merged/combined into the previous range,
* skip to the next one in the list
*/
if (rangePurged[i])
continue;
/*
* If we are at the last range in the list, then
* just add the range to the new list
*/
if (i == (zipRangesToNormalize.length - 1) ){
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
break;
}
/*
* Logic to normalize the zipcode range
*
* Take the upperbound from the current range and compare it with
* the lowerbound from the next element.
*
* If the upperbound of the current range is lower than the lowerbound of the
* next range, then there are no overlaps. Add the current range to the new list and continue.
*
* If the upperbound of the current range is equal are greater than the lowerbound of the
* next range, then there are two possible options,
* 1). if the upperbound of the current rage is greater or equal to the upperbound of the next range,
* then create a new range with the lower bound = lowerbound of the current range and upperbound = upperbound of current range
* 2). Otherwise, create a new range with lower bound = lowerbound of the current range and upperbound = upperbound of the next range
*
*/
int minZip = zipRangesToNormalize[i][0];
int maxZip = zipRangesToNormalize[i][1];
int nextMinZip = zipRangesToNormalize[i + 1][0];
int nextMaxZip = zipRangesToNormalize[i + 1][1];
if (maxZip >= nextMinZip) {
if (maxZip >= nextMaxZip)
newZipRanges[newZipIndex] = new int[] { minZip, maxZip };
else
newZipRanges[newZipIndex] = new int[] { minZip, nextMaxZip };
//keep track of the ranges that are purged from the original list.
rangePurged[i+1] = true;
} else {
//if there is no overlap, add the current range to the new list
newZipRanges[newZipIndex] = zipRangesToNormalize[i];
}
newZipIndex++;
}
return newZipRanges;
}
/**
* method to print the zipcode range to the console
*
* @param zipRangesToPrint A 2d array representing the zipcode ranges to print
*/
private void printTheZipRanges(int[][] zipRangesToPrint){
if(zipRangesToPrint == null || zipRangesToPrint.length <=0)
throw new IllegalArgumentException("zipRangesToPrint is empty or null");
for (int i = 0; i < zipRangesToPrint.length; i++) {
//I'm checking for null condition here because of the way I've created this array.
//I've initially assigned it the same length as the
if (zipRangesToPrint[i] != null)
{
System.out.println("["+ zipRangesToPrint[i][0] + ", "
+ zipRangesToPrint[i][1]+"]");
}
}
}
/**
* Method to sort the zipcode range list based on the lower bound.
*
* @param zipCodeRangesToSort A 2d integer array representing the zipcode ranges that need to be sorted
* @return
*/
private void sortZipCodeRangeArray(int[][] zipCodeRangesToSort){
Arrays.sort(zipCodeRangesToSort, new Comparator<int[]>() {
@Override
public int compare(int[] range1, int[] range2) {
int range1Min = range1[0];
int range2Min = range2[0];
if (range1Min > range2Min)
return 1;
if (range1Min < range2Min)
return -1;
return 0;
}
});
}
}
Loading
Loading
lang-java