You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this exercise, we will see how computational thinking can be used to add up all the numbers between 1 and 200 in our heads, i.e. `1 + 2 + 3 + 4` and so on. We should be able to do this in less than a minute.
8
24
9
25
Seems impossible?
@@ -84,15 +100,94 @@ That's it! Using those four key steps, we have learned the basics of computation
84
100
85
101
::::::::::::::::::::::::::::::::: challenge
86
102
87
-
## Practice
103
+
## Option 1: Practice with Numbers
88
104
89
105
Use the algorithm above to add up all the numbers between 1 and 24, 1 and 50, and 1 and 1,000.
90
106
107
+
:::::::: solution
108
+
109
+
**1 to 24:**
110
+
- x = 24
111
+
- (24 / 2) * (24 + 1)
112
+
- 12 * 25 = **300**
113
+
114
+
**1 to 50:**
115
+
- x = 50
116
+
- (50 / 2) * (50 + 1)
117
+
- 25 * 51 = **1,275**
118
+
119
+
**1 to 1,000:**
120
+
- x = 1000
121
+
- (1000 / 2) * (1000 + 1)
122
+
- 500 * 1001 = **500,500**
123
+
124
+
::::::::::::::::
125
+
91
126
::::::::::::::
92
127
93
128
::::::::::::::::::::::::::::::::: discussion
94
129
95
-
## Discussion
130
+
## Discussion: Odd Numbers
96
131
97
132
The numbers above are all even numbers. What would be the process for adding up numbers if the final number is an odd one, e.g., 17? Can you use the same formula? If not, what adaptations would you need to make to the formula?
133
+
134
+
:::::::: solution
135
+
136
+
Yes, the formula `(x/2) * (x + 1)` still works mathematically.
137
+
138
+
**For 17:**
139
+
- x = 17
140
+
- (17 / 2) * (17 + 1)
141
+
- 8.5 * 18 = **153**
142
+
143
+
**Conceptually:**
144
+
If you prefer the "pairing" method (Decomposition):
145
+
- You have 8 pairs (1+17, 2+16, etc.) that sum to 18.
146
+
- The middle number, 9, is left over.
147
+
- (8 * 18) + 9 = 144 + 9 = **153**.
148
+
149
+
Both methods yield the same result. The abstraction formula handles the "half pair" (0.5) correctly.
150
+
151
+
::::::::::::::::
152
+
98
153
:::
154
+
155
+
::::::::::::::::::::::::::::::::: challenge
156
+
157
+
## Option 2: Shelving Books Efficiently
158
+
159
+
Imagine you are working in a library. You have a cart of new books that need to be shelved. The shelves are currently quite full. Every time you find the correct spot for a new book (e.g., by call number), you have to shift all the subsequent books on that shelf to the right to make space. This physical shifting is heavy, slow, and tedious.
160
+
161
+
How can you use **Computational Thinking** to solve this problem for future shelving? Apply the four steps:
162
+
163
+
1.**Decomposition:** What are the individual actions involved in shelving?
164
+
2.**Pattern Recognition:** What is the specific bottleneck that repeats?
165
+
3.**Abstraction:** Can we conceptually change how the shelf "works"?
166
+
4.**Algorithm:** What new process can we adopt to minimize the work?
167
+
168
+
*Hint: Think about "Gapped Insertion Sort" or "Library Sort".*
169
+
170
+
:::::::: solution
171
+
172
+
1.**Decomposition:** Shelving involves three main actions: (a) Finding the correct spot, (b) Making space (shifting existing books), and (c) Placing the new book.
173
+
2.**Pattern Recognition:** The "Making space" step is the bottleneck. It requires physical effort and time, and it happens *every single time* we insert a book into a contiguous row. The more books on the shelf, the more work it takes to shift them.
174
+
3.**Abstraction:** Instead of viewing the shelf as a solid block of books that must be packed tightly, view it as a collection of items with *buffers* or *gaps* distributed between them. The "concept" of the shelf now includes empty space as a feature, not a bug.
175
+
4.**Algorithm (The Library Sort):**
176
+
***Step 1 (Setup):** Spread the existing books out on the shelves so there are intentional gaps between them (e.g., leave 25% of the shelf empty).
177
+
***Step 2 (Insert):** When a new book arrives, place it into the open gap at its correct location. **No shifting is required!**
178
+
***Step 3 (Maintenance):** If a specific gap fills up completely, redistribute just the books in that small section to create new gaps.
179
+
180
+
By using this algorithm, you trade a small amount of space (the gaps) for a massive gain in speed and reduced effort. In computer science, this is known as a **Gapped Insertion Sort**.
0 commit comments