A programmer is dry-running an algorithm to find the maximum absolute difference between any two adjacent elements in the list \(L = [15, 22, 18, 30, 25]\).
\(max\_diff = 0\)
\(i = 0\)
while \(i < 4\):
\(diff = L[i+1] - L[i]\)
if \(diff < 0\):
\(diff = -diff\)
if \(diff > max\_diff\):
\(max\_diff = diff\)
\(i = i + 1\)
What is the final value of \(max\_diff\) recorded in the trace table after the loop terminates?
GCE O-Level · Computing (7155)
Algorithm Design:练习题
4 道选择题即时批改,另有 5 道文字题附完整解题步骤,全部围绕「Algorithm Design」。
A student is writing an algorithm to calculate the average of all strictly positive numbers in a list L = [10, -5, 20, 0] without using built-in functions like sum().
total = 0
count = 0
for x in L:
if x > 0:
total += x
count += 1
avg = total / count
What is the final value of avg?
A programmer needs to find the minimum value in a list L = [7, 3, 9, 2, 5] without using the built-in min() function. Which of the following code logic segments correctly identifies the minimum value?
A student is manually tracing an algorithm to count elements in a list that meet specific criteria. The algorithm is as follows:
1. Initialize \(count = 0\)
2. For each number \(n\) in the list \(L = [9, 15, 24, 33, 40, 45]\):
a. If \(n > 10\) and \(n \% 3 == 0\):
i. If \(n \% 2 != 0\):
- \(count = count + 1\)
What is the final value of \(count\) after the algorithm has processed the entire list?
A string \(S = \text{"banana"}\) is provided. Explain the logic an algorithm would use to determine the total number of times the character \(\text{"a"}\) appears in \(S\) without using the built-in \(\text{count()}\) or \(\text{find()}\) methods.
先自己写一遍答案,再对照解题步骤。
A student manually traces an algorithm designed to find the maximum value in the list \(L = [3, 8, 10, 6, 12, 15, 7]\). The variable max_val is initialized to the first element of the list, and then the algorithm iterates through the remaining elements, updating max_val whenever a larger number is encountered. How many times is the assignment statement for max_val executed in total, including the initialization step?
先自己写一遍答案,再对照解题步骤。
A programmer uses the Euclidean distance formula \(d = \sqrt{x^2 + y^2}\) within a Python function to calculate the distance of a point from the origin \((0, 0)\). If the point is located at coordinates \((6, 8)\), what is the final value of \(d\)?
先自己写一遍答案,再对照解题步骤。
A programmer needs to determine how many times a specific character \(c\) appears in a given string \(S\).
Outline the steps of an algorithm to achieve this without using Python's built-in functions such as count() or find(). In your explanation, specify the initialization of any necessary variables, the type of loop to be used, and the logic for updating the count.
先自己写一遍答案,再对照解题步骤。
Consider a list of integers \(A = [12, -5, 8, 0, -3, 10]\). An algorithm is needed to calculate the average value of all positive integers (numbers greater than \(0\)) within the list.
Describe the algorithmic steps required to calculate this average without using built-in Python functions like sum() or len(). Include logic to handle the scenario where the list contains no positive integers to prevent a run-time error.
先自己写一遍答案,再对照解题步骤。
* thinka提供的内容由AI生成,可能并非总是准确或最新。请将其用作辅助资源,并与官方材料进行核实。
想多做几道同类题目?立即开始练习这个课题,边做边批改。
立即练习