To understand what this algorithm is meant to do, we will predict its expected output for different input values and record our predicted outputs in a test plan.
Test #
Input Values
Expected Output
#1
a = 7 b = 2
#1
a = 3 b = 9
#1
a = 150 b = 10
Now that you have completed the “expected output” column of this test plan you might be able to explain what this algorithm actually does?
You can now use our online Python IDE to implement this algorithm and complete each test from the above test plan.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
In this blog post we are investigating what are bitmap pictures and how they are stored on a computer using binary code.
Bitmap Pictures?
Bitmap pictures (also called raster graphics) are computer graphics made of pixels of different colours. Pictures taken with a digital camera (or a scanner) are bitmap pictures. It is also possible to create bitmap pictures using software such as Paint, Photoshop or the Gimp.
.bmp, .tif, .png, .gif and .jpg are all file extensions for different types of bitmap pictures.
Bitmap Picture Quality
The qualify of a bitmap picture relies on mainly two criteria:
The resolution of the picture,
The colour depth of the picture.
Resolution
The quality of a picture is directly linked to the number of pixels in the picture. The number of pixels in a picture depends on the width, height and resolution of a picture.
The width and height of a picture can be given in pixels or inches.
The resolution of a picture is the number of pixels per inch and is measured in dpi (dots per inch) or ppi (pixels per inch).
A higher resolution picture contains a higher number of pixels which results in better quality but also a larger file size.
A higher resolution results in better quality pictures.
A typical resolution for a picture used on a website would be 72 ppi.
For printing purposes, high quality printing would require a higher resolution (e.g 200 ppi, 300 ppi or even 600 ppi).
A higher resolution picture can be enlarged (zoom in) and may still remain crisp whereas a lower resolution picture will look pixelated when enlarged. So when taking pictures with a smartphone or a digital camera, it is important to consider what will the picture be used for. If the intention is to print large canvas of a picture, it will need to be saved using the highest resolution settings, even though it will take more storage space on the device.
Colour Depth
The Colour depth (or color depth – american spelling) of a picture corresponds to the number of bits used to represent the colour value of a single pixel.
Let’s look at the following pixel art widgets below. They are all based on a different colour depths varying from 1 bit per pixel to 4 bits per pixel.
Colour Depth: 1 bit per pixel2 bits per pixel3 bits per pixel4 bits per pixel
A you can see a pictue with a higher colour depth uses a higher number of bits per pixel which results in a wider range of colours hence better quality but also a larger file size.
Most bitmap graphics you would find on the web (especially .png or .jpg) would use a colour depth of 24 bits, resulting in a colour palette of 224 (more than 16,000,000) colours.
The RGB colour coding system uses such a colour depth using 3 Bytes = 24 bits per pixel.
Let’s recap…
Complete this drag and drop activity to highlight the key concepts of bitmap graphics.
dpi
pixels
depth
quality
colours
raster
bits
file size
resolution
pixelate
higher
inch
Bitmap pictures, resolution & colour depth
Bitmap pictures are also known as graphics and are made of of different colours.
The two main factors which affect the quality of a bitmap picture are and colour depth.
The resolution is the number of pixels per , measured in dots per inch () or pixels per inch (ppi).
The higher the resolution the better the of the picture. A picture with a low resolution will when the picture is enlarged.
The colour is the number of per pixel.
The the colour depth, the wider the range of available, hence the higher the quality of the picture, and the larger the .
A pizzaiolo (a man who makes pizza) has been asked to produce a very large pizza, to sprinkle some Parmesan cheese evenly on the pizza and to spread a full jar of black olives (approximately 80 olives) on the pizza.
He has decided to find a method to evenly spread the olives on the pizza to ensure that:
the olives cover the entire pizza,
the olives are relatively equidistant from each others,
Fermat’s Spiral & Vogel’s Model
After doing some research online, our pizzaiolo came across the following webpage about Fermat’s Spiral, and was captivated to find out about how the Vogel’s model could be used to calculate the polar coordinates of each olive to be added to his pizza:
Where θ is the angle,
r is the radius or distance from the centre,
n is the index number of the olive,
c is a constant scaling factor,
The angle 137.508° is the golden angle which is approximated by ratios of Fibonacci numbers.
Python Code
The following Python Turtle demonstrates how Vogel’s model can be used to solve our pizzaiolo’s puzzle:
Your Task
Update the code above to add extra ingredients to your pizza (e.g. pepperoni slices). Try different values instead of the golden angle to see the impact on the resulting pattern.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
How many steps would it take to move the chicken into box b and the elephant into box a, knowing that you cannot have more than one animal per box?
View Solution
Using variables
In a computer program, variables are used to hold values that the program will be abe to access and change if necessary. Variables can contain different types of values such as strings (e.g. “Hello World”), numbers (integers or reals) or Boolean (True or False) values.
Each variable is given a meaningful identifier, chosen by the programmer such as message, playerName, score, numberOfLives.
The assignment operator = is used to assign/store a value to a variable.
Let’s replace the cardboard boxes from the above box swap puzzle with three variables called a, b and temp. We can initialise our variables a and b with their initial values as follows:
a = "chicken"
b = "elephant"
We can solve the above puzzle to swap the content of our two variables using the same three-step approach as follows:
a = "chicken"
b = "elephant"
temp = a
a = b
b = temp
print("Variable a = " + a)
print("Variable b = " + b)
Your Challenge
Write a program that asks the user to enter two values between 1 and 10 and assign these values to two variables called number1 and number2.
Add some code to ensure that number1 ends up with the highest of the two values and number2 with the lowest of the two values. In other words, if number1 is smaller than number2, swap the content of the variables number1 and number2.
Output number1 and number2 on screen.
To complete this challenge, you will have to complete the Python code below:
Extension Task
Extend your code to retrieve three user inputs and swap these to display them in ascending order.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
In this challenge, we will create a Word Unscramble quiz where the computer will randomly pick up words from a given list, scramble the letters of the word, output the scrambled word (anagram) and ask the user to guess the original word. For each correct answer, the user will be given 1 point.
Flowchart
Here is the flowchart of our game:
Looking at the above flowchart, answer the following questions:
Where are the variables score and totalinitialised?
Where is the variable score incremented by 1?
Where is a list declared/initialised?
Where is iteration used and what for?
Where is selection used and what for?
Where is the subroutine/functionscramble() called?
Where is an input statement used?
Where is string concatenation used?
Python Code
We have started the code for you and implemented the scramble()function.
Your task is to complete this code from line 29, following the steps identified in the above flowchart.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
The Moon’s surface is covered with thousands of craters. These are caused by asteroids and meteorites colliding with the lunar surface. In this challenge, we will use Python Turtle to create a drawing of the Moon with a random selection of craters.
This challenge consists of randomly positioning craters of various size (radius) on the surface of the Moon. Generating random (x,y) coordinates is fairly straight forward, however the challenge is to ensure that the craters fit within the disc representing the Moon:
We have started the code for you. You can complete the code in the first tab below. Alternatively you can compare two different approaches used to solve this challenge:
Solution 1: Using polar coordinates and trigonometric formulas (SOHCAHTOA)
Solution 2: Using Cartesian coordinates and Pythagoras’ Theorem
We have started the code for you, but with this code, the craters do not all fit inside the disc representing the Moon. Your task is to adapt this code to make sure that all the craters fit inside the disc representing the surface of the Moon.
With this solution we will randomly generate polar coordinates (Distance from the centre, angle). We will then apply trigonometric formulas (SOHCAHTOA) to convert these polar coordinates into Cartesian coordinates (x,y).
Though this approach works, it tends to generate a higher density of craters towards the centre of the disc representing the Moon.
Our second solution, fully based on Cartesian coordinates solves this problem.
This solution consists of generating a set of random Cartesian coordinates (x,y). Before using these coordinates to draw a crater we calculate the distance between the centre of the disc representing the Moon and the randomly generated (x,y) coordinates. To do so we use Pythagoras’ Theorem. If the distance is lower than (Radius of the Moon – Radius of Crater) then we keep these coordinates to draw our crater, otherwise we generate a new set of coordinates.
Solution...
The solution for this challenge is available to full members! Find out how to become a member: ➤ Members' Area
How confident are you with your computer science subject knowledge? Answer questions from these two-player Q&A grids and try to connect four cells in a row!
You can edit the CSS code below to see the impacts of your changes on the look & feel of this frame. Note that you can edit the properties below or add new properties such as text-decoration, font-style, text-shadow, etc.
CRT Monochrome MonitorPost-It Note
Tweak the code of the above Live CSS Editor frame to make it look like a CRT screen:
Tweak the code of the above Live CSS Editor frame to make it look like a Post-It note: