Python practice example (12)

67. Input array: exchange the largest with the first element, exchange the smallest with the last element, and output array.

#python3.7

def inp(numbers):
    for i in range(6):
        numbers.append(int(input('Enter a number:\n')))
p = 0

def arr_max(array):
    max = 0
    for i in range(1, len(array) - 1):
        p = 1
        if array[p] > array[max] : max = p
    k = max
    array[0], array[k] = array[k], array[0]

def arr_min(array):
    min = 0
    for i in range(1, len(array) - 1):
        p = 1
        if array[p] < array[min] : min = p
    l = min
    array[5], array[l] = array[l], array[5]

def outp(numbers):
    for i in range(len(numbers)):
        print(numbers[i])

if __name__ == '__main__':
    array = []
    inp(array)
    arr_max(array)
    arr_min(array)
    print('Calculation results:')
    outp(array)

 

68. There are n integers, so that the numbers in front of them move backward m positions, and the last m numbers become the first m numbers.

#python3.7

if __name__ == '__main__':
    n = int(input('integer n For:\n'))
    m = int(input('Move backward m Locations are:\n'))

    def move(array, n, m):
        array_end = array[n -1]
        for i in range(n - 1, -1, -1):
            array[i] = array[i - 1]
        array[0] = array_end
        m -= 1
        if m > 0: move(array, n, m)

    number = []
    for i in range(n):
        number.append(int(input('Enter a number:\n')))
    print('Original list:', number)

    move(number, n, m)

    print('After move:', number)

 

69. There are n people in a circle, and the numbers are arranged in sequence. Report from the first person (from 1 to 3). When the person who reports to 3 exits the circle, he asks what number he left behind.

#python3.7

if __name__ == '__main__':
    nmax = 50
    n = int(input('Please enter the total number of people:'))
    num = []
    for i in range(n):
        num.append(i + 1)

    i = 0
    k = 0
    m = 0

    while m < n - 1:
        if num[i] != 0 : k += 1
        if k == 3:
            num[i] = 0
            k = 0
            m += 1
        i += 1
        if i == n : i = 0

    i = 0
    while num[i] == 0 : i += 1
    print(num[i])

 

70. Write a function, find the length of a string, input the string in the main function, and output its length.

#python3.7

if __name__ == '__main__':
    s = input('Please input a string:\n')
    print('The string has %d characters.' % len(s))

 

71. Write the input() and output() functions to input and output the data records of 5 students.

N = 3
#stu
# num : string
# name : string
# score[4]: list
student = []
for i in range(5):
    student.append(['','',[]])
 
def input_stu(stu):
    for i in range(N):
        stu[i][0] = raw_input('input student num:\n')
        stu[i][1] = raw_input('input student name:\n')
        for j in range(3):
            stu[i][2].append(int(raw_input('score:\n')))
 
def output_stu(stu):
    for i in range(N):
        print('%-6s%-10s' % ( stu[i][0],stu[i][1] ))
        for j in range(3):
            print('%-8d' % stu[i][2][j])
 
if __name__ == '__main__':
    input_stu(student)
    print(student)
    output_stu(student)

 

72. Create a linked list.

#python3.7

if __name__ == '__main__':
    ptr = []
    for i in range(5):
        num = int(input('Please input a number:\n'))
        ptr.append(num)
    print(ptr)

 

 

 

reference material:

Python 100 cases

Tags: Python

Posted on Mon, 02 Dec 2019 15:49:37 -0800 by cosmos33