The most detailed Python analysis in the history of Lanqiao Cup (14th session)

1. Multiple choice questions (5 questions in total)

1. (4.0 points) Which of the following options is not a built-in method in the collection? ( )

A. isdisjoint ( )
B. copy ( )
C. string ( )
D. issubset ( )
Answer: C
Analysis:
copy ( ) can copy all elements in the collection and return a shallow copy; issubset () determines whether a set contains other sets, which is equivalent to a>=b; isdisjoint () determines whether two sets are disjoint. A, B, and D are all built-in methods of collections, so C is selected.

2. (4.0 points) It is known that s = "hello python", then s[1: 8] represents ( ).

A. hello py
B. hello p
C. ello py
D. ello p Analysis: The slicing format of the string is s[start index: end index + 1]. The starting point index is 1, and the corresponding letter is e; the starting point index is 7, and the corresponding letter is y, so cut out the string "ello py" and select C.
Answer: C

3. (4.0 points) After executing the print(23 / 10) statement, the output result is ( ).

A. 2
B. 2.3
C. 3
D. 23
Answer: B
Analysis: "/" represents the division operator, and the operation result must be a floating point number. The result of dividing 23 by 10 is 2.3, so choose B.

4. (4.0 points) Which of the following functions can generate random decimals? ( )

A. randint ( )
B. randrange ( )
C. shuffle ( )
D. random ( )
Answer: D
Analysis: randint(a, b) means randomly generating an integer in the range of a~b; randrange(a, b, step ) means randomly generating an integer within the specified step range between a~b; the function of shuffle () is to randomly disrupt the order of list elements. None of A, B, and C have the function of generating random decimals. The random ( ) function is to generate a random floating point number in the range of 0 to 1, so D is selected.

5. (4.0 points) Which of the following descriptions of lists is incorrect ( ).

A. The elements in the list can be accessed using subscripts/indexes
B. The elements in the list can be deleted using the del statement
C. Use The add method can add elements to the list
D. A list is an ordered sequence containing 0 or more object references
Answer: C< a i=5> Analysis: del is a keyword in Python, specially used to perform deletion operations. It can not only delete the entire list, but also delete certain elements in the list. Option B is correct. The method of adding elements to the list is append, so option C is wrong, so choose C.

2. Programming questions (5 questions in total)

Question 1 15.0 points

Problem description:
(Note. No information is allowed to be added in the brackets of the input ( ) input function)
Programming implementation:
Given an integer N (-1000≤N≤1000), output a number 1 greater than N.
For example: N = 5, the number one greater than 5 is 6, then 6 is output.
Input description
Input an integer N (-1000≤N≤1000)
Output description
Output an integer, representing a number 1 greater than N
Sample input
5
Sample output Reference code:
6

n = int(input())
print(n + 1)

Question 2 18.0 points

Problem description:
(Note. No information is allowed to be added in the brackets of the input ( ) input function)
Programming implementation:
Given a positive integer N (10≤N≤10000), output the smallest number among all digits in N.
For example: N = 1182, the numbers on each digit of 1182 are 1, 1, 8, 2 respectively. Among them, the smallest number on the digit is 1, then 1 is output.
Input description
Input a positive integer N (10≤N≤10000)
Output description
Output an integer, representing the smallest number among all digits in N
Sample input
1182
Sample output< /span> Reference code: Method 2: You can use the min () function directly on the string to return the character with the minimum ASCII code (‘0’ < ‘1’ < ‘2’…). Method 1: You can use strings to obtain each digit of a positive integer. After converting each number to an integer type, add it to a list and use the min () function. Analysis
1



#方法一
n = input()
ls = []
for x in n:
	ls.append(int(x))
print(min(ls))

#方法二
n = input
print(min(n))

Question 3 20.0 points

Problem description:
(Note. No information is allowed to be added in the brackets of the input ( ) input function)
Programming implementation:
Given a set of integer data (no more than 100 integers), complete the following operations in order and output the results.
Operation requirements:
1) Convert all negative integers in the data into positive integers;
2) Convert the converted The data is sorted from small to large.
For example:
The integer data is 1, -3, 6, -2. After converting the negative integer into a positive integer, we get 1, 3, 6, 2, The results after sorting the values ​​from small to large are 1, 2, 3, 6.
Enter description
Enter a row of integer data (-100≤N≤100), and separate the integers with a comma
Output description
Output a row of integer data, indicating the result of the required operation. The integers are separated by a comma
Sample input
1,-3,6,-2
Sample output
1,2,3,6
Analysis
According to the meaning of the question, first use a for loop to modify the list traversal. The abs () absolute value function can convert negative integers into positive integers. Then use sorted() to sort the list. Finally, according to the output format requirements, use the method of outputting the last element alone or string concatenation to achieve an output format of multiple data lines separated by commas.
Reference code

#方法一
ls = [int(i) for i in input().split(",")]
for i in range(len(ls)):
	ls[i] = abs(ls[i])
ls = sorted(ls)
for i in range(len(ls) - 1):
	print(ls[i], end=",")
print(ls[-1])

#方法二
ls = [abs(int(i)) for i in input().split(",")]
ls = sorted(ls)
res = ""
for n in ls:
	res += str(n) + ","
print(res[:-1])

Question 4 25.0 points

