zoukankan      html  css  js  c++  java
  • day45-python实例练习

        '''ABCD * 9 = DCBA
        通过计算机的方法,给我们计算出 A= ? B = ?  C = ? D =?  
        A; 1-9
        B: 1-9
        C: 1-9
        D; 1-9
        A != B != C != D
        '''
    
    例子:
    class CountNumber(object):
        '''ABCD * 9 = DCBA
        通过计算机的方法,给我们计算出 A= ? B = ?  C = ? D =?  
        A; 1-9
        B: 1-9
        C: 1-9
        D; 1-9
        A != B != C != D
        '''
        def __init__(self):
            print("ABCD * 9 = DCBA; A !=B !=C !=D")
        def numAdbcd(self):
            for A in range(1,10):
                for B in range(0,10):
                    for C in range(0,10):
                        for D in range(0,10):
                            if (A*1000 + B*100 + C*10 + D) * 9 == (D*1000+C*100+B*10+A):
                                print("{0}{1}{2}{3} *9 = {4}{5}{6}{7}".format(A,B,C,D,D,C,B,A))
                                print("A = {0}, B = {1}, C = {2}, D = {3}".format(A, B, C, D))
    def main():
        countNumber = CountNumber()
        countNumber.numAdbcd()
    
    if __name__ == '__main__':
        main()
    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @time: 2018/1/7 16:10
    # Author: caicai
    # @File: demon3.py
    '''九宫格'''
    
    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("number = {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()
    去一个passwd文件按序号大小进行排序写入新的字典
    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
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
    systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
    systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:998:996:User for polkitd:/:/sbin/nologin
    colord:x:997:995:User for colord:/var/lib/colord:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    unbound:x:996:994:Unbound DNS resolver:/etc/unbound:/sbin/nologin
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    libstoragemgmt:x:995:993:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
    geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin
    setroubleshoot:x:992:989::/var/lib/setroubleshoot:/sbin/nologin
    rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
    qemu:x:107:107:qemu user:/:/sbin/nologin
    radvd:x:75:75:radvd user:/:/sbin/nologin
    chrony:x:991:988::/var/lib/chrony:/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
    pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    gnome-initial-setup:x:990:985::/run/gnome-initial-setup/:/sbin/nologin
    avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    tcpdump:x:72:72::/:/sbin/nologin
    osadmin:x:1000:1000:osadmin:/home/osadmin:/bin/bash
    mysql-proxy:x:989:984:MySQL-Proxy user:/:/sbin/nologin
    wxp:x:1001:1001::/home/wxp:/bin/bash
    nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin
    uwsgi:x:987:982:uWSGI daemon user:/run/uwsgi:/sbin/nologin
    
    
    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)
                print(self.contextList)
        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()
  • 相关阅读:
    Windows下MongoDB安装与PHP扩展
    仿Smarty替换模板标签时遇到的问题
    class ha_innobase: public handler
    类handler
    类Item_field
    函数buf_pool_init_instance
    函数buf_pool_init
    安装php,nginx 带debug
    gdb调试SAPI方式的php
    重启php-fpm
  • 原文地址:https://www.cnblogs.com/wxp997/p/8228345.html
Copyright © 2011-2022 走看看