• python的阶段复习


    1.ABCD乘于9 = DCBA,求ABCD的值,且ABCD均互不相等

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time    :2017/12/26 16:11
    # @Author  :huangdongju
    # @File    :1.py
    '''ABCD乘于9 = DCBA,求ABCD的值,且ABCD均互不相等'''
    
    class CountNumer(object):
        def __init__(self):
            print("ABCD*9=DCBA; A != B! =C != D")
    
        def numAbcd(self):
            for A in range(1,10):
                for B in range(0,10):
                    for C in range(0,10):
                        for D in range(1,10):
                            if (A*1000 + B*100 + C*10 + D)*9 == (D *1000 + C*100 + B*10 + A):
                                print("{0}{1}{2}{3}*9 = {3}{2}{1}{0}".format(A,B,C,D))
                                print("A = {0}, B = {1},C = {2}, D={3}".format(A,B,C,D))
    
    
    
    def main():
        countNumber = CountNumer()
        countNumber.numAbcd()
    
    if __name__ == '__main__':
        main()

      结果:

    2.九宫格,相加等于15

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time    :2017/12/26 16:50
    # @Author  :huangdongju
    # @File    :2.py
    '''九宫格,相加等于15'''
    class NinePaper(object):
        def __init__(self):
            print('''
                        _____________
                        |_A_|_B_|_C_|
                        |_D_|_E_|_F_|
                        |_G_|_H_|_I_|
                        A, B, C, D, E, F, G, H, I 必须是1-9数字,且不能重复
                        所有的行,列,对角线的和都为15
            '''
            )
            self.numbers = list()
            for i in range(1,10):
                self.numbers.append(i)
            print("numbers = {0}".format(self.numbers))
    
        def run(self):
            for A in range(1,10):
                l1 = list()
                l1 += self.numbers
                l1.remove(A)
                for B in l1:
                    l2 = list()
                    l2 += l1
                    l2.remove(B)
                    for C in l2:
                        l3 = list()
                        l3 += l2
                        l3.remove(C)
                        for D in l3:
                            l4 = list()
                            l4 += l3
                            l4.remove(D)
                            for E in l4:
                                l5 = list()
                                l5 += l4
                                l5.remove(E)
                                for F in l5:
                                    l6 = list()
                                    l6 += l5
                                    l6.remove(F)
                                    for G in l6:
                                        l7 = list()
                                        l7 += l6
                                        l7.remove(G)
                                        for H in l7:
                                            l8 = list()
                                            l8 += l7
                                            l8.remove(H)
                                            for I in l8:
                                                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:
                                                    print('''
                                                    _____________
                                                    |_{0}_|_{1}_|_{2}_|
                                                    |_{3}_|_{4}_|_{5}_|
                                                    |_{6}_|_{7}_|_{8}_|
                                                    '''.format(A, B, C, D, E, F, G, H, I))
    
    def main():
        ninePaper = NinePaper()
        ninePaper.run()
    
    
    if __name__ == '__main__':
        main()

    结果:

      _____________
                        |_A_|_B_|_C_|
                        |_D_|_E_|_F_|
                        |_G_|_H_|_I_|
                        A, B, C, D, E, F, G, H, I 必须是1-9数字,且不能重复
                        所有的行,列,对角线的和都为15
            
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

                                                    _____________
                                                    |_2_|_7_|_6_|
                                                    |_9_|_5_|_1_|
                                                    |_4_|_3_|_8_|
                                                    

                                                    _____________
                                                    |_2_|_9_|_4_|
                                                    |_7_|_5_|_3_|
                                                    |_6_|_1_|_8_|
                                                    

                                                    _____________
                                                    |_4_|_3_|_8_|
                                                    |_9_|_5_|_1_|
                                                    |_2_|_7_|_6_|
                                                    

                                                    _____________
                                                    |_4_|_9_|_2_|
                                                    |_3_|_5_|_7_|
                                                    |_8_|_1_|_6_|
                                                    

                                                    _____________
                                                    |_6_|_1_|_8_|
                                                    |_7_|_5_|_3_|
                                                    |_2_|_9_|_4_|
                                                    

                                                    _____________
                                                    |_6_|_7_|_2_|
                                                    |_1_|_5_|_9_|
                                                    |_8_|_3_|_4_|
                                                    

                                                    _____________
                                                    |_8_|_1_|_6_|
                                                    |_3_|_5_|_7_|
                                                    |_4_|_9_|_2_|
                                                    

                                                    _____________
                                                    |_8_|_3_|_4_|
                                                    |_1_|_5_|_9_|
                                                    |_6_|_7_|_2_|
                                                  

    3.对/etc/passwd文件进行排序

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time    :2017/12/27 15:52
    # @Author  :huangdongju
    # @File    :3.py
    import codecs
    import os
    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()

    passwd文件:

    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
    saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

    排序后的结果:

    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

    4.消息队列与线程的复习

      消息队列是为了防止消息丢失,或者是调用方不需要一直等待响应方的结果。程序代码如下:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time    :2017/12/30 13:41
    # @Author  :huangdongju
    # @File    :demon1.py
    #消息队列与多进程的结合
    import codecs
    import time
    from threading import Thread
    from queue import Queue
    
    # 创建一个生产者类,通过多线程的方式进行传送
    class Produce(Thread):
    
        # 创建构造器,创建构造器之后的第一步为继承,继承Produce
        def __init__(self,queue):
            super(Produce,self).__init__()
            self.fileName = "../online/passwd"
            self.fileList = list()
            self.queue = queue
    
    
        # 重写run方法,在run方法中打开文件
        def run(self):
            with codecs.open(self.fileName) as f :
                self.fileList += f.readlines()
            for line in self.fileList:
                self.queue.put(line)
    
    
    # 创建消费者类
    class Consumer(Thread):
        def __init__(self,queue):
            self.queue = queue
            super(Consumer,self).__init__()
            self.newPasswd = "newpasswd.txt"
            self.fileList = list()
            self.stat = 1
    
        # 消息队列中10秒钟还没有新的数据,则断开连接。在等待的10的过程中,如果在第五秒的时候有数据
        # 传输进来,则这10秒的计时应该又从0开始
        def run(self):
            while 1:
                if self.queue.empty():
                    time.sleep(2)
                    self.stat += 1
                    if self.stat == 5:
                        break
                else:
                    self.stat = 1
                    data = self.queue.get()
                    self.fileList.append(data)
    
            with codecs.open(self.newPasswd,'w') as f:
                f.writelines(self.fileList)
    
    def main():
        q = Queue()
        produce = Produce(q)
        consumer = Consumer(q)
        produce.start()
        consumer.start()
    
    if __name__ == '__main__':
        main()

      执行结果为,在当前目录下生产一个新的文件newpasswd.txt

    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
    sync:x:5:0:sync:/sbin:/bin/sync
    shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    halt:x:7:0:halt:/sbin:/sbin/halt
    mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/cache/rpcbind:/sbin/nologin
    saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
  • 相关阅读:
    WebGL_0008:支持移动端的控制台调试工具
    调整两数组元素使得两数组差值最小
    集五福
    打印机顺序打印
    子弹分发
    字符串分割
    乐观锁、悲观锁
    字符串去重
    数组最后剩下的数字
    shell常用工具
  • 原文地址:https://www.cnblogs.com/huangdongju/p/8126555.html
Copyright © 2020-2023  润新知