"" "Title: Enter three integers x, y, Z, please put these three numbers from small to large output. "" "Def Inputint (i):" "" Enter and determine whether the input is an integer, not re-enter, is the input integer is returned:p Aram I:: return: "" "while true:a = in Put ("Integer%d:"% i) if A.isdigit (): a = Int (a) break else:print ("Input error, please reenter") return adef Sort1 (arr): "" "Sort output with array:p Aram arr:: Return:" "" Print ("Output One", end= ":") Arr.sort () Print (ARR) def sort2 (arr): "" "direct comparison of one one:p Aram arr:: Return:" "" Print ("Output two", end= ":") A = Arr[0] If a > arr[1]: b = A A = arr[1] else:b = arr[1] If a > arr[2]: c = a else: c = arr[2] Print (A, end= ",") if B > C:print (C, end= ",") print (b) Else:print (b, End = ",") print (c) def sort3 (arr): "" "using the sorted function, sort the dictionary calculation, this function is very unaccustomed to the result of sorted (d, Key=d.get) is actually D's key list Sor Ted (...) Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list you can see that the first parameter is an iterative object, and the following parameters areWith the default values, the focus is described as follows: 1, CMP, compare the function, this has two parameters, the value of the parameter is removed from the iterator object, the function must abide by the rule is, greater than return 1, less than return-1, is equal to return 0 2, key, is mainly used to compare the elements, only one parameter, The parameters of the specific function are taken from the iterator object, specifying that an element in the object can be iterated to sort 3, reverse, reversed, and not reversed by default:p Aram Arr:: Return: "" "Print (" Output three ", end=": ") d = {1:arr[0], 2:arr[1], 3:arr[2]} for W in sorted (d, Key=d.get): Print (d[w], end=", ") def SORT4 (arr): "" "Write yourself a bubble sort, long time no written:p Aram Arr:: Return:" "" Print ("\ n Output four", end= ":") for I in range (0, Len (arr)): For j in range (i + 1, len (arr)): if arr[i] > arr[j]: a = arr[i] arr[i] = a[j ] a[j] = a print (arr) def sort5 (arr): "" Calculates:p with multiple assignments of one variable Aram arr:: Return: "" "Print ( "Output Five", end= ":") if arr[0] > arr[1]: arr[0], arr[1] = arr[1], arr[0] if arr[0] > arr[2]: arr[0], ARR[2] = arr[2], arr[0] if arr[1] > arr[2]: arr[1], arr[2] = arr[2], arr[1] Print (arr) def sort6 (arr): "" "Use the Min function to lose:p ARAM arr:: Return: "" "Print (" Output six ", end=": ") while Len (arr) > 0:a = min (arr) Arr.remove (a) If Len (arr) = = 0:print (a) else:print (A, end= ",") def Answer (): "" "first determine if the input is an integer and then row Sequential output: return: "" "arr = [] for I in range (1, 4): Arr.append (Inputint (i)) Sort1 (arr) sort2 (arr) Sort3 (arr) sort4 (arr) sort5 (arr) sort6 (arr) answer ()
Python Learning-Exercises (5)