Python Practice Example 68

Python 100例Python 100 Examples

Problem:There are n integers, moving each of the preceding numbers back by m positions, and the last m numbers become the first m numbers.

Program analysis:None.

Program source code:

Example

#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': n = int(raw_input('Integer n is:\n')) m = int(raw_input('The number of positions to move back m is:\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(raw_input('Enter a number:\n'))) print 'Original list:',number move(number,n,m) print 'After moving:',number

The output result of the above example is:

整数 n 为:
8
向后移 m 个位置为:
5
输入一个数字:
2
输入一个数字:
8
输入一个数字:
6
输入一个数字:
1
输入一个数字:
78
输入一个数字:
45
输入一个数字:
34
输入一个数字:
2
原始列表: [2, 8, 6, 1, 78, 45, 34, 2]
移动之后: [1, 78, 45, 34, 2, 2, 8, 6]

Python 100例Python 100 Examples

Other extensions