PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | 30 | 100 |
Estimate | 估計這個任務須要多少時間 | 20 | 30 |
Development | 開發 | 800 | 1000 |
Analysis | 需求分析 (包括學習新技術) | 60 | 120 |
Design Spec | 生成設計文檔 | 30 | 20 |
Design Review | 設計複審 | 20 | 10 |
Coding Standard | 代碼規範 (爲目前的開發制定合適的規範) | 30 | 30 |
Design | 具體設計 | 30 | 60 |
Coding | 具體編碼 | 200 | 250 |
Code Review | 代碼複審 | 30 | 30 |
Test | 測試(自我測試,修改代碼,提交修改) | 60 | 200 |
Reporting | 報告 | 60 | 150 |
Test Repor | 測試報告 | 10 | 30 |
Size Measurement | 計算工做量 | 20 | - |
Postmortem & Process Improvement Plan | 過後總結, 並提出過程改進計劃 | 30 | 120 |
合計 | 890 | 1250 |
main(argv)函數html
DFS(i, x, y)函數java
judge(i, x, y)函數git
MY_OTP(i)函數github
簡單流程圖
算法
很明顯代碼已是炸了,不過比起第一次用pylint已經好很多了...第一次但是負分XD數組
看下效果,有所進步,剩下的就是命名的規範了
性能優化
通過一陣搗鼓評分提高了很多,但仍然沒有達到滿分,測試了一下代碼正常運行
只能說,有時候投降不失爲一種優雅的退場,我就不折磨本身了
網絡
#改成直接給予參數,而不是從命令行接受 if __name__ == '__main__': # sys.argv[1:]爲要處理的參數列表,sys.argv[0]爲腳本名,所以棄之不用 #main(sys.argv[1:]) main(['-m', '9', '-n', '2', '-i', 'input.txt', '-o', 'output.txt'])
顯然對於我來講這個圖是天書
app
由此表中可得知被調用次數最多和耗時最多的是judge()合法性判斷函數和_DFS_()遞歸函數框架
M = 9 MY_MAPS = [] # 上面都是給judge()函數運行提供必要的全局變量 with open('output.txt', 'r', encoding='utf-8') as _fp_: # 此處直接讀取已解矩陣用來判斷合法性 _MYMAP_ = [] for line in _fp_.readlines(): if line != '\n': # 用換行符分割矩陣 _MYMAP_.append(list(map(int, line.strip().split(" ")))) else: MY_MAPS.append(_MYMAP_) _MYMAP_ = [] MY_MAPS.append(_MYMAP_) # MY_MAPS是集合了全部數據的三維數組 #單元測試時用來提供全局變量... ################# #global M, MY_MAPS # 行列不重複判斷
import unittest from Sudoku import judge class test_judge(unittest.TestCase): def test_myfun(self): test_num = judge(0, 1, 2)#測試數值 self.assertEqual(test_num, 1)#指望值 if __name__ == '__main__': unittest.main()
GOOOOOD!測試符合預期結構
# -*- coding: UTF-8 -*- import sys import getopt # 全局變量 M = "" N = "" MY_MAPS = [] OP = ""
if __name__ == '__main__': # sys.argv[1:]爲要處理的參數列表,sys.argv[0]爲腳本名,所以棄之不用 main(sys.argv[1:])
def main(argv): """ 經過sys模塊來識別參數 :return: """ # 聲明全局變量 global M, N global MY_MAPS, OP in_put = "" out_put = "" try: # 獲取參數並處理異常 opts, args = getopt.getopt(argv, "m:n:i:o:", ["help"]) except getopt.GetoptError: print('Error: Sudoku.py -m -n -i -o') sys.exit(2) # 處理獲取的參數 for opt, arg in opts: if opt in "--help": # 給予幫助提示 print('Error: Sudoku.py -m -n -i -o') sys.exit() elif opt in "-m": M = int(arg) elif opt in "-n": N = int(arg) elif opt in "-i": in_put = arg elif opt in "-o": out_put = arg with open(in_put, 'r', encoding='utf-8') as _fp_: # 以讀狀態打開指定文件讀取矩陣 _MYMAP_ = [] for line in _fp_.readlines(): if line != '\n': # 用換行符分割矩陣 _MYMAP_.append(list(map(int, line.strip().split(" ")))) else: MY_MAPS.append(_MYMAP_) _MYMAP_ = [] MY_MAPS.append(_MYMAP_) # MY_MAPS是集合了全部數據的三維數組 OP = open(out_put, 'w', encoding='utf-8') for i in range(N): if i > 0: OP.write('\n') # 分割矩陣 _DFS_(i, 0, 0) # 遞歸求解 OP.close()
def _DFS_(_i_, _x_, _y_): """ 【DFS】深度優先搜索遞歸方式 :return: """ # 聲明引用全局變量 global M, MY_MAPS if _x_ > M - 1: # 完成條件 _MY_OTP_(_i_) # 保存數值 elif MY_MAPS[_i_][_x_][_y_] != 0: # 當前格子不可填 if _y_ == M - 1: # 右邊界換行 _DFS_(_i_, _x_ + 1, 0) else: _DFS_(_i_, _x_, _y_ + 1) # 下一格 else: # 當前格可填 for i in range(1, M + 1): MY_MAPS[_i_][_x_][_y_] = i # 試探填入數值 if judge(_i_, _x_, _y_): # 判斷其試探值的合法性,當判斷函數返回值爲1即合法 if _y_ == M - 1: # 邊界狀況 _DFS_(_i_, _x_ + 1, 0) else: _DFS_(_i_, _x_, _y_ + 1) # 回溯 MY_MAPS[_i_][_x_][_y_] = 0
def judge(_i_, _x_, _y_): """ 合法性判斷 :return: """ global M, MY_MAPS # 行列不重複判斷 for i in range(M): if i != _x_ and MY_MAPS[_i_][_x_][_y_] == MY_MAPS[_i_][i][_y_]: return 0 if i != _y_ and MY_MAPS[_i_][_x_][_y_] == MY_MAPS[_i_][_x_][i]: return 0 # 區塊重複判斷 _x1_ = _y1_ = row = col = 0 # 塊內座標初始值 # 區塊定位參考於https://github.com/zxw0621/demo/blob/master/20177596/src/sudoku.py#L42 # 這定位寫的太好了 # 根據其階數肯定其模塊規模以及所屬模塊 if M % 3 == 0: row = 3 col = int(M / 3) elif M % 2 == 0: row = 2 col = int(M / 2) _x1_ = int(_x_ // row * row) _y1_ = int(_y_ // col * col) # 遍歷所屬區塊,檢查其合法性 for i in range(_x1_, _x1_ + row): for j in range(_y1_, _y1_ + col): if _x_ != i and _y_ != j and MY_MAPS[_i_][_x_][_y_] == MY_MAPS[_i_][i][j]: return 0 return 1
def _MY_OTP_(_i_): """ 向文件內寫入所得矩陣 :return: """ global N, M, MY_MAPS, OP # 遍歷當前求解矩陣 for _x_ in range(M): for _y_ in range(M): OP.write(str(MY_MAPS[_i_][_x_][_y_]) + ' ') OP.write('\n') # 換行
try: # 獲取參數並處理異常 opts, args = getopt.getopt(argv, "m:n:i:o:", ["help"]) except getopt.GetoptError: print('Error: Sudoku.py -m -n -i -o') sys.exit(2)
# 處理獲取的參數 for opt, arg in opts: if opt in "--help": # 給予幫助提示 print('Error: Sudoku.py -m -n -i -o') sys.exit() elif opt in "-m": M = int(arg) elif opt in "-n": N = int(arg) elif opt in "-i": in_put = arg elif opt in "-o": out_put = arg
命令行
輸入文件
輸出文件
至此程序宣佈完成...