More results...

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

Page Rank Algorithm – Take the Quiz

Check your understanding of the page rank algorithm used by search engines such as Google to sort search results.

To find out more about this algorithm, read this blog post first:
Search Engine Indexing and Page Rank Algorithm

Quiz #1


QuestionSolution
Find the page rank score of web page B:
page-rank-a-quiz
Page Rank Score for web page B = 14
page-rank-a-solution

Quiz #2


QuestionSolution
Find the page rank score of web page B:
page-rank-b-quiz
Page Rank Score for web page B = 12
page-rank-b-solution

Quiz #3


QuestionSolution
Find the page rank score of web page A:
page-rank-c-quiz
Page Rank Score for web page A = 14
page-rank-c-solution

Search Engine Indexing and Page Rank Algorithm

Search Engines Indexing


Search engines like Google maintain huge databases called “indexes” of all the keywords and the web addresses of pages where these keywords appear.

When a web designer creates a new website they can contact the search engine to let them know they would like their web page to be scanned and added to the search engine index. They can do so by completing an online submission form. (e.g. https://www.google.com/webmasters/tools/submit-url)

Search engines also use “bots” (software robots) called web-bots or spider bots that constantly (24/7) crawl the web to scan webpages, update the index, follow hyperlinks to move from one page to the other. They can hence find new pages if these have been linked to existing pages that have already been indexed.

spider-bots

In their indexes, for each URL that has been indexed, a page rank score is also stored. This is a number that will be used to sort search results when displaying these to the end user.

Page Rank Algorithm


When a user uses a search engine (e.g. Google) the following steps take place:

  • A user submits a search query using Google’s search engine.
  • Google searches all of the pages/URLs it has indexed for relevant content. (based on keywords)
  • Google sorts the relevant pages/URLs based on PageRank scores.
  • Google displays a results page, placing those pages/URLs with the most PageRank (assumed importance) first.

So we can define the page rank score as a numerical value, calculated by a search engine for each page on the web to measure its degree of importance.

The page rank of a page is regularly updated when the spider bots of a search engine crawl the web.

Page Rank Formula?

Google does not disclose its exact PageRank formula. But it is a pretty safe bet that calculating PageRank is not easy math. The key concept of this formula though is as follows:

PageRank for a given page = Initial PageRank + (total ranking power of a page / number of outbound links of this page) + …

Where the total ranking power is calculated by adding the Page Rank score of all web pages that links to your own web page divided by the number of outbound links this page have. (“+…” means repeat this formula for each single page that links towards your page.)

Which means:

  • The more web pages link towards your web page, the higher the page rank score for your page.
  • The higher the page rank score of pages linking towards your web page, the higher the page rank score for your page.
  • If a webpage has a high page rank score but also have hundreds of outbound links it will not give you much page rank score. (e.g. This is to minimise the impact of “link sharing” to artificially boost your page rank score).

The following diagram shows how the page rank score of pages A to G is calculated. Each arrow on this diagram represents a hyperlink from one page to another page.

page-rank-algorithm

Page Rank Algorithm – Take the Quiz

Nowadays the Page Rank algorithm used by Google is based on a more complex formula that takes into consideration a wide range of other factors such as:

  • Is your website layout responsive / mobile friendly?
  • Is your website secure? (e.g. Using the https protocol)
  • Is your website regularly updated?
  • How long does it take for your web page to load on average?
  • etc.

SEO: Search Engine Optimisation


seo-search-engine-optimisation

SEO refers to a range of techniques used to increase your visibility on major search engines. When designing and editing websites, web designers and web authors try to:

  • Use keywords rich web addresses (domain names, files names for html pages and picture files).
  • Use meta tags in each html page (Header section, invisible to the user but used by search engines).
  • Use a lot of relevant keywords in the text of the web pages.
  • Increase their page rank score by getting other websites to include hyperlinks to their website:
    • Registering their website on business directories.
    • Offering link exchange/sharing with other websites.
    • Sharing links towards their website on social networks.

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: