有位文豪说得好:“看一个作家的水平,不是看他发表了多少文字,而要看他的废纸篓里扔掉了多少。” 我觉得同样的理论适用于编程。好的程序员,他们删掉的代码,比留下来的还要多很多。如果你看见一个人写了很多代码,却没有删掉多少,那他的代码一定有很多垃圾。 ----摘自《编程的智慧》
今天在OpenHatch上找到一个涉及到几个Python知识点的小项目,需求是编写一个带评分功能的英文填字图版游戏。
详细需求:
practice breaking down a problem and solving it in Python from scratch
practice command line argument parsing
practice reading from files
practice working with dictionaries and for loops
涉及到的Python知识点:
1.文件操作
2.argparse库的使用(个人选择,用sys.argv效果类似)
3.collections中Counter类的高级应用
4.列表解析的应用
5.其他若干小的函数应用
首次编码:
#!/usr/bin/env python
from __future__ import print_function
import argparse
SCORES = {"a":1,"c":3,"b":3,"e":1,"d":2,"g":2,"f":4,"i":1,
"h":4,"k":5,"j":8,"m":3,"l":1,"o":1,"n":1,"q":10,"p":3,
"s":1,"r":1,"u":1,"t":1,"w":4,"v":4,"y":4,"x":8,"z":10}
def get_sowpod():
word_list = []
with open('/Users/gaoxing/Desktop/sowpods.txt')as f:
for line in f:
word_list.append(line.strip().lower())
return word_list
def argparse_arg():
argparser = argparse.ArgumentParser()
argparser.add_argument('args')
res_arg = argparser.parse_args()
return vars(res_arg)
def arg_proc():
arg_dict = argparse_arg()
arg_list = []
for w in(arg_dict.values()[0]):
if w not in arg_list:
arg_list.append(w)
return arg_list
def scrab():
word_list = get_sowpod()
source_list = arg_proc()
target_list = []
for line in word_list:
tmp_list = []
for word in line:
if word not in tmp_list:
tmp_list.append(word)
if set(tmp_list).issubset(set(source_list)):
target_list.append(line)
return target_list
def get_score(words):
dict_score = {}
for word in words:
dict_score[word] = sum(SCORES[c] for c in word)
return dict_score
def sort_dict(d):
dict1 = sorted(zip(d.values(), d.keys()), reverse=True)
for key, val in dict1:
print(key,',',val)
if __name__ == '__main__':
d = get_score(scrab())
print(sort_dict(d))
效果图:
开始代码重构:
存在的问题:
1.函数概念不突出,没有main函数,很多情况都是直接用一个函数调用另一个函数,没有输入参数
2.对此脚本输入大写参数如 ZAEFIEE
时,程序无任何输出结果
3.不输入参数时,得不到需求要求的效果图,
需求:
本脚本:
4.函数
scrab()
过于复杂
进行修改:
修改内容:
1.把argparse_arg()
函数和 arg_proc()
合并,并改名为get_user()
;
2.增加main
函数,分别接受用户输入,构建核对列表,调用scrab()
函数并进行评分和排序;
3.对于问题4, collections库中有个计数器叫做Counter,可以进行加减法运算,直接运用这个类和列表解析式来代替多个for
循环和复杂的if
判断
4.修改大写参数无输出结果问题
5.若干细节修改
修改后的代码:
#!/usr/bin/env python
from __future__ import print_function
import argparse
from collections import Counter
SCORES = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def get_sowpod():
word_list = []
with open('/Users/gaoxing/Desktop/sowpods.txt') as f:
for line in f:
word_list.append(line.strip())
return word_list
def get_user():
argparser = argparse.ArgumentParser()
argparser.add_argument('[RACK]')
res_arg = argparser.parse_args()
arg_dict = vars(res_arg)
return arg_dict.values()[0]
def scrab_plus(source, target):
source_counter = Counter(target)
return [ word.lower() for word in source if not (Counter(word) - source_counter) ]
def get_score(words):
dict_score = {}
for word in words:
dict_score[word] = sum(SCORES[c] for c in word)
return dict_score
def sort_dict(d):
dict1 = sorted(zip(d.values(), d.keys()), reverse=True)
for key, val in dict1:
print(key, ',', val)
def main():
sowpod_list = get_sowpod()
user_list = get_user()
target_list = scrab_plus(sowpod_list, user_list.upper())
sort_dict(get_score(target_list))
if __name__ == '__main__':
main()
运行效果图:
欢迎各位指出这个脚本可以再改进的地方!
若需转载请联系本人!