Master Guide of Examples: Computational Thinking (Weeks 1-4)
This document provides a concrete example for every identified question pattern from the first four weeks of Computational Thinking, complete with a step-by-step solution and analysis.
Week 1: Procedural Thinking & Basic Operations
Pattern 1.1: Procedure Interpretation
Example:
The following procedure is executed. What will X represent at the end?
Step 1: Arrange all cards in Pile 1Step 2: Initialize variables A, B and X to 0Step 3: If Pile 1 is empty then stop and go to Step 7Step 4: Read the top cardStep 5: If Shop Name is “SV Stores” then add total bill amount to A and increment BStep 6: Move card to Pile 2 and repeat from Step 3Step 7: X = A / B
Click for Solution
1. **Analyze Variable `A`:** It's an **accumulator**. It starts at 0 and sums the `total bill amount` for every card that matches the condition `Shop Name is “SV Stores”`. Therefore, `A` is the *total of all money spent at SV Stores*.
2. **Analyze Variable `B`:** It's a **counter**. It starts at 0 and is incremented by 1 for every card that matches the same condition. Therefore, `B` is the *number of bills from SV Stores*.
3. **Analyze the Final Calculation:** Step 7 calculates `X = A / B`. This is `(Total Sum) / (Total Count)`.
4. **Conclusion:** This is the definition of an average.
Final Answer:X represents the Average of total bill amount from “SV Stores”.
Pattern 1.2: Bug Identification
Example:
The following procedure is supposed to count all cards, but it gives the wrong answer. Identify the incorrect step.
Step 1: Arrange all cards in Pile 1Step 2: Initialize count to 0Step 3: If Pile 1 is empty then stopStep 4: Read the top cardStep 5: Increment countStep 6: Move card to Pile 2 and repeat from Step 2
Click for Solution
1. **Trace the Loop:** Let's follow the instructions.
* **Start:** `count` is set to 0 (Step 2).
* **First card:** The card is read, `count` becomes 1 (Step 5).
* **Loop Instruction:** Step 6 says "repeat from **Step 2**".
2. **Spot the Error:** When the procedure jumps back to Step 2, the instruction is `Initialize count to 0`. This **resets the counter** every single time a card is processed.
3. **Result:** The procedure will never count past 1. The final value of `count` will be 0 if the pile was empty, or 1 if it had any cards.
4. **The Fix:** The procedure should loop back to the check, which is **Step 3**, not the initialization.
Final Answer: The mistake is in Step 6, which contains an incorrect loop instruction.
Week 2: Advanced Logic & Finding Extrema
Pattern 2.1: Finding Minimum/Maximum
Example:
What do A and B represent at the end of this procedure on the “Scores” dataset (scores are 0-100)?
Step 1: ...Step 2: Initialize A to 101 and B to 0Step 3: If Pile 1 is empty then stopStep 4: Read the top card XStep 5: If A > X.Chemistry, then set A = X.ChemistryStep 6: If B < X.Mathematics, then set B = X.MathematicsStep 7: ...
Click for Solution
* **Analysis of `A` (The Limbo Dance):**
* `A` is initialized to **101**, a value higher than any possible score.
* The condition `If A > X.Chemistry` means `A` will only be updated if a new score is *smaller* than the current value of `A`.
* This is the classic algorithm for finding the **minimum** value.
* **Analysis of `B` (King of the Hill):**
* `B` is initialized to **0**, a value lower than or equal to any possible score.
* The condition `If B < X.Mathematics` means `B` will only be updated if a new score is *larger* than the current value of `B`.
* This is the classic algorithm for finding the **maximum** value.
Final Answer:A will be the Lowest marks in Chemistry, and B will be the Highest marks in Mathematics.
Pattern 2.3: State Management & Re-initialization
Example:
What does A represent at the end of this procedure on the “Words” dataset?
Step 1: ...Step 2: Initialize A to 1000 and B to 0Step 3: ...Step 4: Read cardStep 5: Add Letter Count to BStep 6: If Word does not end with a full stop then go to Step 9Step 7: If Word ends with a full stop AND B < A then store B in AStep 8: Re-initialize B to 0Step 9: Move card to Pile 2 and repeat from Step 3
Click for Solution
1. **Identify the Variables' Roles:**
* `B` is an **accumulator**. It sums the `Letter Count` of words it sees (`Step 5`).
* `A` is a **minimum-holder**. It's initialized to a large number and is only updated if a new value (`B`) is smaller (`Step 7`).
2. **Identify the Grouping Logic:**
* The key is **Step 8: `Re-initialize B to 0`**. This reset happens only when a word ends with a full stop.
* This means the procedure is processing words in groups, and each group ends with a full stop. These groups are **sentences**.
3. **Combine the Logic:**
* The procedure calculates the total number of letters for each sentence (`B`).
* At the end of each sentence, it checks if that sentence's length is the shortest one found so far (by comparing `B` with `A`).
* It then resets `B` to start counting for the next sentence.
Final Answer:A represents the length of the shortest sentence based on the number of letters.
Week 3: Procedures & Boolean Flags
Pattern 3.1: Boolean Logic with Flags
Example:
The following pseudocode is executed. If E is True at the end, what does it imply?
Initialize E to TrueWhile Pile 1 is not empty: Read card X If X.Gender is "F" then If X.Physics < 60 OR X.Chemistry < 60 OR X.Maths < 60 then Set E to False
Click for Solution
1. **Analyze the Flag Initialization:** `E` starts as `True`. This is an "optimistic" assumption. The procedure is trying to prove a "for all" statement.
2. **Analyze the Condition for Falsification:** `E` is set to `False` if a female student is found who has a score `< 60` in *at least one* subject (`OR` logic).
3. **Determine the Meaning of `E` remaining `True`:** For `E` to remain `True` until the end, the falsification condition must *never* be met for any female student.
4. **State the Opposite:** The opposite of "at least one score is < 60" is "all scores are >= 60".
5. **Conclusion:** `E` being `True` at the end implies that **all female students have scores greater than or equal to 60 in all three subjects**.
Final Answer: All female students have scores greater than or equal to 60 in Physics, Chemistry and Maths.
Pattern 3.2: Using Procedures (Subroutines)
Example:
A procedure CountItems(Pile, ItemName) returns the number of bills in Pile that contain ItemName. What does A - B represent?
Pile_Soap = all bills containing "Soap"Pile_Facewash = all bills containing "Facewash"A = CountItems(Pile_Soap, "Facewash")B = CountItems(Pile_Facewash, "Shampoo")Result = A > B
This is a hypothetical example to demonstrate the pattern. Let’s analyze a more direct one from your files.
Corrected Example: A procedure avg(P) returns the average total marks of cards in pile P. What does A count?
Vellore_Avg = avg(Vellore_Pile)Chennai_Avg = avg(Chennai_Pile)Initialize A to 0While Madurai_Pile is not empty: Read card X If X.Total > Vellore_Avg AND X.Total < Chennai_Avg then Increment A
Click for Solution
1. **Trace the Procedure Calls:** The first two lines call the `avg` procedure to get two specific numbers. `Vellore_Avg` becomes the average total for Vellore students, and `Chennai_Avg` becomes the average total for Chennai students.
2. **Analyze the Main Loop:** The procedure then iterates through the `Madurai_Pile`.
3. **Analyze the Condition:** For each Madurai student (`X`), it checks if their total marks fall *between* the two previously calculated averages.
4. **Conclusion:** `A` is counting the number of students from Madurai whose total marks are strictly greater than the Vellore average and strictly less than the Chennai average.
Final Answer: Number of students from Madurai with a total score between the Vellore average and the Chennai average.
Week 4: Nested Loops & All-Pairs Comparisons
Pattern 4.1: All-Pairs Comparison
Example:
The following pseudocode (using the standard 3-pile nested loop structure) is run on the “Olympics” dataset. What does E count?
// Inside the inner loop:If X.Country is equal to Y.Country AND X.Medal is not equal to Y.Medal then Increment E
Click for Solution
1. **Identify the Structure:** This is a classic **All-Pairs Comparison** using a nested loop. The procedure is systematically creating every possible pair of players (`X` and `Y`).
2. **Analyze the Condition:** The counter `E` is incremented only if a pair meets two conditions simultaneously:
* `X.Country is equal to Y.Country`: The two players must be from the **same country**.
* `X.Medal is not equal to Y.Medal`: The two players must have won **different medals**.
3. **Combine the Logic:** The procedure is counting the number of pairs of players who are teammates from the same country but who won different types of medals (e.g., one won Gold, the other Silver).
Final Answer:E represents the number of pairs of players from the same country with different medals.
Pattern 4.2: Nested Logic on a Single Card
Example:
The following is executed on the “Words” dataset. What does A count?
Initialize A to 0While Pile 1 is not empty: Read card X // Inner logic starts here i = 1 is_followed = False While i < (Letter Count of X): L1 = i-th letter L2 = (i+1)-th letter If is_vowel(L2) then is_followed = True i = i + 1 If is_followed is True then Increment A
Click for Solution
1. **Identify the Structure:** The outer loop processes each word card `X`. The inner `While` loop iterates through the *letters* of the word on that single card.
2. **Analyze the Inner Logic:**
* The loop checks every letter from the first to the second-to-last.
* The condition `If is_vowel(L2)` checks if the letter *following* the current one is a vowel.
* The `is_followed` flag is set to `True` if this condition is met even once for the word.
3. **Analyze the Outer Logic:**
* The counter `A` is incremented only if the `is_followed` flag was flipped to `True` during the inner loop.
4. **Combine the Logic:** The procedure is counting words for which there is at least one letter that is immediately followed by a vowel.
Final Answer:A represents the number of words with at least one letter followed by a vowel.