第五次課

1. 判斷一個字符串數字有多少個、字母有多少個,空格有多少、其餘有多少。

#! /usr/bin/python
# - * - 編碼: utf-8 - * -
# @做者: Gavin-zhang

'''

判斷一個字符串數字有多少個
字母有多少個
空格有多少個
其餘字符

'''


while 1:
    strings = input("Please input a string(input quit to exit): ")
    alpha, dig, space, other = 0, 0, 0, 0
    if strings.strip() == "quit":
        exit(1)
    for i in strings:
        if i.isdigit():
            dig += 1
        elif i.isspace():
            space += 1
        elif i.isalpha():
            alpha += 1
        else:
            other += 1
    print("alpha = {0}".format(alpha))
    print("dig = {0}".format(dig))
    print("space = {0}".format(space))
    print("other = {0}".format(other))

Please input a string(input quit to exit): aaa 111 @#$
alpha = 3
dig = 3
space = 2
other = 3
Please input a string(input quit to exit): quitpython

Process finished with exit code 1git

2. ABCD乘9=DCBA,A=? B=? C=? D=?

#! /usr/bin/env python
# encoding: utf-8
# @author: Gavin_zhang
# @time: 2018/4/11 下午10:27
# @file: demon2.py
'''
ABCD乘9=DCBA,A=? B=? C=? D=? 答案:a=1,b=0,c=8,d=9      1089*9=9801
'''

for A in [1]:
    for B in range(0, 10):
        for C in range(0, 10):
            for D in [9]:
                if ((1000* A + 100*B + 10*C + D)*9 == 1000*D + 100*C + 10*B + A ):
                    print("A = {0}".format(A))
                    print("B = {0}".format(B))
                    print("C = {0}".format(C))
                    print("D = {0}".format(D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A, B, C, D))

A = 1
B = 0
C = 8
D = 9
1089x9=9801app

3. 九宮格

#! /usr/bin/env python
# encoding: utf-8
# @author: Gavin_zhang
# @time: 2018/4/11 下午10:42
# @file: demon3.
'''

九宮格
1-9
                -------------
                | A | B | C |
                | D | E | F |
                | G | H | I |
                -------------
全部的橫豎斜線加起來都等於15
A  1-9
B  1-9 除A
C  1-9 除A,B
'''

number = list()
for i in range(1, 10):
    number.append(i)
print(number)


count = 1
for A in number:
    a = number.copy()
    a.remove(A)
    for B in a:
        b = a.copy()
        b.remove(B)
        for C in b:
            c = b.copy()
            c.remove(C)
            for D in c:
                d = c.copy()
                d.remove(D)
                for E in d:
                    e = d.copy()
                    e.remove(E)
                    for F in e:
                        f = e.copy()
                        f.remove(F)
                        for G in f:
                            g = f.copy()
                            g.remove(G)
                            for H in g:
                                h = g.copy()
                                h.remove(H)
                                for I in h:
                                    if (A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == G+E+C == 15):
                                        print('''
                                        第{9}種例子
                                        -------------
                                        | {0} | {1} | {2} |
                                        | {3} | {4} | {5} |
                                        | {6} | {7} | {8} |
                                        -------------'''.format(A,B,C,D,E,F,G,H,I,count))
                                        count += 1

[1, 2, 3, 4, 5, 6, 7, 8, 9]ui

                                        第1種例子
                                        -------------
                                        | 2 | 7 | 6 |
                                        | 9 | 5 | 1 |
                                        | 4 | 3 | 8 |
                                        -------------編碼

                                        第2種例子
                                        -------------
                                        | 2 | 9 | 4 |
                                        | 7 | 5 | 3 |
                                        | 6 | 1 | 8 |
                                        -------------spa

                                        第3種例子
                                        -------------
                                        | 4 | 3 | 8 |
                                        | 9 | 5 | 1 |
                                        | 2 | 7 | 6 |
                                        -------------code

                                        第4種例子
                                        -------------
                                        | 4 | 9 | 2 |
                                        | 3 | 5 | 7 |
                                        | 8 | 1 | 6 |
                                        -------------orm

                                        第5種例子
                                        -------------
                                        | 6 | 1 | 8 |
                                        | 7 | 5 | 3 |
                                        | 2 | 9 | 4 |
                                        -------------ip

                                        第6種例子
                                        -------------
                                        | 6 | 7 | 2 |
                                        | 1 | 5 | 9 |
                                        | 8 | 3 | 4 |
                                        -------------utf-8

                                        第7種例子
                                        -------------
                                        | 8 | 1 | 6 |
                                        | 3 | 5 | 7 |
                                        | 4 | 9 | 2 |
                                        -------------

                                        第8種例子
                                        -------------
                                        | 8 | 3 | 4 |
                                        | 1 | 5 | 9 |
                                        | 6 | 7 | 2 |
                                        -------------
4.  0! +1! +2! + 3! +4! + 5! + n! 1 + 1 + 2 + 6 + …… + n*(n-1)*(n-2)……*1

#! /usr/bin/env python
# encoding: utf-8
# @author: Gavin_zhang
# @time: 2018/4/11 下午10:56
# @file: demon4.py

'''
0! +1! +2! + 3! +4! + 5! + n!
1 + 1 + 2 + 6 + …… + n*(n-1)*(n-2)……*1
'''

def jc(n):
    result = 1
    if n == 0:
        return result
    else:
        for i in range(1, n+1):
            result *= i
        return result


n = input("Plese inpurt number n:")
count = 0
for i in range(0, int(n)+1):
    count += jc(i)

print("count = {0}".format(count))

Plese inpurt number n:4
count = 34

5. 編碼

相關文章
相關標籤/搜索