In this blog post we will use a Python script to work out how many lego bricks would be needed to build planet Earth!
To solve our problem we will use the following data:
We will work out the number of lego bricks needed in several step using a sequencing algorithm: An algorithm consisting of instructions that are carried out (performed) one after another.
We will use the following steps:
- Convert all measurement to the same unit (e.g. mm)
- Calculate the volume of planet Earth
- Calculate the volume of a lego brick
- Number of bricks needed = volume of planet Earth / volume of a lego brick
We will need to use the following two formulas:
The complete flowhchart for our algorithm:
You can now code this algorithm using Python to reveal the answer to the initial question: how many lego bricks are needed to build planet Earth!
Number of lego bricks
If you have completed this algorithm, the final answer should be approximatively:
It is good practice to use subroutines whenever possible as these can then easily be reused in other programs. Your task is to adapt your code by defining a d using two functions as follows:
- Your first subroutine should be called getVolumeOfSphere(), take one parameter called radius and apply the relevant formula to calculate and return the volume of a sphere based on the given radius.
- Your second subroutine should be called getVolumeOfCuboid(), take three parameters called width, length and height and apply the relevant formula to calculate and return the volume of a cuboid based on the given dimensions.
Solution...The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area