More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Standard Deviation Algorithm

In statistics, given a set list of numbers, the standard deviation is a measure of the amount of variation or dispersion within the set.

A low standard deviation indicates that the values tend to be close to the mean value of the set.
A high standard deviation indicates that the values are spread out over a wider range.

The formula to calculate the standard deviation for a given set of numbers is as follows:

Where x represents the mean value of the set and n represents the number of values in the set.

Your task is to design an algorithm for a function called standardDeviation() that takes one parameter: a list of integer values. The list can be of any length. Your function will calculate and return the standard deviation for this given list.

Python Code

We have started the code for you by creating a function called inputList() that lets the user input any list of numbers, one number at a time. Your task is now to add the code for the standardDeviation() function.

Test Plan

Test # Input Values Expected Output Actual Output
#1 7,14,9,21,32,15 8.3199893162325
#2 23,101,52,48,107 32.480147782915
#3 10,40 15
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

The Jorvik Viking Settlement​

From around A.D. 800 to the 11th century, a large number of Scandinavians Vikings left their homelands to seek their fortunes on the east coast of England. These seafaring warriors ransacked different coastal sites from Northembria to East Anglia. In 866, they invaded the city of York which, after being renamed Jorvik, became their capital.​

​Most of their constructions was made of wood, hence there are not so many vestiges left of their occupation. Vikings invaded the East coast of England after crossing the North Sea on long wooden ships. One of their traditions was to bury these ships in the ground to create a burial site for their dead.​

While excavating a site under the Cathedral of York, a team of archaeologists have recently found a complex system of 12 locking compartments, carved directly in stone. They believe each compartment contains a Viking shield and that each shield has a symbol engraved on it. By accessing these 12 symbols, they believe they could reveal the location of a ship burial site where Ragnar Lothbrok, one of the most famous Viking warrior, is buried alongside his undiscovered treasure of inestimable value.​

Each locking mechanism consists of two sliders. For each mechanism, a diagram has also been carved in the stone which, we believe, provides the key to unlock the compartment. It is essential to open these compartments by finding the correct position of the sliders, as trying to break the mechanism itself to open the compartment would damage its content and would prevent the archaeologists from revealing the code engraved on each shield.​

The team of archaeologists is asking your help to help them unlock the 12 compartments and reveal the location of Ragnar Lothbrok’s treasure!​
The Jorvik Viking SettlementOpen in New Window
The Jorvik Viking SettlementOpen in New Window

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Countries of Africa

Can you name all of the 54 countries of Africa?

In this challenge we will write a Python script to create a quiz where we ask the user to guess as many African countries as possible. The player will score one point per correct country.

Using a list in Python

Our Python program will use a list to store all 54 countries.

countries = [“Algeria”, “Angola”, “Benin”, “Botswana”, “Burkina Faso”, “Burundi”, “Cabo Verde”, “Cameroon”, “Central African Republic”, “Chad”, “Comoros”, “Ivory Coast”, “Djibouti”, “Democratic Republic of the Congo”, “Egypt”, “Equatorial Guinea”, “Eritrea”, “Eswatini”, “Ethiopia”, “Gabon”, “Gambia”, “Ghana”, “Guinea”, “Guinea-Bissau”, “Kenya”, “Lesotho”, “Liberia”, “Libya”, “Madagascar”, “Malawi”, “Mali”, “Mauritania”, “Mauritius”, “Morocco”, “Mozambique”, “Namibia”, “Niger”, “Nigeria”, “Republic of the Congo”, “Rwanda”, “Sao Tome & Principe”, “Senegal”, “Seychelles”, “Sierra Leone”, “Somalia”, “South Africa”, “South Sudan”, “Sudan”, “Tanzania”, “Togo”, “Tunisia”, “Uganda”, “Zambia”, “Zimbabwe”]

To check how many items (countries) are in the list, we can use the len() function:

numberOfCountries = len(countries)
print("There are " + str(numberOfCountries) + " countries in Africa!")

To check if a country is in the list we can use the keyword in as follows:

if "Kenya" in countries:
   print("Kenya is in Africa!")

To remove a country from the list, we can use the remove() function. For instance is the user has named a country which is in the list, we can remove this country from the list.

if "Kenya" in countries:
   countries.remove("Kenya")

