More results...

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

How many Bytes in…

binary-dataIn this challenge we will write a set of functions to calculate how many Bytes there are in a given number of kilobytes, megabytes, gigabytes, terabytes or petabytes.

First let’s investigate the link between these storage units:

how-many-bytes-in

BytesKilobytesMegabytesGigabytesTerabytesPetabytes
A Byte is a small storage unit that contains 8 bits. Here is what a Byte looks like:

01101110

A Byte can be used to store an integer between 0 and 255, or a single character (using the ASCII code).
A kilobyte (KB) contains 1024 Bytes.
1 KB = 1024 Bytes

A text file is often measured in Kilobytes as it would contain a few thousand characters.
A megabyte (MB) contains 1024 kilobytes.
1 MB = 1024 KB
= 1024 x 1024 Bytes

A digital photograph or an mp3 would typically take up a few megabytes of disk space.
A gigabyte (GB) contains 1024 megabytes.
1 GB = 1024 MB
= 1024 x 1024 KB
= 1024 x 1024 x 1024 Bytes

A high quality movie (DVD) would take up a few gigabytes. An SD card or USB key would contain a few GB of data.
A terabyte (TB) contains 1024 gigabytes.
1 TB = 1024 GB
= 1024 x 1024 MB
= 1024 x 1024 x 1024 KB
= 1024 x 1024 x 1024 x 1024 Bytes

A hard drive can contain a few Terabytes of data.
A petabyte (PB) contains 1024 terabytes.
1 PB = 1024 TB
= 1024 x 1024 GB
= 1024 x 1024 x 1024 MB
= 1024 x 1024 x 1024 x 1024 KB
= 1024 x 1024 x 1024 x 1024 x 1024 Bytes

Very large databases stored on web servers used by large websites (Google, Facebook, etc…) will contain a few Petabytes of data.

Subroutines?


By completing this challenge we will investigate how subroutines (functions/procedures) are used in Python to create sections of code that can be called/reused many times within a program. Subroutines mean that a section of code can be identified using a name (identifier). This enables us to add more instructions to the already existing functions used in Python such as print or input.

To create a new function in Python we have to declare it first using the keyword def in Python. Our first function will be called kilobytes_to_bytes() and will take one parameter: numberOfKilobytes: (integer value).

def kilobytes_to_bytes(numberOfKilobytes):

We will then add the code to calculate the number of bytes that this function should return:

def kilobytes_to_bytes(numberOfKilobytes):
      bytes = 1024 * numberOfKilobytes
      return bytes

Note how the code for the subroutine/function is indented to the right.

We will then be able to call our the kilobytes_to_bytes() function to perform any conversion. T

numberOfBytes = kilobytes_to_bytes(5)

Check the Python code below where we have implemented and used the kilobytes_to_bytes() function to convert a user input into bytes.

Your task consists of creating another set of functions as follows:

  • megabytes_to_bytes()
  • gigabytes_to_bytes()
  • terabytes_to_bytes()
  • petabytes_to_bytes()

Extension Task


hourglassCreate a new Python program that will:

  • Ask the user how old they are (age in years),
  • Convert this number in days knowing that there are 365.25 days in a year,
  • Output this number of days to the end-user.

Add more code to your program to calculate:

  • How old is the user in days,
  • How old is the user in hours,
  • How old is the user in minutes,
  • How old is the user in seconds.
unlock-access

Solution...

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

Four-in-a-row challenge!

connect4In this blog post you will use Python Code to complete this simulation of the game of connect 4. (Rules of the game)

You will first need to reverse-engineer the code provided. The code so far is used to:

  • Display the 6×7 grid using Python Turtle.
  • Randomly place tokens on the gird, taking turns (yellow and red).
  • Refresh the grid on the screen using the drawGrid() function.

The code provided uses a variable called connect4, use to store a two-dimensional array (6×7) of integer values. In Python, a 2D-array is a list of lists. Within this array, a 0 represents an empty place, a 1 represents a yellow token and a 2 represents a red token.

connect4-2d-array

Your Task:


Complete the checkIfWinner() function (from line 39) to check if after placing the token the game continues or if the player has aligned 4 tokens either in a row, column or diagonal. If so the function will return the color value of the winner (1 for Yellow, 2 for Red, 0 if no winner)

unlock-access

Solution...

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

Langton’s Ant

langton-antLangton’s Ant is a cellular automaton that models an ant moving on a grid of cells following some very basic rules.

At the start of the simulation, the ant is randomly positioned on a 2D-grid of white cells. The ant is also given a direction (either facing up, down, left or right).

The ant then moves according to the colour of the cell it is currently sitting in, with the following rules:

  1. If the cell is white, it changes to black and the ant turns right 90°.
  2. If the cell is black, it changes to white and the ant turns left 90°.
  3. The ant then moves forward to the next cell, and repeat from step 1.

