Created
July 10, 2014 03:21
-
-
Save weisi/66bf6b082b9d974d1290 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
#!/usr/bin/env python | |
# Weisi Dai <weisi@x-research.com> | |
t = 0 | |
swaps = 0 | |
def check(a, i): | |
global swaps | |
t = i | |
if a[t] == t: | |
return False | |
while a[t] <> t: | |
a[t], t = t, a[t] | |
swaps = swaps + 1 | |
if t == i: | |
return False | |
return True | |
def checkDup(a): | |
global swaps | |
swaps = 0 | |
for i in range(len(a)): | |
if check(a, i): | |
return True | |
return False |
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
..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... | |
Salute! All answers correct! Max swaps = 5 |
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
#!/usr/bin/env python | |
# Weisi Dai <weisi@x-research.com> | |
import chkdup | |
import sys | |
LENGTH = 5 | |
def naive_checkDup(a): | |
for x, itemx in enumerate(a): | |
for y, itemy in enumerate(a[x + 1:]): | |
if itemx == itemy: | |
return True | |
return False | |
def generate_arr(ID): | |
ret = [] | |
for i in range(LENGTH): | |
ret.append(ID % LENGTH) | |
ID = ID / LENGTH | |
return ret | |
wrong_ans = False | |
max_swaps = 0 | |
for i in range(LENGTH ** LENGTH): | |
arr = generate_arr(i) | |
arr_copy = arr[:] | |
ans_chkDup = chkdup.checkDup(arr) | |
ans_naive_checkDup = naive_checkDup(arr_copy) | |
max_swaps = max(max_swaps, chkdup.swaps) | |
if ans_chkDup != ans_naive_checkDup: | |
print "Wrong answer for %s" % arr_copy | |
wrong_ans = True | |
else: | |
sys.stderr.write(".") | |
if wrong_ans: | |
print "There's at least 1 wrong answer." | |
else: | |
print "Salute! All answers correct! Max swaps = %s" % (max_swaps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
include
using namespace std;
template<typename Type, int SIZE>
bool swap_check(Type(&a)[SIZE], int index)
{
/* 获取此索引对应的元素值 */
int value = a[index];
}
template<typename Type, int SIZE>
bool fuck(Type (&a)[SIZE])
{
for (int index = 0; index < SIZE; ++index)
{
/* 如果此索引对应的元素值不等于索引值,则尝试交换此元素 */
if (!swap_check(a, index))
{
return false;
}
}
}
int main()
{
int a[] = { 2, 3, 1, 0 , 6, 5, 4, 7};
bool ret = fuck(a);
return 0;
}