Python Practice Example 67

Python 100例Python 100 Examples

Topic:Input an array, swap the largest with the first element, swap the smallest with the last element, and output the array.

Program analysis:None.

Program source code:

Example

#!/usr/bin/python # -*- coding: UTF-8 -*- def inp(numbers): for i in range(6): numbers.append(int(raw_input('Enter a number:\n'))) p = 0 def arr_max(array): max = 0 for i in range(1,len(array) - 1): p = i 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 = i 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) # Input 6 numbers and put them into an array arr_max(array) # Get the largest element and swap it with the first element arr_min(array) # Get the smallest element and swap it with the last element print 'Calculation result:' outp(array)

The output result of the above example is:

输入一个数字:
1
输入一个数字:
2
输入一个数字:
3
输入一个数字:
7
输入一个数字:
9
输入一个数字:
8
计算结果:
9
2
3
7
8
1

Python 100例Python 100 Examples

Other extensions