GCE O-Level · Computing (7155)

Algorithm Design:練習題

4 條多項選擇題即時批改,另有 5 條文字題附完整解題步驟,全部圍繞「Algorithm Design」。

9 條題目26 免費,無需登記
第 1 題
1

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?

第 2 題
1

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?

第 3 題
1

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?

第 4 題
1

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?

第 5 題
3

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.

先自己寫一次答案,再對照解題步驟。

第 6 題
5

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?

先自己寫一次答案,再對照解題步驟。

第 7 題
3

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\)?

先自己寫一次答案,再對照解題步驟。

第 8 題
4

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.

先自己寫一次答案,再對照解題步驟。

第 9 題
7

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生成,可能並非總是準確或最新。請將其用作輔助資源,並與官方材料進行核實。

你已看過標準答案,接下來輪到批改你的答案。

這一頁可以告訴你好答案的樣子,卻無法指出你的答案欠缺什麼。thinka 按真實評分準則批改你的文字答案,約 15 秒完成。

想多做幾條同類題目?立即開始練習呢個課題,即做即批改。

立即練習