More results...

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

Darts Scoring Algorithm

The following diagram explains how a dart is allocated a score in a game of darts.
dartboard-scores

To calculate the score of an arrow based on its (x,y) coordinates we will use two calculations:

  • The distance of the arrow from the centre of the target,
  • The angle matching the arrow position.

Calculating the distance


To calculate the distance of the arrow from the centre of the target, we will use the Pythagora’s Theorem:
dartboard-pythagoras
You can review this blog post where we have used a similar approach to calculate the score of an arrow in a game of archery.

We will then be able to use this distance to check where the arrow landed:

  • Bull’s Eyes (Red inner circle)
  • Bull (Green inner circle)
  • On target (Black or white zones)
  • On the triple ring
  • On the double ring
  • Outside of the target

To do so you will need the dimensions of the target (in pixels). These are given on the diagram below:
dartboard-distances

Calculating the angle


To calculate the angle we will use the trigonometric formulas (SOCATOA):
dartboard-trigonometric-formula

Using the following angles your program can then figure out the score for the arrow:
dartboard-angles

Complete the code


Check the code below used to display the dartboard and to throw an arrow by generating random (x,y) coordinates.

Complete the code to calculate both the distance and the angle of the arrow based on the (x,y) coordinates of the arrow. Use this information to find out the actual score of the arrow.

Extension Task


Adapt your code so that the computer throws three darts. Your script should then calculate the total score of the three darts.
unlock-access

Solution...

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

Factorial Challenge

In Mathematics, the factorial of n is denoted by n! and calculated by the product of integer numbers from 1 to n.

For instance:
factorial-of-5

In this challenge you will write a Python program that asks the user to enter a positive number. In return the program will output the factorial of this number.

Iterative Approach


n! = n x (n-1) x (n-2) x (n-3) x … x 4 x 3 x 2 x 1

Your task: Write a function to calculate n! using an iterative approach.

Recursive Approach


Another approach to define the factorial of a number n is to use a recursive approach:

n! = n x (n-1)!

Your task: Write a function to calculate n! using a recursive approach.

0! = 1


The factorial calculations given above work for any positive integer greater than 0.

There is however an exception for the value 0 as 0! = 1.

Amend your code to ensure that if the user enters the value 0, both your functions return the value 1.

Also, add some input validation to ensure that the user does not enter a negative value.

Help?


Check this blog post for some help on how to complete this challenge.

Did You Know?

Six weeks is exactly 10! seconds (=3,628,800)
6 Weeks, 7 Days per week: 6 x 7 days
6 Weeks, 24 hours per day: 6 x 7 x 24 hours
6 Weeks, 60 minutes per hour: 6 x 7 x 24 x 60 minutes
6 Weeks, 60 seconds per minute: 6 x 7 x 24 x 60 x 60 seconds
Factor some numbers: 6 x 7 x (8 x 3) x (3 x 4 x 5) x (2 x 3 x 10)
Rearrange: 10 x 3 x 3 x 8 x 7 x 6 x 5 x 4 x 3 x 2
Lastly as 3 x 3 = 9 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2

playingcards

There are 52! ways to shuffle a deck of cards.

That is 8.0658175… × 1067

unlock-access

Solution...

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

Kings & Queens of England

Portrait of Henry VIII
Portrait of Henry VIII

There have been 66 monarchs of England and Britain spread over a period of 1500 years.

For this Python challenge you will use a text file listing all of these monarchs in chronological order. You will then write a Python program to prompt the user to enter a year (between 757 and 2017) and your program will look up for the name of the monarch of England at the given date.

To complete this challenge you will need to read more about how to read through a CSV file.

The text file we will use contains one line per monarch. On each line it stores the year the reign started, the year it ended and the name of the monarch as follows:

StartYear,EndYear,Name
TextFileMonarchs-of-England.csv

Complete the Code


Testing


Once you have completed the code check that it produces the expected output by performing the following tests:

