Latest web development tutorials

Python includes exercises 39

Python 100 Li Python 100 Li

Title: There has been a good array row sequence. Now enter a number, required by the laws of the original plug it into an array.

Program analysis: First, determine if this number is a number greater than the last, and then consider the case number is inserted in the middle, insert the number of this element after the turn after a shift position.

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    # 方法一
    a = [1,4,6,9,13,16,19,28,40,100,0]
    print 'original list is:'
    for i in range(len(a)):
        print a[i]
    number = int(raw_input("insert a new number:\n"))
    end = a[9]
    if number > end:
        a[10] = number
    else:
        for i in range(10):
            if a[i] > number:
                temp1 = a[i]
                a[i] = number
                for j in range(i + 1,11):
                    temp2 = a[j]
                    a[j] = temp1
                    temp1 = temp2
                break
    for i in range(11):
        print a[i]

The above example output is:

original list is:
1
4
6
9
13
16
19
28
40
100
0
insert a new number:
7
1
4
6
7
9
13
16
19
28
40
100

Python 100 Li Python 100 Li