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により生成されているため、内容が正確でない場合があります。補助教材としてご使用いただき、公式の教材と合わせてご確認ください。
模範解答は見ました。次はあなたの答案を採点します。
このページは良い答案の形を示せますが、あなたの答案に何が足りないかは教えられません。thinka は実際の採点基準に沿って記述答案を約 15 秒で採点します。
同じような問題をもっと解きたい?このトピックの新しい問題を、解きながら採点。
練習を始める