Created
March 21, 2017 03:35
-
-
Save carlaiau/ac692be5041d23cee9082f8dda4e1921 to your computer and use it in GitHub Desktop.
Theoretical
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
/* This Fails tests */ | |
public static boolean isSetOf1toN(int[][] t){ | |
int l = 0; | |
for(int outer = 0; outer < t.length; outer++){ | |
l += t[outer].length; | |
} | |
int[] oneDimensional = new int[l]; | |
int i = 0; | |
for(int outer = 0; outer < t.length; outer++){ | |
for(int inner =0; inner < t[outer].length; inner++){ | |
oneDimensional[i] = t[outer][inner]; | |
i++; | |
} | |
} | |
Arrays.sort(oneDimensional); | |
/* If the last index does not equal index + 1, this is not a set. | |
But this fails the test */ | |
if(oneDimensional[oneDimensional.length] != oneDimensional.length + 1){ | |
return false; | |
} else{ | |
for(i = 1; i < oneDimensional.length; i++){ | |
if( oneDimensional[ i ] != oneDimensional[ i - 1] + 1){ | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
/* Correct Submission */ | |
public static boolean isSetOf1toN(int[][] t){ | |
int l = 0; | |
for(int outer = 0; outer < t.length; outer++){ | |
l += t[outer].length; | |
} | |
int[] oneDimensional = new int[l]; | |
int i = 0; | |
for(int outer = 0; outer < t.length; outer++){ | |
for(int inner =0; inner < t[outer].length; inner++){ | |
oneDimensional[i] = t[outer][inner]; | |
i++; | |
} | |
} | |
Arrays.sort(oneDimensional); | |
for(i = 1; i < oneDimensional.length; i++){ | |
if( oneDimensional[ i ] != oneDimensional[ i - 1] + 1){ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment