• 100+Python挑战性编程练习系列 -- day 22


    Question 87

    请编写一个程序,计算并打印控制台输入的字符串中每个字符的个数。
    例如:如果将以下字符串作为程序的输入:
    abcdefgabc
    然后,程序的输出应该是:
    a,2
    c,2
    b,2
    e,1
    d,1
    g,1
    f,1

    方法1:

    import string
    
    s = input()
    for letter in string.ascii_lowercase:
        cnt = s.count(letter)
        if cnt > 0:
            print("{},{}".format(letter,cnt))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    方法2:

    s = 'abcdefgabc'
    for i in sorted(set(s)):
        print(f'{i}, {s.count(i)}')
    
    • 1
    • 2
    • 3

    Question 88

    请编写一个程序,接受控制台的字符串,并以相反的顺序打印出来。
    示例:如果将以下字符串作为程序的输入:*
    rise to vote sir
    然后,程序的输出应该是:
    ris etov ot esir

    s = input()
    s = ''.join(reversed(s))
    print(s)
    
    • 1
    • 2
    • 3

    Question 89

    请编写一个程序,从控制台接受一个字符串,并打印具有偶数索引的字符。
    例如:如果将以下字符串作为程序的输入:
    H1e2l3l4o5w6o7r8l9d
    然后,程序的输出应该是:
    Helloworld

    s = "H1e2l3l4o5w6o7r8l9d"
    s = [ s[i] for i in range(len(s)) if i%2 ==0 ]
    print(''.join(s))
    
    • 1
    • 2
    • 3

    Question 90

    请写一个程序来打印[1,2,3]的所有排列

    from itertools import permutations
    
    def permuation_generator(iterable):
        p = permutations(iterable)
        for i in p:
            print(i)
    
    
    x = [1,2,3]
    permuation_generator(x)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Question 91

    写一个程序来解决一个经典的中国古代难题:我们在一个农场的鸡和兔子中数出35个头和94条腿。我们有多少只兔子和多少只鸡?

    方法1:

    def solve(numheads,numlegs):
        ns='No solutions!'
        for i in range(numheads+1):
            j=numheads-i
            if 2*i+4*j==numlegs:
                return i,j
        return ns,ns
    
    numheads = 35
    numlegs = 94
    solutions=solve(numheads,numlegs)
    print(solutions)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    方法2:

    import itertools
    
    def animal_counter(lst):
        chickens = 0	
        rabbits = 0
        for i in lst:
            if i == 2:
                chickens += 1
            elif i == 4:
                rabbits += 1
        print(f"Number of chickens is {chickens}\nNumber of rabbits is {rabbits}")
    
    
    def animal_calculator(total_legs, total_heads, legs_of_each_species):
        combinations = itertools.combinations_with_replacement(legs_of_each_species, total_heads)
        correct_combos = []
        for i in list(combinations):
            if sum(i) == total_legs:
                correct_combos.append(i)
        print(correct_combos)
        for i in correct_combos:
            animal_counter(i)
    
    animal_calculator(94, 35, legs_of_each_species=[2,4])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    【LeetCode-中等】128. 最长连续序列(详解)
    Electron常见问题 63 - sentry上传自定义pdb符号表
    计算机毕业设计ssm+vue基本微信小程序的小学生兴趣延时班预约小程序
    C++多线程基础
    Linux 主机之间无密访问
    SQL按照id集合顺序返回
    JavaScript基础之七JavaScript函数的使用
    web前端开发Vue面试题记录
    浅析linux 内核 高精度定时器(hrtimer)实现机制(一)
    常驻巨噬细胞诱导的纤维化在胰腺炎性损伤和PDAC中具有不同的作用
  • 原文地址:https://blog.csdn.net/qq_42034590/article/details/130744794