Python rookie study notes: combat: Number Sequencing

Examples of practice Python

input three integers x, y, z, please these three numbers small to large outputs.
(Source: https: //www.runoob.com/python/python-exercise-example5.html)
After getting this question, I will use the most stupid way to write it out, and optimized.


'' '
X = int ( INPUT ( ' Enter an integer X-: ' ))
Y = int ( INPUT ( ' Enter an integer the Y: ' ))
Z = int ( INPUT ( ' Enter an integer the Z: ' ) )
IF X <= Y:
    IF Y <= Z:
        Print (X , Y , Z)
    elif Z <= Y:
        IF X <= Z:
            Print (X , Z , Y)
        the else :
            Print (Z , X , Y )
IF X> = Y:
    if z <= y:
        print(z,y,x)
    elif z >= y:
        if x <= z:
            print(y,x,z)
        else:
            print(y,z,x)

'''


But clearly, this method is only relative comparison between the numbers seem a bit low the


So I used another way: to establish a list of selected sorting method to deal with


'''

X = int ( INPUT ( 'Enter a digital X-:' ))
Y = int ( INPUT ( 'Please enter a number the Y:' ))
Z = int ( INPUT ( 'Please enter a number the Z:' ))
Array = [the X- , the y- , z] # beginning of the input, define a list

DEF bubble_sort ():
    for IDX in the Range ( len (Array)): # create two cycles, so that the number of columns in one by one to compare
        for J in the Range (+ IDX . 1 ,len (Array)):                    
            IF Array [IDX]> Array [J]:
                Array [IDX] , Array [J] = Array [J] , Array [IDX] where # is for digital switching, forward of the small number go (bubble)
    return Array # function that returns a value

bubble_sort () # call the function, the function of the internal code to run again

for i in Array:                                             

# One print out the contents of the column, where the number of columns may be printed directly print (array) or the print function Print (bubble_sort)
    Print (I , End = '' )

'''


Here I used a loop (I call this block of statements written in a function and can not define, personal habits)


Cycle over and over again so that the adjacent two numbers compared to the small number top surface


This cycle of it, it has had another idea, also known as bubble sort


'' '
X = int ( INPUT ( ' Enter an integer X-: ' ))
Y = int ( INPUT ( ' Enter an integer the Y: ' ))
Z = int ( INPUT ( ' Enter an integer the Z: ' ) )
Array = [X , Y , Z]


def bubble_flag(list):
    length = len(list)
    for index in range(length):
        for i in range(1,length - index):
            if list[i-1] > list[i]:
                list[i] , list[i-1] = list[i-1] , list[i]
    return list


bubble_flag(array)


for i in array:
    print(i,end=' ')

'''

Here we will see, in fact, no different to the previous code (select sorting method), but bubble sort is the big numbers put back (I still feel called to sink method is more appropriate)


I write to you for instance is already beginning to complete the task, but I was not satisfied




Improved digital input


The first method of operation but inevitably there is a small amount of drawbacks: If you have hundreds or even tens of thousands of numbers?


Obviously, it can not meet, so I choose between two methods of re-processing, the number of digits in front of me to write a function package


'''

DEF sort_input_MoreNumber (List):
    Array = List
    Number The = int ( the INPUT ( 'Do you want to enter a few numbers:' ))
    for i in the Range (Number The):
        the X-= int ( the INPUT ( 'Please enter a number to add to the sort number of columns: ' ))
        array.append (X)
    return Array

'''

At this time only need to define an empty list, and then referencing function can be achieved


However, this method is not very good, when the list of numbers too much, when you type may be entered incorrectly, and so have to go look at a number of the total number of digital


In this time we can use another way to input when entered by the user to enter a cycle can see their first few numbers, and infinite loop


And exit the loop when the input QUIT sort, I will not give code


Finally, a sorting method, this sorting method is more efficient for the future, but for starters, I have written can exercise thinking


Therefore, this method is not recommended for beginners


'''

list.sort()

'''

Yes, very briefly, this is the Python built-in functions, allows us to develop efficient and faster, no need to write code for a lengthy


This, this section notes ended


end

Guess you like

Origin blog.51cto.com/14472763/2424659