• 五、python的练习题


    1、输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/5 17:53
    # @Author  : chenjiahe
    # @File    : count.py
    
    #用isdigit函数判断是否数字
    #用isalpha判断是否字母
    #用isalnum判断是否数字和字母的组合
    while 1:
        strings = input("Please input a strings(quit will exit): ")
        alpha, dig, space, other  = 0, 0, 0, 0
        if strings.strip() == "quit":
            exit(1)
    
        for i in strings:
            if i.isalpha():
                alpha += 1
            elif i.isdigit():
                dig += 1
            elif i.isspace():
                space += 1
            else:
                other += 1
        print("alpha:{0}".format(alpha))
        print("dig:{0}".format(dig))
        print("space:{0}".format(space))
        print("other:{0}".format(other))

    执行结果

    2、99乘法表

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/5 18:33
    # @Author  : chenjiahe
    # @File    : 9*9.py
    
    for a in range(1,10):
        for b in range(1,a+1):
            print("{0} x {1} = {2}  ".format(a,b,a*b),end='  ')
    
        print()

    结果:

    3、九宫格(数独)

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/6 16:11
    # @Author  : chenjiahe
    # @File    : 九宫格.py
    '''
    九宫格
                          -------------
                          | A | B | C |
                          | D | E | F |
                          | G | H | I |
                          -------------
    所有的横竖斜线加起来都等于15
    '''
    
    number = [1,2,3,4,5,6,7,8,9]
    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 == C+E+G == 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

    结果:

    4、ABCD乘九=DCBA,A=? B=? C=? D=?

    #ABCD乘九=DCBA,A=? B=? C=? D=? 答案: a=1,b=0,c=8,d=9 1089*9=9801
    
    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("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))

    结果:

     5、阶乘

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/6 18:28
    # @Author  : chenjiahe
    # @File    : 阶乘.py
    
    '''
    计算n个阶乘之和
    0! + 1! + 2! …… + n!
    n! = n*(n-1)*(n-2)*(n-1)……*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("计算n的阶乘之和:")
    count = 0
    for i in range(0,int(n)+1):
        count += jc(i)
    print("{0}的阶乘之和是{1}".format(n,count))

    结果:

     6、面试题

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Time    : 2018/6/7 14:07
    # @Author  : chenjiahe
    # @File    : 面试题.py
    
    '''
    用python完成转换序列的字符,有一串字符串,包含只包含四个字母,GCTA,现在需要进行以下转换:
    *'G' ->'C'
    *'C' ->'G'
    *'T' ->'A'
    *'A' ->'U'
    如果序列中有不包含这四个字母的字符,就返回空。举例:
    1.'ACGTGGTCTTAA' -> 'UGCACCAGAAUU'
    2.'ACGTXXXCTTAA' -> ''
    '''
    
    while 1:
        string1 = input("Please input a string only with 'G' 'C' 'T' 'A': ")
        string2 = ''
        length1 = len(string1)
        for i in range(0,length1):
            if ((string1[i] == 'G')):
                string2 = string2 + 'C'
            elif ((string1[i] == 'C')):
                string2 = string2 + 'G'
            elif ((string1[i] == 'T')):
                string2 = string2 + 'A'
            elif ((string1[i] == 'A')):
                string2 = string2 + 'U'
            else:
                print(' ')
                exit(1)
        print("{0}".format(string2))

    结果:

  • 相关阅读:
    [转] Linux下crontab命令的用法
    [转] Try to use one var statement per scope in JavaScript
    [转] 主流JS框架中DOMReady事件的实现
    MySQL 表复制语句
    [转] MySQL中的运算符展示
    [转] mysql分组取每组前几条记录(排名)
    MySQL 连接结果集
    [转] mysql show processlist命令 详解
    [转] mysql 5.0存储过程学习总结
    jquery中使用event.target的几点
  • 原文地址:https://www.cnblogs.com/chenjiahe/p/9144379.html
Copyright © 2020-2023  润新知