How to think like a Computer Scientist: 課後習題第十四章4

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     31/08/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import sys
#import string
import time
import random
import copy

def test(did_pass):
    '''print the result of a test '''
    linenum=sys._getframe(1).f_lineno
    if did_pass:
        msg = "Test at line{0} ok".format(linenum)
    else:
        msg = "Test at line{0} failed".format(linenum)
    print msg

def share_diagonal(x0,y0,x1,y1):
    dy = abs(y1-y0)
    dx = abs(x1-x0)

    return dy == dx

def col_clashed(bs, c):

    for i in range(c):
        if share_diagonal(i,bs[i],c,bs[c]):
            return True

    return False

def has_clashed(the_board):
    length = len(the_board)
    for i in range(1,length):
        if col_clashed(the_board, i):
            return True
    return False

def mirror_y_axis(bs):
    result = []
    for i in bs:
        result.append(7-i)
    return result

def mirror_x_axis(bs):
    result = copy.deepcopy(bs)
    result.reverse()
    return result

def rotate_90_degree(bs):
    result = list(range(8))
    for (i,v)in enumerate(bs):
        result[v] = 7 - i
    return result

def rotate_180_degree(bs):
    return rotate_90_degree(rotate_90_degree(bs))

def rotate_270_degree(bs):
    return rotate_180_degree(rotate_90_degree(bs))

def get_all_family_member(bs):
    #bs = [0,4,7,5,2,6,1,3]
    result = []
    result.append(rotate_90_degree(bs))
    result.append(rotate_180_degree(bs))
    result.append(rotate_270_degree(bs))
    result.append(mirror_x_axis(bs))
    result.append(mirror_y_axis(bs))

    tmp = mirror_x_axis(rotate_90_degree(bs))
    if tmp not in result:
        result.append(tmp)
    tmp = mirror_y_axis(rotate_90_degree(bs))
    if tmp not in result:
        result.append(tmp)
        tmp = mirror_x_axis(rotate_180_degree(bs))
    if tmp not in result:
        result.append(tmp)
    tmp = mirror_y_axis(rotate_270_degree(bs))
    if tmp not in result:
        result.append(tmp)
    if tmp not in result:
        result.append(tmp)
    tmp = mirror_y_axis(rotate_270_degree(bs))
    if tmp not in result:
        result.append(tmp)

    return result

def test_mirror_relation(unique, bs):
    family = get_all_family_member(unique)
    if bs in family:
        return True
    return False

def test_suite():
    rng = random.Random()
    bd = list(range(8))  #modify the parameter to get board size of 4,8,16
    num_found = 0
    tries = 0
    t0 = time.clock()
    fruit_list = []
    while num_found < 12:
        rng.shuffle(bd)
        tries += 1
        found = False
        if not has_clashed(bd):
            if bd in fruit_list:
                continue

            for unique in fruit_list:
                if test_mirror_relation(unique,bd):
                    found = True
                    break

            if found == True:
                continue
            fruit_list.append(copy.deepcopy(bd))
            t1 = time.clock()
            print ("Found solution {0} in {1:>4} tries time used: {2:.6f} seconds.".format(bd,tries,t1-t0))
            tries = 0
            num_found += 1
            t0 = t1

    for i in fruit_list:
        print i

def main():
    #print test_mirror_relation([0,4,7,5,2,6,1,3],[7,1,3,0,6,4,2,5])
    test_suite()
    pass

if __name__ == '__main__':
    main()
Found solution [4, 6, 3, 0, 2, 7, 5, 1] in  281 tries time used: 0.004484 seconds.
Found solution [3, 7, 0, 2, 5, 1, 6, 4] in  311 tries time used: 0.005502 seconds.
Found solution [3, 1, 7, 4, 6, 0, 2, 5] in  867 tries time used: 0.012594 seconds.
Found solution [2, 5, 1, 6, 0, 3, 7, 4] in 1228 tries time used: 0.022219 seconds.
Found solution [7, 1, 3, 0, 6, 4, 2, 5] in  357 tries time used: 0.005872 seconds.
Found solution [2, 0, 6, 4, 7, 1, 3, 5] in  201 tries time used: 0.003755 seconds.
Found solution [2, 4, 1, 7, 0, 6, 3, 5] in 2008 tries time used: 0.029161 seconds.
Found solution [4, 1, 3, 6, 2, 7, 5, 0] in 2018 tries time used: 0.035975 seconds.
Found solution [3, 7, 0, 4, 6, 1, 5, 2] in 1743 tries time used: 0.031616 seconds.
Found solution [2, 7, 3, 6, 0, 5, 1, 4] in  468 tries time used: 0.008579 seconds.
Found solution [4, 6, 1, 3, 7, 0, 2, 5] in 2509 tries time used: 0.040942 seconds.
Found solution [3, 6, 4, 2, 0, 5, 7, 1] in 1529 tries time used: 0.035933 seconds.
[4, 6, 3, 0, 2, 7, 5, 1]
[3, 7, 0, 2, 5, 1, 6, 4]
[3, 1, 7, 4, 6, 0, 2, 5]
[2, 5, 1, 6, 0, 3, 7, 4]
[7, 1, 3, 0, 6, 4, 2, 5]
[2, 0, 6, 4, 7, 1, 3, 5]
[2, 4, 1, 7, 0, 6, 3, 5]
[4, 1, 3, 6, 2, 7, 5, 0]
[3, 7, 0, 4, 6, 1, 5, 2]
[2, 7, 3, 6, 0, 5, 1, 4]
[4, 6, 1, 3, 7, 0, 2, 5]
[3, 6, 4, 2, 0, 5, 7, 1]


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章