(Note: The input and output descriptions and examples may be different from the actual questions and are for reference only)
Question description:
(Note. input ( )No information is allowed to be added in the parentheses of the input function)
Programming implementation:
Suppose there are N (1≤N≤100) kinds of fruits in the orchard. The monkey wants to pick some fruits to take home, but the total weight of the fruits picked by the monkey cannot exceed W (1≤W≤1000).
It is known that the maximum picking quantity of each fruit Ni (1≤Ni≤100), the weight of each fruit Wi (1≤Wi≤100) and the vitamin content Vi of each fruit are known (1≤Vi≤100). What is the maximum amount of vitamins a monkey can obtain when the total weight of picked fruits does not exceed W.
For example: N = 3, W = 5, which means there are 3 kinds of fruits, and the total weight of the fruits picked by the monkey cannot exceed 5.
The maximum picking amount of each fruit Ni, the individual weight of each fruit Wi and the individual vitamin content Vi of each fruit are as follows:
Insert image description here

Take 3 of the first fruit, 1 of the second fruit, and none of the third fruit. The total volume is 5, and the maximum obtainable vitamin content is 3 * 2 + 4 * 1 = 10.
Enter description
In the first line, enter two positive integers representing N and W. Separate the numbers with spaces.
Continue Next N lines, enter 3 positive integers in each line, representing Ni, Wi, Vi respectively. The numbers are separated by spaces
Output description
Output a number , indicating the maximum amount of vitamins a monkey can obtain when the total weight of picked fruits does not exceed W
Sample input
3 5
4 1 2
1 2 4
2 1 1
Sample output
10
Analysis
This question is a multi-dimensional knapsack problem, that is, the number of each type of object in the knapsack is finite, given by the input array. To solve this problem, a larger number of items can be bundled into a variety of items, which evolved from the 01 backpack and the complete backpack.
Reference code

N, W = [int(i) for i in input().split()]
Ni = []
Wi = []
Vi = []
for i in range(N):
	ls = [int(i) for i in input().split()]
	Ni.append(ls[0])
	Wi.append(ls[1])
	Vi.append(ls[2])
F = [0 for i in range(0, w + 1)]

def ComoleteBackPack(w, v):
	for i in range(w, w + 1)
		F[i] = max(F[i], F[i-w] + v)

def OneZeroBackPack(w, v):
	for i in range(W, w - 1, -1):
		F[i] = max(F[i], F[i-w] + v)

def MultipleBackPack(w, v, n):
	if w * n >= w:
		ComoleteBackPack(Wi[i], Vi[i])
		return
	temp_n = 1
	while temp_n < n:
		OneZeroBackPack(temp_n * w, temp_n * v)
		n -= temp_n
		temp_n *= 2
	OneZeroBackPack(n * w, n * v)

for i in range(0, N):
	MultipleBackPack(Wi[i], Vi[i], Ni[i])

print(F[w])		

Question 5 30.0 points

(Note: The input and output descriptions and examples may be different from the actual questions and are for reference only)
Question description:
(Note. input ( )No information is allowed to be added in the parentheses of the input function)
Programming implementation:
Two astronauts are exploring an unknown planet. There are some obstacles on the planet. These obstacles are represented by the number 1, and the absence of obstacles is represented by the number 0. The planet is represented as an N*M matrix. Two astronauts became separated during the exploration. Given the position of astronaut A (x1, y1) and the position of astronaut B (x2, y2), please help astronaut A find a shortest path to the position of astronaut B, and output the length of the shortest path (excluding starting point).
Note:
1.x1 and x2 represent the row numbers of the matrix, y1 and y2 represent the column numbers of the matrix;
2 .The position of the upper left corner is (0, 0);
3. The positions of astronauts A and B can only be on the number 0;
4. There are obstacles The location of the object cannot be passed.
For example: when N=4, M=5, x1=1, y1=0, x2=3, y2=3, the position of astronaut A is (1,0), astronaut B is The position (3, 3), the matrix is ​​expressed as follows:
Insert image description here

Among them, the shortest route from A to B is represented by an arrow, and the length of the shortest route is 7.
Enter description
In the first line, enter two positive integers N and M. The positive integers are separated by spaces.
Continue In the N lines below, enter M numbers in each line. The number 1 represents obstacles, the number 0 represents no obstacles, and the numbers are separated by spaces
Enter four integers in line N+2 , representing x1, y1, x2, y2 respectively, and the integers are separated by spaces
Output description
Output a number indicating the arrival of astronaut A to astronaut B The length of the shortest path to the location (excluding the starting point)
Sample input
4 5
0 0 0 0 0 a>
0 1 0 1 0
0 1 0 0 0
0 0 1 0 0
1 0 3 3
Sample output
7
Analysis
This question uses breadth-first search Algorithm (BFS), searches for the shortest path from the start point to the end point.
Reference code

n, m = [int(i) for i in input().split()]
ls = [[int(i) for i in input().split()] for i in range(n)]
x1, y1, x2, y2 = [int(i) for i in input().split()]

vis = [[x1][y1]] = 1
queue = [[x1, y1, 0]]
d = [(1, 0), (-1, 0), (0, -1), (0, 1)]

while len(queue) > 0:
	x, y = queue[0][0], queue[0][1]
	#print(queue)
	if x == x2 and y == y2:
		print(queue[-1][2])
		break
	for dx, dy in d:
		xx = x + dx
		yy = y + dy
		if 0 <= xx < n and 0 <= yy < m and vis[xx][yy] == 0 and ls[xx][yy] == 0:
			queue.append([xx, yy, queue[0][2] + 1)
			vis[xx][yy] = 1
	
	queue.pop(0)

Guess you like

Origin blog.csdn.net/qq_73698300/article/details/129915202