theory
Single choice question
1. There are the following Python programs, including lambda functions. After running the program, the output result is?
g = lambda x,y:x*y print(g(2,3))
A,2 B,3 C,6 D,8
2. Run the following program, and the output is?
def dtox(x,base = 2): s = [] while x>0: s.append(x % base) x = x // base return s print(dtox(11))
A,Program error B,1101 C,[1,1,0,1] D,[1,0,1,1]
3. Which of the following is not an advantage of a function?
A,Improve code reuse rate B,Make the program concise and clear C,It is convenient for program modification and expansion D,Code runs faster
4. Is the following description of functions correct?
A,Statements in functions do not change the value of any non global variables B,The parameters passed into the function will be stored in the function in the form of copies C,The name of a function cannot be the same as Python The reserved words of are the same D,Each function must have one return sentence
5. What is the keyword of a custom function in Python?
A,sub B,def C,function D,void
6. Run the following program, and the output is?
def nxs(x): s = 0 while x: s = s * 10 + x % 10 x //= 10 return s print(nxs(106))
A,106 B,610 C,160 D,601
7. After executing the custom function dy written in Python, the output result is?
def dy(x,y): return x>y print(dy(10,20))
A,10 B,20 C,False D,True
8,
There is a kind of function called null function. To construct an empty function, fill the following horizontal line with? ( )
def function name ():
_______
A,None B,return None C,pass D,empty
9. What is the output of the following code?
def fun(a,b): t = a a = b b = t print(a, b) fun(pow(3,2),pow(2,3)) #pow(x,y) returns the value of x**y
A,3 2 B,8 9 C,2 3 D,9 8
10. In the function, return [expression] is used to end the function and selectively return a value to the caller. Sometimes return without expression is equivalent to returning?
A,0 B,None C,1 D,False
11. For the following functions, use f(1,2,3) and f(1,2) to call the statements, and the running results are?
def f(x,z,y=2): print(x+y+z)
A,All 5 B,6 And 5 C,All 6 D,5 And 6
12. What is the print result of the following program?
def demo(item,lis=[]): lis=[] lis.append(item) return lis print(demo('a'),end='') print(demo('b'))
A,['a']['a', 'b'] B,['a']['b'] C,[][] D,[]['b']
13. The function is as follows. Which of the following options will report an error when calling this function?
def showNumber(numbers): for n in numbers: print(n)
A,showNumber([2,4,5]) B,showNumber('abcesf' ) C,showNumber(3.4) D,showNumber((12,4,5))
14. Among the following keywords, which is used to import the third-party library?
A,include B,from C,import D,continue
15. Among the following options, what functions can pip, which is not a third-party library installation tool, achieve?
A,Install a third-party library B,Uninstall installed third-party libraries C,Modify installed third-party libraries D,Query installed third-party libraries
16. As for the advantages and disadvantages of the evaluation algorithm, is the following correct?
A,Just consider whether you get the right answer B,Just consider the execution time of the algorithm C,Just consider the space occupied by the algorithm D,Consider the execution time and space of the algorithm
17. The following procedure is used to calculate the value of item 4 of Fibonacci sequence:
def f(n): if n==1 or n==2: return 1 elif n>2: return f(n-1)+f(n-2) else: return -1 print(f(4))
Excuse me: what kind of algorithm does this solution belong to?
A,induce B,list C,Recurrence D,recursion
18. The famous Fibonacci sequence is 1, 1, 2, 3, 5, 8. That is, except that the first two items are 1, each other item is the sum of the first two items.
The following program calculates the size of the fourth value of the sequence.
n,a,b=4,1,1 if n==1: print("Section%d The number is%d. "%_____(1)______) if n==2: print("Section%d The number is%d. "%(n,b)) if n>2: for i in range(2,n): result=a+b a,b=b,result print("Section%d The number is%d. "%(n,result))
Excuse me: which of the following options is not suitable for the location of the horizontal line (1)?
A,(n, 1) B,(n, a) C,(n, b) D,(a, n)
19. Run the following program, and the output is?
def f(n): if n==1: return 1 else: return f(n-1)+(n-1)*f(n-1) print(f(4))
A,64 B,24 C,4 D,16
20. The known Pell sequence is defined as follows:
What is the value of item 5?
A,29 B,30 C,12 D,32
21. Run the following program, and the output result is?
def Pell(n): if n==1: return 1 if n==2: return 2 if n>=3: return 2*Pell(n-1)+Pell(n-2) print(Pell(4))
A,12 B,4 C,3 D,24
22. Run the following program, and the output result is?
def f(n): if n==1 or n==2: return 1 elif n>2: return f(n-1)+f(n-2) else: return -1 print(f(-2))
A,-2 B,-1 C,error D,1
23. When playing the game of guessing numbers between 1-20, if you use the dichotomy strategy and give the prompt of "big" or "small", how many times can you guess correctly in the worst case?
A,5 B,10 C,15 D,20
24. Which of the following options is not a feature of divide and conquer algorithm?
A,If the scale of the problem is reduced to a certain extent, it can be easily solved B,The solution of the subproblem divided by the problem can be combined with the solution of the problem C,Each sub problem must be decomposed until it cannot be decomposed D,The problem has the property of optimal substructure
25. Which of the following sorting algorithms uses the idea of divide and conquer algorithm?
A,Bubble sort B,Insert sort C,Select sort D,Quick sort
Judgement question
26. The code of a custom function in Python needs to be written before calling the function.
27. When Python defines a function, optional parameters must be written after non optional parameters.
28. When writing a function in Python, you must specify the return value of the function.
29. User defined function is a program written by the user, which generally includes four parts: function name, parameter, return value, function body, etc. Among them, function name and parameters are essential parts.
30. The code in the def block of the user-defined function is not part of the main program, and the operation will skip this code.
31. Algorithm optimization is mainly to reduce redundant data and optimize time complexity and space complexity.
32. Third party libraries are required to use crawler technology to capture website information in Python.
33. For recursion, recursion and regression are indispensable.
34. The recursive algorithm is the same as the recursive algorithm, which is called repeatedly.
35. The general steps of using divide and conquer algorithm to solve problems are decomposition, solution and merging.
Practical operation
First question
Character encryption. Convert the capital letters into 7-bit binary numbers one by one according to their ASCII values, and fill 0 on the far left to convert each letter into 8-bit binary numbers, and then convert 8-bit binary numbers into hexadecimal numbers every four bits. If the plaintext is "CIE", the encrypted ciphertext is "434945". The python program is as follows to improve the code at the dash.
def xtob(n): #Convert integer to binary if n <= 1: return str(n) return xtob(n//2)+ ① mingwen = "CIE" he = '0123456789ABCDEF' rst = '' for c in mingwen: s = "0" + xtob(ord(c)) #Convert plaintext to 8-bit binary number i = 0 data = 0 while i < 8: data =data * 2 + ② if (i + 1) % 4 == 0: rst = rst + he[data] data = 0 ③ print('Ciphertext is:',rst)
Second question
The following functions are implemented by recursive algorithm. Please improve the code at the horizontal line.
def gcd(a,b): m=a%b if m==0: return ① else: a=b ② return ③ print(gcd(12,18))
Question 3
The following program uses divide and conquer algorithm to find the maximum value in the list. Please complete the code with blanks.
#When the subproblem size is less than or equal to 2 def zui_da_zhi(a_list): if ① : if a_list[0]>=a_list[1]: most=a_list[0] else: most=a_list[1] else: most=a_list[0] return most # Divide and conquer algorithm sorting def fen_zhi(init_list): n=len(init_list) if n<=2: #If the scale of the problem is less than or equal to 2, solve it return zui_da_zhi(init_list) #Decomposition (subproblem scale is n/2) left_list,right_list= ② #Recursion, divide and conquer left_max,right_max= ③ #merge return zui_da_zhi([left_max,right_max]) #test data test_data=[12,25,4,47,69,5,4,6,37,89,21] print('The maximum value is:',fen_zhi(test_data))