zoukankan      html  css  js  c++  java
  • 三个习题

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

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    "实现:abcd * 9 = dcba"
    
    for a in xrange(1,10):
        for b in xrange(10):
            for c in xrange(10):
                for d in xrange(10):
                    abcd = a * 1000 + b * 100 + c * 10 + d
                    dcba = d * 1000 + c * 100 + b * 10 + a
                    if abcd * 9 == dcba:
                        print abcd
                    else:
                        continue

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

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    class NinePaper(object):
        def __init__(self):
            self.numbers = list()
            for i in range(1, 10):
                self.numbers.append(i)
            print("numbers = {0}".format(self.numbers))
        def run(self):
            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]
            for line1 in lines:
                for line2 in lines:
                    if set(line1) & set(line2):
                        continue
                    for line3 in lines:
                        if set(line1) & set(line2) & set(line3):
                            continue
                        if line1[0] + line2[0] + line3[0] != 15:
                            continue
                        if line1[1] + line2[1] + line3[1] != 15:
                            continue
                        if line1[2] + line2[2] + line3[2] != 15:
                            continue
                        if line1[0] + line2[1] + line3[2] != 15:
                            continue
                        if line1[2] + line2[1] + line3[0] != 15:
                            continue
                        print('''
                _____________
                |_{0}_|_{1}_|_{2}_|
                |_{3}_|_{4}_|_{5}_|
                |_{6}_|_{7}_|_{8}_|
                '''.format(line1[0], line1[1], line1[2], line2[0], line2[1], line2[2], line3[0], line3[1], line3[2],))
    
    def main():
        ninePaper = NinePaper()
        ninePaper.run()
    
    if __name__ == '__main__':
        main()

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

    #!/usr/bin/env python
    #-*- coding:utf-8 -*-
    
    import os
    import codecs
    
    class SortPasswd(object):
      def __init__(self):
        self.passwd = "passwd"
        self.newpasswd = "newPasswd"
        self.contextList = list()
        if not os.path.exists(self.passwd):
          print("please download passwd from linux.")
          exit(1)
        print("sort file is :{0}".format(self.passwd))
        print("sorted file is :{0}".format(self.newpasswd))
    
      def getContextList(self):
        with codecs.open("passwd") as fr:
          self.contextList += sorted(fr.readlines(), key=lambda line:int(line.split(":")[2]), reverse=False)
      
      def writeContextList(self):
        with codecs.open("new_passwd", "w") as fw:
          fw.writelines(self.contextList)
    
    def main():
    sortpasswd = SortPasswd()
    sortpasswd.getContextList()
    sortpasswd.writeContextList()
    
    if __name__ == '__main__':
    main()
  • 相关阅读:
    泛型接口协变和抗变
    泛型类功能
    泛型结构
    using 关键字给类和名称空间指定别名
    sqlite创建数据库问题
    sqlite命令
    必须输入大于0的整数
    最近在看c#本质论和B站上对应这本书的视频
    Linux系统管理笔记
    创建圆形类,其中包括set,get方法
  • 原文地址:https://www.cnblogs.com/the-way-to-bifk/p/8159482.html
Copyright © 2011-2022 走看看