* sort list def que6(): # 6. Enter three integers x, y, z, Form a list , Please put this n Number of output from small to large . #
Program analysis ： The list contains sort method , So make a list of them . li = np.random.randint(-100, 100, size=10) # In situ conversion
li = li.tolist()     # use sort() result li_sort = sorted(li, reverse = False)
print(' use sort method , Rearrange results ：{}'.format(li_sort)) # no need sort method , Write your own sorting method , # Bubble sort def
bubbleSort(m): m = m.copy() for time in range(1, len(m)): for index in
range(len(m) - time): if m[index] > m[index+1]: m[index], m[index+1] =
m[index+1] , m[index] return m # Select sort def selectSort(m): m = m.copy() for
seat_L in range(len(m)-1): for seat_R in range(seat_L+1, len(m)): if m[seat_L]
> m[seat_R]: m[seat_L], m[seat_R] = m[seat_R], m[seat_L] return m #
Insert sort 1（ Internally written functions ）： def insertSort_1(m): result = [] # Single element k Insert list li def to_insert(li,
k): # identifier tab = False # Find insertion position #
The number of cycles should be at least greater than the length of the list +1,None One of them （ Empty list ）, That is to say, there is another card at the end of the card ‘ Empty card ’ for i in range(len(li) + 1): #
Modify identifier , sign ‘ Next loop after traversing ’, That is, in and ‘ Empty card ’ compare if i == (len(li)): tab = True #
If you are on the right li[-1] Comparison complete （ contain ） before , And find the location , That is to compare poker from left to right if not tab and k < li[i]: li.insert(i,
k) break # If the traversal is complete , One more cycle , Namely and ‘ Empty card ’ There is no need to compare , Just replace the cards ‘ Empty card ’ if tab: li.append(k) return li
# Traverse list # result = result[:1] for length in range(len(m)): result =
to_insert(result, m[length]) # print(result,m[length]) return result #
Insert sort 2( Nested loop directly )： def insertSort2(m): m = m.copy() result = m[:1] for index_choose
in range(1, len(m)): # I already have them index_choose Cards , Compare page index_choose+1 Card rules append #
Compare the cards on your opponent one by one , If you compare them all , Add it to the end for index_insert in range(len(result) + 1):
print(result, index_insert,'\n',m, index_choose,'\n\n') if index_insert !=
index_choose and m[index_choose] < result[index_insert] :
result.insert(index_insert, m[index_choose]) break if index_insert ==
index_choose: result.append(m[index_choose]) # print(result, m[index_choose])
return result # print(li) print(' Insert sort ：',insertSort3(li))
print(' Select sort ：',selectSort(li)) print(' Bubble sort ：',bubbleSort(li)) que6()

* Exchange dictionary key values # 1. Exchange elements .\ def que1(): d={1:"one",2:"two"} # method 1 --- Dynamic assignment def
method1(d): d = d.copy() result = {} for k,v in d.items(): result[v] = k return
result # method 2 --- generator def method2(d): d = d.copy() result = {v:k for k,v in
d.items()} return result # method 3 --- Find key by value def method3(d): d = d.copy() # Find value by key
def match(dic, b): return [k for k,v in dic.items() if v == b] #
Mr. Cheng key-None, Revaluation result = {} result = result.fromkeys(d.values()) for k in
result.keys(): result[k] = match(d, k)[0] return result # method 4 --- List to dictionary <
Direct conversion / Dynamic assignment > def method4(d): d = d.copy() key = d.keys() val = d.values() data =
list(zip(key, val)) # method 4-1 result1 = {} for i in range(len(data)):
result1[data[i][1]] = data[i][0] # method 4-2 result2 = dict(zip(val, key)) return
result1, result2 print(' New list dynamic assignment method ：{}'.format(method1(d)))
print(' Generator method ：{}'.format(method2(d))) print(' Key search method ：{}'.format(method3(d)))
print(' A method of transferring dynamic assignment list to dictionary ：{}'.format(method4(d)[0]))
print(' Direct list to dictionary method ：{}'.format(method4(d)[1])) # que1()

* Remove duplicate elements from list # 3. Remove duplicate elements list =[1,2,5,4,1,5,6,8,0,2,5] a =
np.random.randint(-100, 100, size=10) a = a.tolist() def method1(a): a =
a.copy() a = set(a) return a def method2(a): b = a.copy() c = 0 for i in
range(len(a)-1): if b[i+c] in b[:i+c]+b[i+c+1:]: b.pop(i+c) c -= 1 return b
print(' Aggregation method ：',method1(a)) print(' Ergodic method ：',method2(a))

* Output prime number def prime(end): prime_list = [] if end <= 1: print(' Must be greater than 1') else: #
prime_list.append(2) for i in range(2, end+1, 1): count = 0 if i == 2: if i%2
!= 0: prime_list.append(2) else: for m in range(2, i): # Can be divisible , Out of the loop if (i % m)
== 0: # print(i, m) break # Otherwise count +1 else: count += 1 # Whether division is complete or not (0/n) if count ==
i - 2: prime_list.append(i) print(count, i, m) return (prime_list) num =
int(input(' Want to output 2 How much ?')) print(prime(num))

