Ex 2: Check Voussoir Weight
In this tutorial session, you will:
consolidate the knowledge that you have learned in the last session
learn how to write a while loop
Ex 2.1: Check Voussoir Weight
Question:
You have designed a freeform masonry vault and all the stone pieces are unique. You want to assemble the vault manually. However, on the construction site, the manual handling weight limit is 25 kg. Thus, you have to find the pieces that are too heavy and export their index.
Voussoir index | Weight |
0 | 15 kg |
1 | 20 kg |
2 | 54 kg |
3 | 18 kg |
4 | 26 kg |
5 | 18 kg |
Answer:
1. Draw Flowchart
2. Write pseudocode
3. Write your code
Here, we will use the enumerate
method of a list, which adds a counter when we iterate over the list.
We can create an empty list and add the index of the problematic voussoir in the list during our iteration. In the end, we can export the list. The *
operator unpacks the list.
You should get the output: Voussoir 2 4 are too heavy.
Ex 2.2: Optimize Voussoir Weight
Question:
Now you would optimize all the voussoirs that are too heavy by cutting them into two pieces while keeping the sequence of the voussoirs.
Answer:
1. Draw Flowchart
We can continue from the last example. When the voussoir is too heavy, we need to first cut it in half, then check the weight of the half one. If the new weight is still too heavy, keep cutting. After cutting, we need to add the new cut stones back, so the sequence of the original voussoirs will not change.
2. Write pseudocode
3. Write your code
Here, we will use a while loop, which keeps executing the code inside if the condition is True
. The loop will stop when the condition is not fulfilled any more. For example, here we pick a voussoir that is 54 kg and the count is 1. The weight is too heavy. In the first iteration of the while loop, the voussoir will be cut in half, so the weight is divided by 2 and the count is multiplied by 2. Now, the while loop checks the new voussoir weight, which is 54 / 2 = 27. 27 is still bigger than 25, so the loop will keep running. The 27 kg voussoir is further cut in half and the count is multiplied by 2. Now the new weight is 27 / 2 = 13.5, which is smaller than 25. Thus, the while loop stops.
Inside the while loop, we redeclare the value of the variable voussoir, variable count in every iteration. So, we can print the final value.
You will get: 13.5 4
Now let's solve the problem. To notice that, when we iterate over the list, the items of the list cannot be modified. Thus, we create a new empty list: new_voussoir_weights
. After checking each voussoir, we can add the original voussoir or the processed smaller ones to the list.
[15, 20, 13.5, 13.5, 13.5, 13.5, 18, 13.0, 13.0, 18]
should be the new vault weight list.
Last updated