Test # Input Values Expected Output Actual Output
#1 1192 Richard I
#2 1345 Edward III
#3 1042 Harthacanut
Edward the Confessor
#4 1547 Henry VIII
Edward VI
#5 1016 Ethelred II the Unready
Edmund lronside
Cnut (Canute)
#6 512 Not Found

Extension Task:


Tweak the output of your program so that when giving the name of the monarch matching the year entered, your program also mentions who were the previous monarch and the next monarch.

For instance the output of the program could be:

In 1192, the monarch of England was Richard I. Their predecessor was Henry II up until 1189 and their successor was John whose reign started in 1199.

Video Tutorial



unlock-access

Solution...

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

Professor Snape’s Magic Potions

invisibility-potionIn this challenge we will use file handling techniques to read through and access the content of different text files.

We have saved three recipes for the following three magic potions using one text file per recipe:

  • Invisibility Potion:
    A potion that makes you invisible for a short period of time.
  • Aging Potion:
    A potion which causes the drinker to temporarily become older. The more of the potion is drunk, the larger the aging which occurs.
  • Poison Antidote:
    A potion which counteracts ordinary poisons, such as creature bites and stings.

clover
The following Python code opens the text file of the invisibility potion (invisibility-potion.txt) in read-only mode. It then uses a for loop to access and print the content of each line, one line at a time.

Your Challenge


Your challenge consists of adding a menu to this code to let the user decide which potion to display on screen. Based on their choice, the program will open the relevant text file and display the recipe on screen.

Extension Task


Add an extra text file to store this new potion:

Babbling Beverage
A potion that causes uncontrollable speaking of nonsense.

    Add 1 four-leaf clover and 3 dragon scales into a mortar
    Add 1 garlic clove and crush the ingredients
    Pour the ingredients into a copper cauldron
    Add 2 horned slugs in the cauldron
    Add 3 drops of beetroot juice
    Boil and simmer the potion for half an hour
    Stir once every 10 minutes, clockwise

Video Tutorial



unlock-access

Solution...

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

Mind the Gap

mind-the-gapWhen your browser displays a webpage on screen it uses 3 different programming languages called HTML, CSS and JavaScript. We call the combination of these three languages “client-side web technologies”.

Each of the three language has its own syntax and purpose:

  • HTML uses <tags> and its purpose is to add content on the page such as text, pictures, and video clips.
  • CSS is used to customise the look and feel of the page by defining the position of each element on the page, defining the layout of the page as well as formatting text, pictures and other components on the page.
  • JavaScript is used to add user interaction to the page. JavaScript can be linked to HTML tags through various events such as the onClick event of a button.

Each of these languages are based on web standards defined by the World Wide Web Consortium W3C. As a consequence these languages are recognised by all the main web-browsers including Google Chrome, Microsoft Edge, Modzilla Firefox, Safari, etc. on various platforms (Windows, MacOs, Linux, Android etc.)

The Following codepen provides a clear demonstration of the syntax and purpose of HTML, CSS and JavaScript and shows how JavaScript can interact with various HTML tags.

On this codepen you can click the button located below the “Mind the Gap” sign in order to change the text of the sign.

Edit this code in a new window using this button:

Your Challenge:


Add one more button to allow the user to change the background colour of the both sections of the logo (circle and label).