* Print multiplication tables # New multiplication table for i in range(1,10): for j in range(1, i+1):
print('{}×{}={}'.format(i, j, i*j),end="\t") print('\n')

* Print solid and hollow stars # 4. Print triangles def que4(): # Solid triangle def triangle_solid(num): for i in
range(num): tab = False for j in range(i+1): print('*',end='') if j == i: tab =
True if tab: print('\n' ,end = '') # Hollow triangle def hollow_solid(num): for i in
range(num): tab = False for j in range(i + 1): # Judge whether it is the last line if i != num-1: #
Cycle complete , Modify identifier if j == i : tab = True # Whether to print space or not * if (i == j or j == 0):
print('*',end='') else : print(' ',end='') # Last line , Print all asterisks else: print('*',
end='') if tab: print('\n', end='') def hollow_solid_new(num): for i in
range(num): for j in range(i + 1): # Judge whether it is the last line if i != num-1: # Whether to print space or not * if (i
== j or j == 0): print('*',end='') else : print(' ',end='') # Last line , Print all asterisks else:
print('*', end='') print('\n') num = int(input(' How many polygons do you want to print ? Please enter \n'))
triangle_solid1(num) print('\n') hollow_solid(num) return '\n' # que4()

* What day of the year is the judgment def que3(): # 3. Enter the date, month and year , Judge the day of the year ?： # Leap year judgment function def
judge_leap(num): date = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #
( One leap in four years and not a hundred years ) or Four hundred year leap if (num % 4 == 0 and num % 100 != 0) or num % 400 ==0:
date[1] =29 return date # format conversion date = (input(' Please enter a date , The format is as follows ：“2018.02.12”：'))
date_list = (list(map(int, (date.split('.'))))) # Traversal calculation days day = date_list[2] for
i in range(date_list[1]): day += judge_leap(date_list[0])[i]
print('{} month {} The day is {} Year of {} day \n'.format(date_list[1], date_list[2], date_list[0],
day)) # que3()

* Guess the numbers # Guess the numbers again import random def judge_num(num, num_random): if num >
num_random: print('It\'s too big') return 1 elif num < num_random: print('It\'s
too small') return 1 else: print("Congratulation!! That\' right!") return 0 #
Generating random numbers num_start = int(input('Digital lower limit of guess number:\n')) num_end
= int(input('Digital upper limit of guess number:\n')) num_random =
random.randint(num_start, num_end) # Parameter initialization result = 1 # Judge the result i = 0 # Number of cycles
frequency = 3 # Cycle limit times # Total number of guesses , Remaining times print('WARNING: You have【{}】 chances you
guess '.format(frequency), end = '--&&>>--') print('【{}】 chances left
now:\n'.format(frequency - i +1)) while result and i != frequency: # Guess the numbers num =
int(input('Please guess a int_number:\n')) result = judge_num(num, num_random)
i += 1

* Base conversion # Conversion from arbitrary base to decimal def other_to_decimal(hex, num): # Convert integer to list , num_str =
str(num) # map（） take List Elements in object （list type ） Convert to set （set） type num_list = list(map(int,
num_str)) # Reverse list order num_list = num_list[::-1] print(list(map(int, num_str))) #
Get number of digits digit = len(num_list) num_decimal = 0 # accumulation for i in range(digit): numi =
num_list[i] # print(numi, hex**i) num_decimal += numi*(hex**i) # Add the power exponent of each digit
return num_decimal # Decimal to arbitrary base def decimal_to_other(hex, num): # Get number of digits digit =
len(str(num)) num_hex = [] quotient = 1 # be divided by , The remainder is included in the list num_hex while quotient: #
Surplus and quotient quotient = num // hex remainder = num % hex # print(quotient, remainder)
# The remainder is included in the list num_hex.append(remainder) # Quoting for the next cycle num = quotient #
Reverse list order , By slicing and sort() Function can be implemented num_hex = num_hex[::-1] # num_hex.sort(reverse=True) #
If more than decimal , use ASCII Code to letter for i in range(len(num_hex)): if num_hex[i] > 9:
num_hex[i] = chr(int(num_hex[i])+87) # print(num_hex) # List to string result =
(''.join('%s' %m for m in num_hex)) return result Type =
bool(input(" Decimal to any base, please input 1, Any base to decimal, please input 0\n")) if Type: hex =
int(input(" How many base numbers do you need to convert decimal to ? Please enter a positive integer \n")) num = int(input(" The number to be converted is :"))
print(" The conversion result is ：", decimal_to_other(hex, num)) else: hex =
int(input(" How many base numbers need to be converted to decimal ? Please enter a positive integer \n year ")) num = int(input(" The number to be converted is :"))
print(" The conversion result is ：", other_to_decimal(hex, num))

Technology
Daily Recommendation
views 2