Classic examples of python

Posted by wtfsmd on Thu, 16 Dec 2021 18:57:21 +0100

[procedure 11]
Title: print out all the "daffodils". The so-called "daffodils" refers to a three digit number, and the sum of each digit cube is equal to the number
Analysis: judge each three digit in turn. Its single digit cube plus ten digit cube plus hundred digit cube is equal to itself
Get single digits: I% 10
Get ten digits: I / / 10% 10
Get hundreds: I / / 100% 10

print('The number of daffodils is:', end=' ')
for i in range(100, 1000):
    if (i // 100) ** 3 + (i // 10 % 10) ** 3 + (i % 10) ** 3 == i:
        print(i, end=' ')

[procedure 12]
Topic: decompose a positive integer into prime factors. For example, enter 90 and print out 90 = 2 * 3 * 3 * 5

num = int(input('Please enter a positive integer:'))
print(f'{num} =', end=' ')

for i in range(2, num+1):
    while num != i:
        if num % i == 0:
            print(str(i), end=' ')
            print('*', end=' ')
            num = num // i
        else:
            break
            
print('%d' % num)

[procedure 13]
Title: use the nesting of conditional operators to complete this question: students with academic achievement > = 90 points are represented by A, those between 60-89 points are represented by B, and those below 60 points are represented by C.

score = int(input('Enter grade:'))
if score < 0 or score > 100:
    print('The score entered is invalid!')
elif score >= 90:
    print('The student's grade is: A')
elif score >= 60:
    print('The student's grade is: B')
else:
    print('The student's grade is: C')

[procedure 14]
Title: input a line of characters and count the number of English letters, spaces, numbers and other characters.

s = input('Enter a line of characters:')
count_c = 0
count_n = 0
count_s = 0
count_o = 0

for i in s:
    if i.isalpha():
        count_c += 1
    elif i.isdigit():
        count_n += 1
    elif i.isspace():
        count_s += 1
    else:
        count_o += 1

print(f'There are English letters in this string of characters {count_c} Number, number {count_n} Spaces {count_s} Characters, other characters {count_o} One.')

[procedure 15]
Title: find the value of s=a+aa+aaa+aaaa+aa... A, where a is a number. For example, 2 + 22 + 222 + 2222 + 22222 (at this time, a total of 5 numbers are added), and the addition of several numbers is controlled by the keyboard.

n = input('Enter a number:')  # Is a in the title
num = int(input('Enter a positive integer:'))  # Control the addition of several numbers
lst = []
print('s = ', end='')

for i in range(1, num+1):
    if i != num:
        print(n * i, end=' ')
        print('+', end=' ')
        lst.append(int(n * i))
    else:
        print(n * i, end=' ')
        lst.append(int(n * i))

print('=', end=' ')
print(sum(lst))

[procedure 16]
Title: if a number is exactly equal to the sum of its true factors, this number is called "perfect". For example, 6 = 1 + 2 + 3
Program to find all completions within 1000.

print('1000 The perfections within are:', end=' ')
for i in range(1, 1001):
    lst = []
    for j in range(1, i+1):
        if i == j:
            if i == sum(lst):
                print('%d' % sum(lst), end=' ')
        elif i % j == 0:
            lst.append(j)

[procedure 17]
Title: a ball falls freely from a height of 100 meters and jumps back to half of the original height after each landing; How many meters will it pass on the 10th landing? How high is the 10th rebound?

s = 100
lst = [100]
for i in range(1, 11):
    s -= s / 2  # Height of rebound after each fall
    lst.append(s)
lst.pop()
print('On the tenth landing, a total of %f rice' % sum(lst))
print('The height of the tenth rebound is %f rice' % s)

[procedure 18]
Topic: monkeys eat peaches: monkeys pick several peaches on the first day and eat half of them immediately. They don't enjoy it, so they eat another one; The next morning, he ate half of the remaining peaches and another one. After that, I ate the remaining half and one every morning. When I wanted to eat again on the 10th morning, there was only one peach left. How many peaches did you pick on the first day?

n = 1
for i in range(1, 10):
    n = (n + 1) * 2
print(n)

[procedure 19]
Title: two table tennis teams compete with three players each. Team a consists of a, B and c, and team B consists of x, y and Z. Lots have been drawn to decide the list of matches. The players were asked about the list of matches. A says he doesn't compete with x, c says he doesn't compete with x and Z. please program to find out the list of players of the three teams.

lst1 = ['c', 'a', 'b']
lst2 = ['x', 'y', 'z']
dict = {}
for i in lst1:
    for j in lst2:
        if i == 'c' and j != 'x' and j != 'z':
            dict['c'] = j
            lst2.remove(j)
            break
        elif i == 'a' and j != 'x':
            dict['a'] = j
            lst2.remove(j)
            break
        else:
            dict['b'] = j
print('List of players of the three teams:{}'.format(dict))

[procedure 20]
Title: print the following pattern (diamond)

for i in range(1, 8, 2):
    s = '*' * i
    print('{:^7}'.format(s))
for i in range(5, 0, -2):
    s = '*' * i
    print('{:^7}'.format(s))

Topics: Python Algorithm