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 วินาที
อยากฝึกโจทย์แบบนี้เพิ่มไหม เริ่มฝึกหัวข้อนี้ได้เลย ตรวจให้ทันทีทุกข้อ
เริ่มฝึกเลย