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

pick one voussoir in the vault
    if weight heavier than 25
        mark it as problematic voussoir
repeat until all the voussoirs are checked
output the problmatic voussoirs

3. Write your code

Here, we will use the enumerate method of a list, which adds a counter when we iterate over the list.

voussoir_weight_list = [15, 20, 34, 18, 26, 18]
for i, voussoir in enumerate(voussoir_weight_list):
    print(i, voussoir)

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.

# ==============================================================================
# Input
# ==============================================================================
voussoir_weight_list = [15, 20, 34, 18, 26, 18]
problem_index = []

# ==============================================================================
# Program
# ==============================================================================
# enumerate voussoirs in the vault:
for i, voussoir in enumerate(voussoir_weight_list):
    # check whether the weight is bigger than 25 kg
    if voussoir >= 25:
        problem_index.append(i)

# ==============================================================================
# Output
# ==============================================================================
print("Voussoir ", *problem_index, "are too heavy.")

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

pick one voussoir in the vault
    if weight heavier than 25
        cut it the voussoir in half until the weight is below 25
        replace the problematic voussoir with the new ones
repeat until all the voussoirs are checked
output the problmatic voussoirs

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.

voussoir = 54
count = 1
while voussoir >= 25:
    voussoir /= 2
    count *= 2
print(voussoir, count)

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.

# ==============================================================================
# Input
# ==============================================================================
voussoir_weight_list = [15, 20, 54, 18, 26, 18]
new_voussoir_weights = []

# ==============================================================================
# Program
# ==============================================================================
# enumerate voussoirs in the vault:
for i, voussoir in enumerate(voussoir_weight_list):
    # check whether the weight is bigger than 25 kg
    if voussoir >= 25:
        count = 1
        # cut the voussoir by half until it it less than 25 kg
        while voussoir >= 25:
            voussoir /= 2
            count *= 2
        new_voussoir_weights.extend([voussoir] * count)
    else:
        new_voussoir_weights.append(voussoir)

# ==============================================================================
# Output
# ==============================================================================
print(new_voussoir_weights)

[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