练习

 1 # 写一个程序,实现 abcd * 9 = dcba ,其中 a、b、c、d 都是数字
 2 
 3 class CountNumber(object):
 4     '''ABCD * 9 = DCBA
 5     通过计算机的方法,给我们计算出A = ?  B = ? C = ? D = ?
 6     A: 1-9
 7     B: 0-9
 8     C: 0-9
 9     D: 1-9
10     A != B != C !=D
11     '''
12     def __init__(self):
13         print("ABCD * 9 = DCBA; A!=B!=C!=D")
14     def numAbcd(self):
15         for A in range(1, 10):
16             for B in range(0, 10):
17                 for C in range(0, 10):
18                     for D in range(1, 10):
19                         if (A*1000 + B*100 + C*10 + D) * 9 == (D*1000 + C*100 + B*10 + A):
20                             print("{0}{1}{2}{3} *9 = {4}{5}{6}{7}".format(A,B,C,D,D,C,B,A))
21                             print("A = {0}, B = {1}, C = {2}, D = {3}".format(A,B, C, D))
22 def main():
23     countNumber = CountNumber()
24     countNumber.numAbcd()
25 
26 if __name__ == '__main__':
27     main()
 1 # 写一个程序,实现九宫格,要求所有的行,列,对角线的和都为15
 2 '''九宫格'''
 3 
 4 class NinePaper(object):
 5     def __init__(self):
 6         print('''
 7                     _____________
 8                     |_A_|_B_|_C_|
 9                     |_D_|_E_|_F_|
10                     |_G_|_H_|_I_|
11                     A, B, C, D, E, F, G, H, I 必须是1-9数字,且不能重复
12                     所有的行,列,对角线的和都为15
13         ''')
14         self.numbers = list()
15         for i in range(1, 10):
16             self.numbers.append(i)
17         print("numbers = {0}".format(self.numbers))
18     def run(self):
19         for A in range(1, 10):
20             l1 = list()
21             l1 += self.numbers
22             l1.remove(A)
23             for B in l1:
24                 l2 = list()
25                 l2 += l1
26                 l2.remove(B)
27                 for C in l2:
28                     l3 = list()
29                     l3 += l2
30                     l3.remove(C)
31                     for D in l3:
32                         l4 = list()
33                         l4 += l3
34                         l4.remove(D)
35                         for E in l4:
36                             l5 = list()
37                             l5 += l4
38                             l5.remove(E)
39                             for F in l5:
40                                 l6 = list()
41                                 l6 += l5
42                                 l6.remove(F)
43                                 for G in l6:
44                                     l7 = list()
45                                     l7 += l6
46                                     l7.remove(G)
47                                     for H in l7:
48                                         l8 = list()
49                                         l8 += l7
50                                         l8.remove(H)
51                                         for I in l8:
52                                             if A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == C+E+G == 15:
53                                                 print('''
54                                             _____________
55                                             |_{0}_|_{1}_|_{2}_|
56                                             |_{3}_|_{4}_|_{5}_|
57                                             |_{6}_|_{7}_|_{8}_|
58                                                 '''.format(A, B, C, D, E, F, G, H, I))
59 def main():
60     ninePaper = NinePaper()
61     ninePaper.run()
62 
63 if __name__ == '__main__':
64     main()
 1 # 写一个程序,根据 UID 对 /etc/passwd 进行排序
 2 import codecs
 3 import os
 4 
 5 
 6 class SortPasswd(object):
 7     def __init__(self):
 8         self.passwd = "passwd"
 9         self.newpasswd = "newPasswd"
10         self.contextList = list()
11         if not os.path.exists(self.passwd):
12             print("please download passwd from linux.")
13             exit(1)
14         print("sort file is :{0}".format(self.passwd))
15         print("sorted file is :{0}".format(self.newpasswd))
16     def getContextList(self):
17         with codecs.open("passwd") as fr:
18             self.contextList += sorted(fr.readlines(), key=lambda line:int(line.split(":")[2]), reverse=False)
19     def writeContextList(self):
20         with codecs.open("new_passwd", "w") as fw:
21             fw.writelines(self.contextList)
22 
23 def main():
24     sortpasswd = SortPasswd()
25     sortpasswd.getContextList()
26     sortpasswd.writeContextList()
27 
28 if __name__ == '__main__':
29     main()

 

posted @ 2017-12-31 22:59  依哈  阅读(144)  评论(0编辑  收藏  举报