To do so you will need to:

  • Add a button below the poster
  • Create a new Javascript function called changeColours() and add Javascript code to prompt the user to enter two colour codes (hexadecimal format: e.g. #00FF00). Use some of the code as provided below to change the colour of the red circle or of the blue label.
    document.getElementById("red-circle").style.borderColor = "#FFFF00";
    document.getElementById("blue-label").style.backgroundColor = "#00FF00";

You can then use your web application to re-create the following signs:underground-hello-world
underground-sign

Tagged with: , ,

Keep Calm and Carry On Coding

Keep-Calm-And-Carry-On-CodingWhen your browser displays a webpage on screen it uses 3 different programming languages called HTML, CSS and JavaScript. We call the combination of these three languages “client-side web technologies”.

Each of the three language has its own syntax and purpose:

  • HTML uses <tags> and its purpose is to add content on the page such as text, pictures, and video clips.
  • CSS is used to customise the look and feel of the page by defining the position of each element on the page, defining the layout of the page as well as formatting text, pictures and other components on the page.
  • JavaScript is used to add user interaction to the page. JavaScript can be linked to HTML tags through various events such as the onClick event of a button.

Each of these languages are based on web standards defined by the World Wide Web Consortium W3C. As a consequence these languages are recognised by all the main web-browsers including Google Chrome, Microsoft Edge, Modzilla Firefox, Safari, etc. on various platforms (Windows, MacOs, Linux, Android etc.)

The Following online web editor provides a clear demonstration of the syntax and purpose of HTML, CSS and JavaScript and shows how JavaScript can interact with various HTML tags.

On this preview you can click the button located below the poster in order to change the text of the poster.

Edit this code in a new window using this button:

Your Challenge #1:


Add four more buttons to allow the user to change the background colour of the poster:

  • Blue Button: Change colour to: #3050D0
  • Yellow Button: Change colour to: #CEC110
  • Orange Button: Change colour to: #ED3300
  • Red Button: Change colour to: #DB0000

To do so you will need to:

  • Add some buttons below the poster
  • Ensure the <DIV> tag has an id attribute. e.g. id=”poster”
  • Create a new Javascript function called changeColour() and use the follwoing code
    document.getElementById("poster").style.backgroundColor = "#3050D0";

Your Challenge #2:


Add four more buttons to allow the icon used in the poster:

To do so you will need to:

  • Add some buttons below the poster
  • Ensure the <IMG> tag has an id attribute. e.g. id=”icon”
  • Create a new Javascript function called changeIcon() and use the follwoing code
    document.getElementById("icon").src = "https://www.101computing.net/wp/wp-content/uploads/html-icon.png";

Use your application to recreate the following three posters:


Keep-Calm-and-Learn-HTML
Keep-Calm-and-Learn-CSS
Keep-Calm-and-Learn-JavaScript

Tagged with: , ,

Time Guessing Game

stopwatch-micro-bit

Reaction Time Tester


Look at the code provided below. It uses the time library to store the current time (as a number of seconds) at different lines of the code.

The code is executed very quickly, normally in just a few milliseconds, one line at a time. This is called sequencing. However from time to time your code is paused, waiting for the user to input a value. This is the case in Python every time you use the input instruction.

By recording the current time before and after an input instruction, we can then calculate how long it took for the user to type an answer. This is the approach used by the following Python program used to calculate your reaction time (how quickly will you press the return key).

Time Guessing Game


Adapt this script to:

  1. Generate a random number between 1 and 10,
  2. Display the number on screen and ask the user to wait for this number of seconds before pressing the return key,
  3. Calculate how long they waited for and give the user a score as follows:
    • If the user waited for more than the given time, they went bust: 0 points,
    • If the user press the return key at the right time or up to 1/2 second earlier: 50pts,
    • If the user press the return key between 1 second to 1/2 second earlier: 25pts,
    • If the user press the return key between 2 seconds to 1 second earlier: 10pts,
    • If the user press the return key more than 2 seconds earlier: 0pts.
    unlock-access

    Solution...

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

Fast Typing Test

keyboard-typing

Reaction Time Tester


Look at the code provided below. It uses the time library to store the current time (as a number of seconds) at different lines of the code.

The code is executed very quickly, normally in just a few milliseconds, one line at a time. This is called sequencing. However from time to time your code is paused, waiting for the user to input a value. This is the case in Python every time you use the input instruction.

By recording the current time before and after an input instruction, we can then calculate how long it took for the user to type an answer. This is the approach used by the following Python program used to calculate your reaction time (how quickly will you press the return key).

Fast Typing Challenge


Adapt this script to measure how long it will take the end-user to type the entire alphabet (“abcdefghijklmnopqrstuvwxyz”).

Once the user has typed the alphabet, check that they typed it correctly. If not the user should try again until they type it right.

The program should then output:

  • How many attempts they had,
  • How long it took them in total,
  • How long it took them on average per attempt,
  • How long it took them on their last attempt (which is when they got it correct).

Extension Task


Add a menu system to offer three levels.

  • Level 1: The user has to type the alphabet,
  • Level 2: The user has to type the following quote: “The quick brown fox jumps over the lazy dog”.
  • Level 3: A random quote is picked alongside a list of the following three pangrams(*):
    • “The quick brown fox jumps over the lazy dog”
    • “The five boxing wizards jump quickly”
    • “Pack my box with five dozen liquor jugs”

(*)A pangram, or holoalphabetic sentence, includes every letter of the alphabet at least once. The most challenging pangrams are the ones with the fewest letters.

unlock-access

Solution...

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

Hardware Quiz

computer-hardwareComputers hardware consists of all the components that you will find inside the computer (Motherboard, CPU, RAM, graphic card, sound card, network card, etc.) as well as all the peripherals/devices than you can plug to a computer.

Peripherals are often categorised into three types:

  • Input Devices,
  • Output Devices,
  • Storage Devices.

  • input-output-devices-2

    Input Devices Output Devices Storage Devices
    Mouse
    Keyboard
    Microphone
    Webcam
    Scanner
    Barcode Reader
    Sensors
    Chip & Pin Card Reader
    Magnetic Stripe Card Reader
    Touchpad
    Game Controller
    Joystick
    Screen
    Projector
    Speakers
    Headphones
    Printer
    Actuators
    LED
    Magnetic Hard drive
    SSD Hard drive
    USB Key
    CD Drive
    DVD Drive
    SD Card Reader

    input-output-devices-1

    Coding Challenge #1


    Your challenge consists of creating a quiz that will randomly pick and display a peripheral and ask the end-user to decide whether the device is an input device, storage device or output device.

    The user should score 10 points per correct answer. The game should carry on as long as the user is giving a correct answer.

    Video Tutorial


    Coding Challenge #2: The odd one out


    Adapt your quiz so that it displays 4 devices on screen: 3 devices from the same category (e.g. 3 input devices) and 1 device from another category (e.g. storage device). All 4 devices should be displayed in a random order.

    The user is then asked to identify the “odd one out”. The user should score 10 points per correct answer. The game should carry on as long as the user is giving a correct answer.

    e.g. Which device is the odd one out?

    mouse, webcam, printer, keyboard

    unlock-access

    Solution...

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

Sorting Algorithms

Computers are often used to process large amounts of data. Some of the tasks they can be used for is to sort data sets in order, e.g. numerical order or alphabetical order.

Though this may seem like a simple task to complete, a lot of research has focused on finding the most effective approach to sort a list very effectively.

Different solutions have been developed resulting on a range of sorting algorithms, the most popular once being:

  • Bubble Sort Algorithm,
  • Insertion Sort Algorithm,
  • Selection Sort Algorithm,
  • Merge Sort Algorithm,
  • Quick Sort Algorithm.

The effectiveness of an algorithm results in the number of steps it takes to complete a task.

For sorting algorithms, the effectiveness of an algorithm is not just based on the algorithm itself but also on the list of data to be sorted. Some algorithms for instance will be more effective than other on small lists but not on large lists. Some algorithms will be more effective than others if the data is already nearly sorted. Some algorithms such as the merge sort algorithm will be more effective in a concurrent-processing environment as they use a divide and conquer approach.

The following animations are a visual representation of six of the key sorting algorithms:

Tagged with: