day45——三个练习题

Posted yangjinbiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day45——三个练习题相关的知识,希望对你有一定的参考价值。

一、写一个程序,实现 abcd * 9 = dcba ,其中 a、b、c、d 都是数字

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 
 4 "实现:abcd * 9 = dcba"
 5 
 6 for a in xrange(10):
 7 for b in xrange(10):
 8 for c in xrange(10):
 9 for d in xrange(10):
10 abcd = a * 1000 + b * 100 + c * 10 + d
11 dcba = d * 1000 + c * 100 + b * 10 + a
12 if abcd * 9 == dcba:
13 print abcd
14 else:
15 continue

 

二、写一个程序,实现九宫格,要求所有的行,列,对角线的和都为15

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 
 4 class NinePaper(object):
 5     def __init__(self):
 6         self.numbers = list()
 7         for i in range(1, 10):
 8             self.numbers.append(i)
 9         print("numbers = {0}".format(self.numbers))
10     def run(self):
11         lines = [(x,y,z) for x in self.numbers for y in self.numbers if x!=y for z in self.numbers if x!=z and y!=z and x+y+z == 15]
12         for line1 in lines:
13             for line2 in lines:
14                 if set(line1) & set(line2):
15                     continue
16                 for line3 in lines:
17                     if set(line1) & set(line2) & set(line3):
18                         continue
19                     if line1[0] + line2[0] + line3[0] != 15:
20                         continue
21                     if line1[1] + line2[1] + line3[1] != 15:
22                         continue
23                     if line1[2] + line2[2] + line3[2] != 15:
24                         continue
25                     if line1[0] + line2[1] + line3[2] != 15:
26                         continue
27                     if line1[2] + line2[1] + line3[0] != 15:
28                         continue
29                     print(‘‘‘
30             _____________
31             |_{0}_|_{1}_|_{2}_|
32             |_{3}_|_{4}_|_{5}_|
33             |_{6}_|_{7}_|_{8}_|
34             ‘‘‘.format(line1[0], line1[1], line1[2], line2[0], line2[1], line2[2], line3[0], line3[1], line3[2],))
35 
36 def main():
37     ninePaper = NinePaper()
38     ninePaper.run()
39 
40 if __name__ == __main__:
41     main()

三、写一个程序,根据 UID 对 /etc/passwd 进行排序

 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 
 4 import os
 5 import codecs
 6 
 7 class SortPasswd(object):
 8   def __init__(self):
 9     self.passwd = "passwd"
10     self.newpasswd = "newPasswd"
11     self.contextList = list()
12     if not os.path.exists(self.passwd):
13       print("please download passwd from linux.")
14       exit(1)
15     print("sort file is :{0}".format(self.passwd))
16     print("sorted file is :{0}".format(self.newpasswd))
17 
18   def getContextList(self):
19     with codecs.open("passwd") as fr:
20       self.contextList += sorted(fr.readlines(), key=lambda line:int(line.split(":")[2]), reverse=False)
21   
22   def writeContextList(self):
23     with codecs.open("new_passwd", "w") as fw:
24       fw.writelines(self.contextList)
25 
26 def main():
27 sortpasswd = SortPasswd()
28 sortpasswd.getContextList()
29 sortpasswd.writeContextList()
30 
31 if __name__ == __main__:
32 main()

 

以上是关于day45——三个练习题的主要内容,如果未能解决你的问题,请参考以下文章

Day 45 Mysql 数据库练习题二

集合代码----小练习4

webView loadUrl 打开浏览器选择对话框

以编程方式替换片段

apriori片段代码

从搜索文档中查找最小片段的算法?