These simple rules lead to complex behaviours. Three distinct modes of behaviour are apparent, when starting on a completely white grid:

  1. Simplicity: During the first few hundred moves it creates very simple patterns which are often symmetric.
  2. Chaos: After a few hundred moves, a big, irregular pattern of black and white squares appears. The ant traces a pseudo-random path until around 10,000 steps.
  3. Emergent order: Finally the ant starts building a recurrent “highway” pattern of 104 steps that repeats indefinitely.

All finite initial configurations tested eventually converge to the same repetitive pattern, suggesting that the “highway” is an attractor of Langton’s ant, but no one has been able to prove that this is true for all such initial configurations.

Source: wikipedia
LangtonsAntAnimation

Python Code (Using Python Turtle)


Below is our implementation of Langton’s Ant model using Python Turtle. Note that, on a fixed-size 2D grid, we have had to add one rule to the model:

  • On a fixed-size 2D-grid, the simulation stops when the ant reaches the edge of the grid.

Your Task


You can complete this code by adding multiple ants to this model or by starting with a random grid of black and white cells instead of an an empty grid of white cells only.

A more complex extension to this challenge is to add multiple colours to the model (not just black and white cells). See examples on this page.

Number Sequence – Finding the nth Term

Can you complete the following number sequences?

Number Sequence #1:number-sequence-2
Number Sequence #2:number-sequence-3
Number Sequence #3:number-sequence-1

Number Sequences


There are different types of number sequences. Let’s investigate the most widely used types of number sequences.
You may want to read this page first: https://www.mathsisfun.com/numberpatterns.html

Arithmetic Sequences

An Arithmetic Sequence is made by adding the same value each time. When creating an arithmetic number sequence you have to decide of a starting number (e.g. “2”) and an increment (e.g. “3”)

number-sequence-diagram-1

Geometric Sequences

A Geometric Sequence is made by multiplying by the same value each time.
When creating a geometric number sequence you have to decide of a starting number (e.g. “2”) and a multiplier (e.g. “3”)

number-sequence-diagram-2

Square Numbers

1,4,9,16,25,36…

This sequence consists of calculating the squares of whole numbers.

number-sequence-diagram-3

Incremental Sequence

1, 2, 4, 7, 11, 16, 22, 29, 37, …

This sequence is generated by adding an “increasing” number, that is a number that each time is incremented by 1 as we progress through the number sequence. Not clear? Check the diagram below.

number-sequence-diagram-4

Fibonacci Numbers

1, 2, 3, 5, 8, 13, 21, 34, …

The Fibonacci Sequence is found by adding the two numbers before it together.

  • The 3 is found by adding the two numbers before it (1+2)
  • The 5 is found by adding the two numbers before it (2+3)
  • The 8 is found by adding the two numbers before it (3+5)

number-sequence-diagram-5

Python Challenge


In this challenge, we are writing a Python script to help us find the nth term of a number sequence.

Arithmetic Sequence

We have completed the Python code to find out the nth term of an arithmetic number sequence:

Your challenge is to tweak this code to help find out the nth term of any:

  • Geometric Sequence
  • Square Number Sequence
  • Incremental Sequence
  • Fibonacci Numbers Sequence

Original Price Calculator

sale-labelsShopping during the sales can sometimes be very confusing. With discounted prices at 10%, 20%, 50% or even 70%!

For this challenge you are going to write a Python script that prompts the user to enter a discounted price in pounds (or in your own currency) (e.g. £90) and the discount rate that this price benefits from (e.g. 25%).

Your program will then calculate and display the original price (price before discount) of the item (e.g. £120).

price-after-discount

Flowchart


Your program will consist of 3 key sections: Input > Process > Output
input-process-output

The flowchart of your program is as follows:
Price-Before-Discount-Flowchart

Python Code


Your task is to complete the following Python code:

Testing


You can now use your code to complete the following test plan:

Test # Input Values Expected Output Actual Output
#1 Discounted Price: £90
Discount Rate: 25%
Original Price: £120
#2 Discounted Price: £45
Discount Rate: 40%
Original Price: £75
#3 Discounted Price: £210
Discount Rate: 30%
Original Price: £300

discounted-prices

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 – Protractor Challenge

Python Turtle? Let’s Recap!


Looking at the following code can you explain the purpose of each Python Turtle instructions:

  • turtle.color(“red”)
  • turtle.forward(100)
  • turtle.right(90)
  • turtle.left(45)
  • turtle.penup()
  • turtle.pendown()
  • turtle.goto(0,0)
  • turtle.circle(50)
  • turtle.setHeading(45)

(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:

Protractor Challenge:


A protractor is an instrument used in Maths when measuring or drawing angles.

Our challenge is to use Python to draw a protractor on screen, using all the graduations for a scale going from 0° to 180°.
protractor

To do so we will use an iterative algorithm based on the following flowchart:
protractor_flowchart

Your task consists of using Python code to implement this flowchart using Python Turtle.

Extension Task #1


Improve the drawing of your protractor further by:

  • adding labels (e.g. 0°, 10°, etc.) next to each graduation,
  • adding smaller graduations (e.g. 5°, 15°, 25° etc. graduations).

Extension Task #2


Use a similar approach to draw an analogue clock using Python Turtle.
unlock-access

Solution...

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

Area Calculator Flowchart Challenge

In this challenge you will design an algorithm to calculate the area of various shapes as listed below:

Shape Name Area
area-square Square width2
area-rectangle Rectangle width x length
area-circle Circle pi x radius2
area-triangle Triangle base x height / 2

Your algorithm should:

  • Ask the user which of the above four shapes they would like to calculate the area of,
  • Based on the chosen shape, your algorithm should ask the end user to enter the required dimensions (e.g. width, or width and length or radius or base and height),
  • Calculate and display the area of the chosen shape.

Flowchart Challenge


Use our flowchart desing tool to create the flowchart of your algorithm.

Online Flowchart Creator

Python Challenge


Once you have completed your flowchart, you can implement this algorithm using Python code.

Test Plan

Test # Input Values Expected Output Actual Output
#1 Square
Width: 5
Area: 25
#2 Rectangle
Width: 4
Length: 6
Area: 24
#3 Circle
Radius: 10
Area: 314.159
#4 Triangle
Base: 7
Height: 4
Area: 14

Extension Task


Complete your algorithm to cater for additional shapes including parallelograms, rhombus and hexagons.
unlock-access

Solution...

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

Min, Max, Mean, Median and Mod – Flowcharts

In this challenge we will design algorithms used to calculate the Min, Max, Mean, Median and Mod from a list of numbers.

First you may want to refresh your maths skills and check the meaning of these terms: http://www.purplemath.com/modules/meanmode.htm

MIN Flowchart


min-flowchart

Challenge #1


Use our flowchart designer tool to create 4 more flowcharts to:

  • Calculate the Max value of a given list,
  • Calculate the Mean value of a given list,
  • Calculate the Median value of a given list,
  • Calculate the Mod value of a given list.
Online Flowchart Creator

Challenge #2


Now that you have designed your algorithms you can implement these using Python code.

unlock-access

Solution...

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

Fizz-Buzz Game Algorithm

fizz-buzz-iconFizz-Buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any multiple of three with the word “fizz”, and any multiple of five with the word “buzz”.

Fizz-Buzz Challenge


For this challenge you need to write a computer program that will display all the numbers between 1 and 100.

  • For each number divisible by three the computer will display the word “fizz”,
  • For each number divisible by five the computer will display the word “buzz”,
  • For each number divisible by three and by five the computer will display the word “fizz-buzz”,

This is what the output will look like:

    1
    2
    Fizz
    4
    Buzz
    Fizz
    7

Fizz-Buzz Algorithm


Check the following flowchart used to generate the first 100 numbers using the Fizz-Buzz rules. (Click on the flowchart to enlarge it).
flowchart-fizz-buzz

Python Challenge


Use the above flowchart to complete this Python code.

Extension Task #1


Tweak this code to use the input command in Python. The program should ask the user to input numbers in order or type the word Fizz or Buzz when required. The program should check that the user has typed the right number or the right word and stop if not. It should then give the final score (How far the player completed the game).

Extension Task #2


Improve this code further to give the players three lives. The player can make up to 3 mistakes. Each time they make a mistake they lose 10 points but carry on playing till they lose their three lives.

Finding the factors of…

For this challenge you will use an algorithm to find all the factors of a given number.

Factors are numbers we can multiply together to get another number. For instance, factors of 15 are 1, 3, 5 and 15, because 1×15=15 and 3×5 = 15.

Your algorithm will be based on the INPUT – PROCESS – OUTPUT model:

  • INPUT: Ask the user to enter a number,
  • PROCESS: Identify all the factors of this number,
  • OUTPUT: Display all the factors of this number.

Flowchart


flowchart-factors-of-a-number

Task 1: Python Code


Your task is to implement your algorithm using Python code.

Task 2: Test Plan

Test # Type of Test Input Values Expected Output Actual Output
#1 Valid 12 1,2,3,4,6,12
#2 Valid 21 1,3,7,21
#3 Valid 48 1,2,3,4,6,8,12,16,24,48
#4 Valid 13 1,13

Task 3: Extension Task: Prime Number?

A prime number is a number that has exactly two factors (1 and itself). Which means that a prime number can be divided evenly only by 1, or itself. A prime number must be a whole number greater than 1.

How could you tweak this code to detect if a number is a prime number or not. If a number is a prime number your algorithm should display a message saying so.

Task 4: Extension Task: Simplifying a Fraction


Complete the following two challenges:

unlock-access

Solution...

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