python practice problem one

    python practice questions

Exercise 1: The user enters a number.
Requirements: # Determine the number of digits.
              # Print each digit and how many times it repeats.
              # Print each digit in sequence, in sequence, tens, hundreds, and print them in sequence.
The implementation code is as follows:
a=str(int(input('Please enter a positive integer')))
if a.lstrip('-'):
    lena = len (a) -1
else:
    lena = len (a)
    print('Number {} is {} digits'.format(a,lena))
for i in a :
    if i =='-':
        continue
    print('Number {} repeated {} times'.format(i,a.count(i)))
for i in reversed(a):
    print('Number: {} is the number of {} digits and print the digits in turn, respectively: {}'.format(a,lena,i))

Effect picture:

#Exercise 2: Find the K-th element in the M-th row of Yanghui's triangle [implementing the search element:]
#Hint: There are M items in the M-th row, and M is a positive integer, so K must not be greater than M
#The number of M in the N-th row , which can be expressed as C(n-1, m-1), which is the number of combinations of M-1 elements taken from n-1 different elements.

The implementation code is as follows:

n=int(input('Please enter the number of lines to display:'))
k=int(input('Please enter the index number of the element to be displayed:'))
if n<= k:
    print('The element with the index number {} in the line {} you are looking for is not found!'.format(n,k))
else:
    for i in range(n):
        newline = [1]
        if i ==0:  
            continue
        for j in range(i-1):
            val = for [j] + for [j + 1]
            newline.append(val)
        newline.append(1)
        pre = newline
    print(pre)
    newline[k-1]=newline[k]
    print('The element with the index number {} of the line {} you are looking for is {}.'.format(n,k,newline[k]))

Effect picture:

Exercise 3: Given a 3X3 matrix, find the corresponding transposed matrix. (The same is true for matrix 2X2.)

The implementation code is as follows:

#Given a 3X3 matrix, find its transpose matrix.
m=[[1,2,3],[4,5,6],[7,8,9]]
print('{}{}{}'.format(m[0],m[1],m[2]))
for i in range(len(m)):
    for j in range(i):
        m[i][j],m[j][i] = m[j][i],m[i][j]
print('{}{}{}'.format(m[0],m[1],m[2]))
The effect diagram is as follows:

 

Exercise 4: Generate 10 numbers at random.
Requirements:
# The range of each number is [1, 20]
# How many numbers are repeated? What are they?
# How many numbers are there that are not repeated? What are they?

# Example: 11, 7, 5, 11, 6, 7, 4, of which 2 numbers 7 and 11 are repeated, and 3 numbers 4, 5, and 6 are not repeated.

The implementation code is as follows:

method 1:

import random #Load function
#Generate a list.
lst_a = [1]*10                      
for i in range(10):                     
    lst_a[i]=random.randint(1,20)     
print('Random 10 positive integers, respectively: {}'.format(lst_a))                        
#Count the number of occurrences of elements in the list lst_a.
sudmsf = [0]*20
for i in range(10):
    sudmsf[lst_a[i]-1] += 1
#print('lst_a element {} corresponds to the number of occurrences{}'.format(lst_a,sudmsf))
#List the elements that appear multiple times and single times by judging.
seta = set()
setb = set()
for j in range(20):
    if sudmsf[j] >= 2:
        seta.add (j + 1)
        alongeth = len (seta)
    if sudmsf[j] == 1:
        setb.add(j+1)
        blongeth = len (setb)
print('Print there are {} elements that appear multiple times, namely: {}'.format(alongeth,seta))
print('Print there are {} elements that appear once, respectively: {}'.format(blongeth,setb))

The effect diagram is as follows:

Method 2: (Learn colleagues code)

import random

lst_num = []
for i in range(10):
    num = random.randint (1,20)
    lst_num.append(num)
    lst_num.sort() # for the following results to look more obvious
print('The random number is: {}'.format(lst_num))

lst_zero = [0] * 20 # used to calculate whether the number is repeated
lst_same_num = [] # used to store repeated numbers
lst_diff_num = [] # used to store unique numbers
for j in range(20):
    lst_zero[j] = lst_num.count(j+1)
    if lst_zero[j] > 1: # filter duplicate numbers
        lst_same_num.append(j+1)
    elif lst_zero[j]: # filter unique numbers
        lst_diff_num.append(j+1)
print('The same number have {}. They are {}.'.format(len(lst_same_num),lst_same_num))
print('The different number have {}. They are {}.'.format(len(lst_diff_num),lst_diff_num))

The effect diagram is as follows:

                                                                                                                                                        by phantom


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325492194&siteId=291194637