Flowchart

Below is the full flowchart for our Python quiz: (Click on flowchart to zoom in)

Python Code

We have started the code, you task is to complete the code in the trinket window below:

Extension Task

Amend this code to give the player three lives. For each invalid answer, the player should lose a life. The game should stop and display a game over message when the player has found all 54 countries or when they have lost their 3 lives.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Let’s Build a Castle

In this challenge we are going to build different medieval castles by customising some Python code.

Our first castle is already made for us and looks as follows:

You can see, in the trinket window below, the Python code used to create this castle.

The code uses a range of pre-defined functions to draw the different building blocks of the castle (walls, towers, doors, flags and loopholes). These functions can be accessed in the extra file (tab) called shapes.py. However for the purpose of this challenge, you will not need to access the code of these functions.

The main functions that you will have to reuse to cutomise the look and feel of your castle are:

  • drawWall()
  • drawTower()
  • drawDoor()
  • drawLoophole()
  • drawFlag()

Each of these functions is using some parameters to change the position, width and height of the different building blocks and to change the colour of the flag. Let’s investigate these functions and their parameters.

drawWall()drawTower()drawFlag()drawDoor()drawLoophole()(X,Y) Coordinates?Colour Codes?
The drawWall() function uses the following 6 parameters:

So the following code would be used to create a wall at position (-150,0). The wall would be 300 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawTower() function uses the following 6 parameters:

So the following code would be used to create a tower at position (-100,0). The tower would be 50 pixels wide, 150 pixels high and would have battlements (crenellation).

The drawFlag() function uses the following 6 parameters:So the following code would be used to create a flag at position (120,120). The flag would be 30 pixels wide, 40 pixels high and be of a red colour.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a door at position (-100,0). The door would be 50 pixels wide, 150 pixels high.
The drawDoor() function uses the following 5 parameters:
So the following code would be used to create a loophole at position (-100,0). The loophole would be 10 pixels wide, 30 pixels high.

(X,Y) Coordinates?


The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high.
Look at the canvas below to understand how (x,y) coordinates work:


Colour Codes


#FF0000#FFFF00#0000AA#00AA00

Python Code

Here is the Python code to create the castle.

Your Task

Your task is to edit the code from line 16 to 27 to re-create the following castles. To do so you will have to tweak the parameters of the functions used to create the castle. You may also need to remove or add some extra lines of code to add extra elements (towers, flags, loopholes, etc…)

Extension Task

Access the code in the shapes.py file (tab).
Create two more functions as follows:

  • drawSun() to draw a yellow circle at a given (x,y) set of coordinates.
  • drawTree() to draw a tree at a given (x,y) set of coordinates.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with:

Python Turtle Race

In this challenge we will create a Python script to complete a Turtle race with four turtles racing across the screen.

We have started the code for you to set up the pitch of grass. You will complete this project step by step in order to:

    Add a start line to the left of the screen,
    Add a finish line to the right of the screen,
    Add four turtles on the start line,
    Animate the turtles to race across the screen,
    Find out which turtle is first to cross the finish line

Here is the Python code that you will need to complete:

Step 1: Start lineStep 2: Finish lineStep 3: Adding TurtlesStep 4: Race Time!Step 5: And the winner is...

Step 1: Adding a start line


To draw the start line as a vertical line on the left hand side of the screen, we will need to use the following (x,y) coordinates to tell the computer where does the start line starts and ends:

You will can add the following code to the above trinket to draw the start line:

#Let's draw the start line
myPen.penup()
myPen.goto(-160, 150)
myPen.pendown()
myPen.goto(-160, 0)

Step 2: Adding a finish line


It’s now your turn to use very similar code, this time to draw the finish line using the following (x,y) coordinates:

View Solution!

Step 3: Adding four turtles on the start line

We are now going to add the four competitors on the start line. We will have four turtles that we will need to position using the following (x,y) coordinates:

Here is the code to add the red turtle at position (-180, 140):

#Let's add our turtles
redTurtle = turtle.Turtle()
redTurtle.shape('turtle')
redTurtle.color('red')
redTurtle.pensize(2)
redTurtle.penup()
redTurtle.goto(-180, 140)
redTurtle.pendown()

