Last active
May 30, 2016 11:13
-
-
Save mitallast/a704ffc3e3921d8b310b9ecf6f87a278 to your computer and use it in GitHub Desktop.
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
int[] a = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
int[] b = [1, 2, 3, 4, 9, 8, 7, 6, 5] | |
int[] e = [9, 1, 8, 2, 7, 3, 6, 4, 5] | |
int[] f = [9, 8, 7, 6, 5, 4, 3, 2, 1] | |
int[] c = [1, 2, 3, 4, 5, 6, 7, 8, 10] | |
int[] d = [1, 2, 3, 4, 5, 6, 7, 8, 8] | |
int[] g = [1, 8, 3, 4, 5, 6, 7, 8, 8] | |
def check(int[] a) { | |
println a | |
int N = a.length | |
int i = 0 | |
while (i < N) { | |
int c = a[i] | |
print i | |
println c | |
// out of range | |
if (c < 1 || c > N) { | |
return false | |
} | |
// duplicate | |
if (c <= i) { | |
return false | |
} | |
// swap | |
if (c != i + 1) { | |
int t = a[c - 1] | |
if (t == c) { | |
return false; | |
} | |
a[c - 1] = c | |
a[i] = t | |
} else { | |
i++ | |
} | |
} | |
return true; | |
} | |
assert check(a) | |
assert check(b) | |
assert check(e) | |
assert !check(c) | |
assert !check(d) | |
assert !check(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment