More results...

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

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.

unlock-access

Solution...

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

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

Bidding Process – Flowchart

auction-bidFor this challenge we will consider the bidding process used within online auction websites such as eBay.

Our aim is to create a system that will accept bids from the end-user, check if the bid being placed is greater than the current bid and if so, update the value of the current bid.

Here is the flowchart for our Bidding System: (Click on this flowchart to open in new window)
ebay-flowchart

Making Predictions


Check the above flowchart to understand how this algorithm will work. Based on this flowchart can you answer the following questions?


?
What will be the first question asked to the end user?


?
How does this algorithm decide if a bid is a valid bid or not?


?
How does this algorithm decide when the bidding process ends?


?
What will be the last message displayed to the end-user?

Python Challenge


Your challenge is to use Python code to implement this algorithm.

Extension Task


Check the following two blog posts to see how this code could be improved further by adding validation routines when capturing user inputs. Use these techniques to make your bidding system more robust:

unlock-access

Solution...

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

Tagged with:
Privacy Overview

This website will store some information about your preferences on your own computer inside a tiny file called a cookie. A cookie is a small piece of data that a website asks your browser to store on your computer or mobile device. The cookie allows the website to remember your actions or preferences over time.

You can delete all cookies that are already on your computer, and you can set most browsers to prevent them from being placed. However, if you do this, you may have to manually adjust some preferences every time you visit a site, and some services and functionalities may not work.

Most browsers support cookies, but you can set your browser to decline them and can delete them whenever you like. You can find instructions here for how you can do that on various browsers.

This website uses cookies to:

  1. Identify you as a returning user and to count your visits in traffic statistics analysis
  2. Remember your custom display preferences (such as light/dark theme option)
  3. Provide other usability features, including tracking whether you have already given your consent to cookies

Enabling cookies is not strictly necessary for the website to work but it will provide you with a better browsing experience.

The cookie-related information is not used to identify you personally and is not used for any purpose other than those described here.

There may also be other types of cookies created after you have visited this website. This site uses Google Analytics, a popular web analytics service that uses cookies to help to analyse how users use the site. The information generated by the cookie about your use of this website (including your IP address) will be transmitted to and stored by Google on servers in the United States. Google will use this information for the purpose of evaluating your use of other website, compiling reports on website activity, and providing other services relating to website activity and internet usage. Google may also transfer this information to third parties where required to do so by law, or where such third parties process the information on Google’s behalf. Google undertakes not to associate your IP address with any other data held by Google.