Can you add some more lines of code to add 3 more turtles using the following coordinates:

  • Green Turtle at position (-180,110)
  • Blue Turtle at position (-180,80)
  • Yellow Turtle at position (-180,50)
View Solution!

Step 4: Animating the turtles to race across the screen


It’s now time for our turtles to race across the screen. To do so we will use an infinite while loop. Within each iteration of the loop, we will move each turtle forward by a randomly generated number of pixels between 1 and 8.

Here is the code to use for our infinite loop…

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))

Step 5: And the winner is…

As you can see with step 4, the four turtles carry on racing even after crossing the finish line. They even disappearing off screen!

Our aim is now to check, within each iteration of the while loop, to see if one of the four turtles has reached the finish line. To do so, we will need to check the x coordinate of each turtle. A turtle with an x coordinate greater than 180 has crossed the finish line!

Here is the final code for our racing loop!

#Let's start the race!
while True:
  redTurtle.forward(random.randint(1,8))
  greenTurtle.forward(random.randint(1,8))
  blueTurtle.forward(random.randint(1,8))
  yellowTurtle.forward(random.randint(1,8))
  
  if redTurtle.xcor()>180:
    print("Red Turtle wins!")
    break
  elif greenTurtle.xcor()>180:
    print("Green Turtle wins!")
    break
  elif blueTurtle.xcor()>180:
    print("Blue Turtle wins!")
    break
  elif yellowTurtle.xcor()>180:
    print("Yellow Turtle wins!")
    break
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Crenellation Challenge

In a medieval castle, the crenelation (a.k.a battlement) is a parapet at the top of a wall that has regularly spaced squared openings for archers to shoot through.

In this challenge, we are going to use Python Turtle to create repeating patterns such as the crenellation of a medieval castle.

First, let’s investigate how to draw a horizontal line using python Turtle:

Task #1:

Your task is to edit the above code to create a single crenellation pattern as follows: (Instead of a horizontal line going across the screen).

To do so you will need to use a few Python Turtle instructions including:

  • myPen.forward(20)
  • myPen.left(90)
  • myPen.right(90)
View Solution!

Task #2: Repeating Pattern


In order to repeat this crenellation pattern to go across the screen, we will use a for loop.

for i in range (0,10):
    myPen.left(90)
    myPen.forward(15)
 
    myPen.right(90)
    myPen.forward(20)

    myPen.right(90)
    myPen.forward(15)

    myPen.left(90)
    myPen.forward(20)

Task 3: Alternative Crenellation Patterns…

Your task is to tweak the above code to recreate the following repetitive patterns…



unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with:

Zip It! – Python Challenge

For this challenge, we are going to recreate an existing built-in python function called zip().

The purpose of the zip() function in python is to merge two given lists into a single list of tuples. Each tuple will contain two values: one value from each list being merged.

You can see how the zip() function works using the trinket embed below.

Your task is to create a new function called zipLists() that will behave exactly the same way as the built-in zip() function, using your own algorithm. Your function will need to take two lists as parameters and return a “zipped list of tuples”. To start with, you may assume that both lists provided will be of the same length (contain the same numbers of values). You will then be able to adapt your code to consider the case where one list has less values than the other. In this case you will ignore surplus values.

Python Code


Complete the code below to define and test your own zipLists() function.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Join the Dots Challenge

To complete this challenge, your task is to connect the 9 dots below using just 4 connected straight lines (without lifting your pen!).

Grab a piece of paper and have a go.

Alternatively, you can draw and connect the 9 dots on this online whiteboard:

Python Turtle Challenge


Did you manage to connect the 9 dots in just 4 straight lines, without lifting your pen? If so you are now going to write an algorithm using Python Turtle for the computer to solve this challenge by following the instructions of your algorithm. To do so we will use (x,y) coordinates. The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high. The coordinates (0,0) refers to the centre of the cnavas.

xy-coordinates

Look at the canvas below to understand how (x,y) coordinates work:

In order to draw on screen, we will use the goto() instruction from the turtle library. This instruction takes two parameters: the x,y coordinates of where the pen should travel to:

pen.goto(50,150)

Complete the code…

We have started the code for you. Your task is to add a few goto() instructions for the computer to solve this challenge challenge!

Tagged with:

The Envelope Challenge

To complete this challenge, your task is to draw the following envelope without lifting your pen and without going over the same line twice!

Grab a piece of paper and have a go.

Alternatively, you can draw your envelope on this online whiteboard:

Python Turtle Challenge


Did you manage to draw the envelop in one continuous line, without visiting any segment twice? If so you are now going to write an algorithm using Python Turtle for the computer to solve this challenge by following the instructions of your algorithm. To do so we will use (x,y) coordinates. The canvas we are drawing on (using Python Turtle) is 400 pixels wide by 400 pixels high. The coordinates (0,0) refers to the centre of the cnavas.

xy-coordinates

Look at the canvas below to understand how (x,y) coordinates work:

In order to draw on screen, we will use the goto() instruction from the turtle library. This instruction takes two parameters: the x,y coordinates of where the pen should travel to:

pen.goto(50,150)

Complete the code…

We have started the code for you. Your task is to add a few goto() instructions for the computer to solve the envelope challenge!

Extension Task


Your extension task consists of drawing this diamond shape using Python Turtle. Note that it, is not possible to draw this shape without either lifting up your pen or re-visiting the same segment twice.

Tagged with:

Logic Gates Circuits in Cars

In this set of challenges you will design and test some logic gates circuits to control different automated features of a car including:

  • Automatic Headlights System
  • Interior Light System
  • Automatic Windscreen Wipers System
Automatic Headlights SystemInterior Light SystemAutomatic windscreen wipers systemDashboard Seatbelts Indicator

Automatic Headlights System


On most modern cars, the car can automatically turn the lights on and off when it detects it is driven at night time or when the driver is driving in rainy conditions. Nowadays most cars are equipped with the following sensors and switches to control the headlights:

  • A light sensor that detects when it is day time. A light sensor would be “on” (1) when there is light outside, but would automatically turn off (0) when it is dark outside (night time or driving through a tunnel).
  • A humidity sensor that detects when it is raining. A humidity sensor would be “on” (1) when it is raining outside, but automatically turns off (0) when is dry outside.
  • A engine switch to turn the engine on (1) or off (0).
  • A light switch to turn the lights on (1) or off (0).

Your task is a to design and test a logic gates circuit for the automatic headlights system that will apply the following rules:

  • The headlights are on if the engine is on and it is dark outside
  • The headlights are on if the engine is on and it is rainy outside
  • The headlights are on if the light switch is turned on (even when the engine is off)
  • If none of the above conditions are met, the headlights are turned off


Interior Light System


The interior light system (aka dome light) is used to lit up the inside of the car. The light is located on the ceiling of the car and can be manually turned on and off using a light switch located next to the light. Most cars are also equipped with sensors on each door. These sensors are on (1) when the door is open and off when the door is closed (0). Each door has its own sensor, including the driver door, the passenger door and the door to access the boot.

Your task is a to design and test a logic gates circuit for the interior light system that will apply the following rules:

  • The interior light is on if the light switch is on
  • The interior light is on if at least one of the door is open (on driver the driver side, passenger side or the door for the boot)
  • If none of the above conditions are met, the interior light is turned off


Automatic windscreen wipers system


The windscreen wipers system is used to automatically detect when the screen wipers should be turned on to improve the driving condition/visibility of the driver when it’s raining. They rely on a humidity sensor located on the windscreen that is on (1) when it detects water/rain. The windscreen wipers can also be turned on manually using a switch on the dashboard of the car.

Your task is a to design and test a logic gates circuit for the automatic windscreen wipers system that will apply the following rules:

  • The wipers are turned on if the engine is on and the humidity sensors detects rain water on the windscreen
  • The wipers are turned on if the engine is on and the driver has turned the wipers switch on
  • If none of the above conditions are met, the windscreen wipers are turned off


Dashboard Seatbelts Indicator


On the dashboard of this two-seater sports car, a light indicator is used to indicate if either the driver (D.) or the passenger (P.) (or both) have not locked their seatbelts.

The car uses weight/pressure sensors underneath each seat to detect if the seat is occupied (sensor on) or not (sensor off).
Each seatbelt has a locking mechanism with a sensor to detect if the seatbelt is on or off.

Design and test a logic gates circuit to control the seatbelt light indicator on the dashboard of the car.


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: