More results...

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

Epoch/Unix Timestamp Converter

date-timeThe Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight GMT).

This explains how date & time values are actually stored on computers: using an integer value representing the number of seconds since 01/01/1970 00:00:00 GMT. Note that a negative value would represent a date prior to 01/01/1970.

For instance, the current date & time is:

Human Readable Date to Epoch Date Converter


Your first challenge is to write a script, using a programming language of your choice, to convert a date entered in a human readable format e.g. DD/MM/YYYY HH:MM:SS as an input and output the date using the Epoch/Unix Timestamp.

To do so you will need to calculate the number of seconds elapsed between the date given and 01/01/1970 00:00:00.

To simplify your calculations you can make the following assumptions:

  • There are 365.25 days in a year.
  • There are 30.44 days in a month (on average).
  • There are 24 hours in a day.
  • There are 60 minutes in an hour.
  • There are 60 seconds in a minute.

You can test you script with today’s date and compare the output of your program with the Epoch date that appears at the top of this blog post.

Epoch Date to Human Readable Date Converter


Your second challenge is to work out and output an actual date in the DD/MM/YYYY HH:MM:SS format from an Epoch timestamp.

Testing


Here are some key dates for you to test your scripts:

Epoch/Unix Timestamp Date Event
410270400 1 January 1983 ARPANET adopted TCP/IP on January 1, 1983, and from there researchers began to assemble the “network of networks” that became the modern Internet. The online world then took on a more recognizable form in 1990, when computer scientist Tim Berners-Lee invented the World Wide Web.
479736000 15 March 1985 The first domain name registered was Symbolics.com. It was registered March 15, 1985, to Symbolics Inc., a computer systems company in Cambridge, Massachussetts.
773409600 05 July 1994 Amazon was founded in the garage of Bezos’ rented home in Bellevue, Washington. In July 1995, the company began service as an online bookstore.
904910400 4 September 1998 The search engine Google was invented by computer scientists Larry Page and Sergey Brin. It was named after a googol (the name for the number 1 followed by 100 zeros) found in the book “Mathematics and the Imagination” by Edward Kasner and James Newman.
1075896000 4 February 2004 The social network Facebook was launched on February 4, 2004, by Mark Zuckerberg, along with fellow Harvard College students and roommates Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes

Using the time library


Note that, in Python, the time library already includes a few functions to manipulate Unix Epoch timestamps. You can for example use this library to convert a Unix Epoch Timestamp into a human readable format, using the following code:

unlock-access

Solution...

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

Hashing Algorithms for Integrity Validation

The enigma machine was used during World War II to encrypt secret messages.

The enigma machine was used during World War II to encrypt secret messages.

Imagine working for the British Secret Services during World War 2 or during the cold war. As part of your role, you would be expected to exchange secret messages with your allies.

Your messages would most likely be encrypted using various encryption techniques.

The issue is that, occasionally, you may receive messages but may not be 100% sure that these messages are genuine messages or not. It could be that your enemies are sending messages pretending to be one of your allies. They may use the same encryption techniques to lure you. Also, your enemies may have intercepted some of your messages and altered them to confuse you.

This is the reason why secret services had to come with a solution when sending and receiving messages to validate the integrity of a message: So, when a message is received, the recipient should be 100% confident that:

  • the message has been issued by the right person,
  • the message has not been tampered with before reaching its destination.

In the 1950’s the first hashing algorithms used to validate the integrity of a message were introduced. They were using the idea of using a complex mathematical calculations on the content of a message (the key) to generate a hash called a checksum that would be appended at the end of the message to be sent.

hashing-algorithm-checksum

Let’s consider the following secret message:

secret-message-sunday

Let’s consider a very basic hashing algorithm that takes this message as an input and returns the hash as being the number of characters of this message.

checksum = hash(message) = LENGTH(message)

When applying our hashing algorithm to our secret message we get a checksum of 23.

hashing-algorithm-sunday

We will now append this checksum at the end of the message before sending it. Our new message is now:

hashing-algorithm-sunday-23

The recipient of this message will run the same hashing algorithm with the content of the message. They will then compare the resulting checksum with the checksum that has been received. If the two checksums are equal, they can be fairly confident that the message is genuine.

If an enemy intercepts the message and tries to alter it, they will not know how the checksum was calculated. Hence they may change the content of the message but will not be able to recalculate the right checksum. (Provided that your hashing algorithm is not as obvious as calculating the number of characters in the message: This would be a very easy algorithm to guess and recreate).

A message which has been tampered with by an enemy may look like this:

A message which has been tampered with.

A message which has been tampered with.

hashing-algorithm-23-24When the recipient receives this message, they will apply the same hashing algorithm and get a checksum of 24. When comparing this with the checksum of the received message they will realise that both checksums do not match and hence will be able to identify that the message is invalid: It has either been produced by someone who does not know the hashing algorithm in use, or it has been tampered with before reaching its recipient. In both case the recipient will have to discard this message as it is not reliable.

Hashing Algorithms for Integrity Validation


To summarise what we have learned so far, the idea of a hashing algorithm used for integrity validation is to provide some assurance that a transferred message or file has arrived intact, that it has not been altered on its way to the recipient.

Note that alterations can be caused intentionally by a third party (e.g. a hacker) or can be the consequence of an unintentional “glitch” in the communication. (e.g. poor quality of communication link such as wifi interference, collisons of data packets on a TCP/IP network, human or sensor error when inputting a message or scanning a barcode, etc.)

Nowadays hashing algorithms used for integrity validation are widely used in a range of contexts such as:

  • Barcodes and ISBN book numbers use a similar approach called check digit.
  • The CSV number of a credit card is also a form of checksum used to validate credit cards.
  • The TCP/IP protocols (HTTP, FTP, SMTP, etc.) all use a checksum on all data packets being sent over the Internet, to ensure that the recipient can validate the integrity of the data packets being received.
  • Digital certificates and software licences also use a checksum to minimise the risk of fraudulent digital certificates and software license keys.
  • exe files of popular software include a checksum. This is to prevent malicious websites trying to get you to download software where the content of the exe file has been altered to add a virus or a trojan horse.

The hash of a hashing algorithm used for integrity validation is often called a checksum and is appended at the end of the data to be transferred.

Sometimes the hash is called a check digit if it only consists of one digit. This is the case for barcodes, ISBN numbers and credit card numbers where the last digit of the code is a check digit, the result of a complex calculation using all the other digits of the code.

You can check the following links to investigate the use of check digits on a barcode or on a credit card (using the Luhn Algorithm to validate a credit card number).

Hashing Algorithms for Storing Sensitive Data

passwordsMore and more online systems such as e-commerce websites, online banking apps, or social networks need to access some sensitive data about you including your password, your credit card details and more recently, some biometric data (e.g. for fingerprint authentication process).

The servers that store your personal data are at risk of being hacked and though the organisations who run these online services invest a lot of money in ensuring their servers are secure (firewalls, encryption of data, etc.) there is always a risk of a hacker accessing the data stored on their servers. This is why organisations prefer not to store very sensitive data directly (passwords, credit card numbers and biometric data). Instead these organisation use complex hashing algorithms to store hash values of your most sensitive data.

Hashing Algorithm


A hashing algorithm is a complex mathematical calculation that takes an input called the key (e.g. your credit card number or your password) to generate a hash value. The hash value often consists of a string of characters of a fixed size (e.g. 32 alpha-numeric characters).
hashing-algorithm
The hashing algorithm will always produce the same hash for a given key. The hash value will be fairly unique (different for each key, though on rare occasions two different keys can produce the same hash, this is called a collision).

When a user enter their password, for instance to login, the same hashing algorithm is used to recreate the hash value. This hash value can then be compared to the hash stored on the server for this user. If they are the same we can assume that the password entered by the user is correct.
hashing-algorithm-password

This works exactly the same with credit card numbers. When asked to pay online, the user enters their credit card number. The hashing algorithm is applied to the key (credit card number) to produce a hash value which can be compared with the hash value stored on the system for that user. If the two hash values are identical, we can assume that the credit card number entered is correct.

one-way-road-sign

A one-way process


Hash functions are one-way functions which means that with the key you can calculate the hash value, however with a hash value you cannot determine the key.

This is an essential characteristic of hashing algorithms which ensures that even if a hacker manages to access the organisation’s database, they will not be able to easily work out your password or credit card numbers from the hash values stored on the database.

Popular Hashing Algorithms


If you want to find out more about some of the most popular hashing algorithms you can investigate the following hash functions:

Hashing Algorithms for Memory Addressing

In this blog post, we will investigate the use of hashing algorithms to quickly locate a record in a large database.

Let’s consider the database of members of a social network such as Instagram, Twitter or Facebook.

Every time the user uses the website or their smartphone app to access the social network, the server retrieves the user’s login name and password. It then has to find the record matching the given username to access their data and verify their password.

login-process

Large social networks have millions of members. Locating a single record in a table that contains millions of records can be time consuming especially if you perform a linear search to locate the record. (Linear Big O Notation). Alternative algorithms such as a binary search can speed up the process (Logarithmic Big O Notation), however a binary search only works when the data is sorted in alphabetical order. This is unlikely to be the case for a social network members table as members will have joined the network at different dates, hence the data will not be sorted in alphabetical order of their usernames. The assumption is that the records are stored in chronological order (based on the date they first signed in).

Using a Hashing Algorithm


To solve this problem, social networks use hashing algorithms when storing and accessing data in a large database.

A hashing algorithm is a complex mathematical calculation that takes an input (a.k.a. the key) (in this case the username of the member) and returns a value called a hash value or hash. When used for memory addressing the hash value generated is the memory location of where the record is stored.
hashing-algorithm-for-memory-addressing

Let’s consider a very basic hashing algorithm used to identify the memory location of a new member who has just signed up.

Our hashing algorithm will add up all the ASCII values of each character of their username. We will assume our database will contain around 50 memory locations. So our hashing algorithm will use the remainder of dividing the total ASCII value by 50 to get a unique number between 0 and 50.

For instance, for a user called James Bond whose username is “Bond”, the resulting hash value would be:

hashKey("Bond") =  (ASCII("B") + ASCII("o") + ASCII ("n") + ASCII("d")) MOD 50
                =  (  66       +     111    +     110     +     100   ) MOD 50
                =  387 MOD 50
                =  37

The details can be stored on location 37 of the members table (hash table) provided below.
hashing-algorithm-for-memory-addressing-example

Every time the user James Bond accesses the social network, he will provide his username. As part of the login process, the server will use the hashing algorithm on this username to quickly calculate the memory location of where James Bond’s record is stored. This is a very efficient approach to access the data. (Constant Big O notation).

collision

Collisions


The hash values generated by a hashing algorithm should be fairly unique. However there will be occasions where two input values return the same hash value. For instance, let’s consider what would happen if a new user, Austin Powers signs in and their username is aPowers:

hashkey("aPowers") =  (ASCII("a") + ASCII("P") + ASCII ("o") + ASCII("w") + ASCII("e") + ASCII("r") + ASCII("s")) MOD 50
                   =  (  97       +     80     +     111     +     119    +    101     +     114    +   115     ) MOD 50
                   =  737 MOD 50
                   =  37

This hash is the same as the hash generated for James Bond’s username. This create a collision. In this case, instead of overwritting the content of memory location 37 (already used by James Bond) the algorithm will jump to location 37 and carry on a linear search from there to find the next empty memory location. This could be memory location 38 if it’s empty, 39 if not, and so on till an empty location is found.

This will slightly slow down the process when trying to locate Austin Powers’ record but would still be far more efficient than without using a hashing algorithm.

Complex hashing algorithms are designed the minimize the risk of collisions: The effectiveness of a hashing algorithm is based on the total number of unique values the algorithm will generate to minimize the risks of collisions.

Hashing Algorithm in Action!


We have implemented our basic hashing algorithm as described above.

Your task is to use it to generate the hash values for these new members of our social network.
Using these hash values, you will then be able to add their information on the members hash table (see “Members Table” tab below). You will notice that a few members have already been stored in this table.

Warning: On occasion the generated hash may generate a collision with an existing member. In this case you you will have to use the next available location to store the members detail in the members table.

The new members to add are:

  • Jason Bourne, username: jBourne
  • Johny English, username: English
  • Lara Croft, username: lCroft

Also, a few users are logging in. Can you use the hashing algorithm to quickly locate their records in the members hash table and access their details.

The users are:

  • jBauer
  • eHunt
  • Holmes
Hashing AlgorithmMembers Table / Hash Table

Memory Location Username Firstname Lastname
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

Recursive vs. Iterative Palindrome Check

A word, phrase, or sequence that reads the same backwards as forwards, e.g. madam.

A word, phrase, or sequence that reads the same backwards as forwards, e.g. madam.

For this challenge we will investigate two algorithms used to find out if a word is a palindrome or not. The first algorithm will use an iterative approach, the second algorithm will use a recursive approach.

Iterative Approach


An iterative approach is based around the use of a loop which can be:

  • A count-controlled loop (e.g. FOR loop)
  • A condition-controlled loop (e.g. WHILE loop or REPEAT UNTIL loop)

For our iterative palindrome check algorithm, we will use a loop to check all the letters in the first half of the word and compare them with the letters in the second half of the word (in reverse order). If they all match then the word is a palindrome.

Recursive Apporach


A recursive function is a function that:

  • Includes a call to itself,
  • Has a stopping condition to stop the recursion.

For our recursive palindrome check algorithm, we will use a function that:

  • Checks that the word is at least two characters long:
    • If the word is less than two characters then the function will stop the recursion (stopping condition) and Return True as the word is a palindrome.
    • If the word is two or more characters long, the function will check that the first and last letter of the word are the same:
      • If they are the same, the function will extract the word in the middle (remove the first and the last letter) and call itself with this new shortest word.
      • If they are different, then the word is not a palindrome. The function will stop the recursion here (stopping condition) and return False.

You can visualise/trace this recursive function on recursionvisualizer.com

Your Task


Complete the following factorial challenge using both an iterative approach and a recursive approach.
unlock-access

Solution...

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

Blackbeard’s Hidden Treasures

pirate-flagThe period of the late 17th and early 18th centuries is known as the Golden Age of Piracy. During this period the most notorious and the most feared of all pirates was Blackbeard. There are many legends based around Blackbeard’s piracy acts in the Caribbean sea and in the Pacific Ocean but the most captivating legend is based around Blackbeard’s great buried treasure, which has yet to be found.

A small treasure chest, full of parchments has recently been found by a team of historians who were researching the Golden Age of piracy at The Codrington Library, Oxford UK. The parchments were hidden within the inside cover of an ancient book about Myths and Legends of the Golden Age of Piracy.

It would seem that Blackbeard may not have buried his treasure in one single location but may instead have split his treasure and buried it using twelve different locations.

Your task is to use the 12 parchments, as well as the treasure map provided below to locate all the content of Blackbeard’s treasure.


Blackbeard’s TreasuresOpen 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

Blackbeard’s Treasure Map

message-in-a-bottleWe have all heard of the famous English pirate called Blackbeard who sailed the seven seas during the XVIII century. Through his numerous acts of piracy, Blackbeard accumulated a huge collection of riches including golden coins, jewels, golden plates and precious stones. Blackbeard was wise enough not to carry his treasure on his vessel. Instead he buried his treasure in a secret location, somewhere in the middle of the Pacific Ocean.

Recently, a team of SCUBA divers found a message in a bottle while SCUBA diving near Coral Bay, Western Australia. Inside this old bottle of rhum, they found two pieces of parchment believed to have belonged to Blackbeard.

The first piece of parchment is a map. Looking at the shapes of the three islands on the map, the SCUBA divers have been able to identify the location of these islands somewhere a few miles off the coast of Australia. They strongly believe that this map will help them find the exact location of Blackbeard’s treasure.

Blackbeard-Treasure Map

The second parchment is believed to be the key to help locate the position of the treasure on the map.
blackbeard-clue

Treasure Hunt


Your task is to write a computer program, using the programming language of your choice, in order to find the row number and the column number to solve the following equation:

Row x Column = 1889121

Looking at the map, we can deduce that the row number could be any number between 2440 and 2470 and that the column number could be any number between 750 and 770.

Your computer program will enable you to pinpoint the exact location of Blackbeard’s treasure on this map!

Once you have found your row and column numbers type them below to check if you have located the treasure!

Row:
Column:



unlock-access

Solution...

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

The Pigpen Cipher

pigpen-cipher-key
The Pigpen cipher (a.k.a. tic-tac-toe cipher) is a geometric substitution cipher, which exchanges letters for symbols which are fragments of a grid.

Secret Message


Using the key provided on the right, can you decode the following secret message?
pigpen-secret-message
 

Pigpen Cipher Encoder


You can use our online Pigpen Cipher Encoder to encode your own secret messages.

Access tis encoder in a new window using this button:

Tagged with: ,

The Rail Fence Cipher

rail-fenceThe rail fence cipher (sometimes called zigzag cipher) is a transposition cipher that jumbles up the order of the letters of a message using a basic algorithm.

The rail fence cipher works by writing your message on alternate lines across the page, and then reading off each line in turn.

For example, let’s consider the plaintext “This is a secret message”.
rail-fence-cipher-plaintext

To encode this message we will first write over two lines (the “rails of the fence”) as follows:
rail-fence-cipher-encoding
Note that all white spaces have been removed from the plain text.

The ciphertext is then read off by writing the top row first, followed by the bottom row:
rail-fence-cipher-ciphertext

Your Challenge


For this challenge, you will have to write two python programs, one to encrypt a message (plaintext to ciphertext), one to decrypt an encoded message (ciphertext to plaintext). To help you with this challenge we have created the flowcharts of both the encoder and the decoder algorithms.

Encoder AlgorithmDecoder Algorithm

Rail Fence Cipher – Encoder


Rail Fence Cipher - Encoder Algorithm

Rail Fence Cipher – Encoder Algorithm

Python Code



Rail Fence Cipher – Decoder


The Rail Fence Cipher - Decoder Algorithm

The Rail Fence Cipher – Decoder Algorithm

Python Code



Break the code!


Use your python script to decipher the following encoded message:

CYTGAHITEROWIIGROVNCDSRPORPYSHATFRTNOSLIGOE

Extension Task


More complex Rail Fence Ciphers have more “rails”. For instance instead of writing the code over two lines (“rails”) you can write over three or four or more lines. The number of lines used in a Rail Fence Cipher is called the key.

Key = 3

A Rail Fence Cipher with 3 "rails" (Key = 3)

A Rail Fence Cipher with 3 “rails” (Key = 3)

Key = 4

A Rail Fence Cipher with 4 "rails" (Key = 4)

A Rail Fence Cipher with 4 “rails” (Key = 4)

Investigate how you could adapt both your encoding and decoding python programmes to enable to encrypt and decrypt messages with different keys.

unlock-access

Solution...

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

Semaphore Code Using Python Turtle

flag-semaphoreFlag semaphore is a telegraphy system conveying information at a distance by means of visual signals with hand-held flags. Information is encoded by the position of the flags. The current flag semaphore system uses two short poles with square flags, which a signalman holds in different positions to signal letters of the alphabet and numbers. The signalman holds one pole in each hand, and extends each arm in one of eight possible directions. At sea, the flags are coloured red and yellow, while on land, they are white and blue.

The following table shows all the different flag positions to represent the 26 letters of the alphabet and the number digits 0 to 9:

Click on the picture to zoom in

Click on the picture to zoom in

Python Turtle Code


We have created an animation that will scan all the characters of a message and animate the signalman to display the right semaphore flags for each character in the message.

For this demo, the message variable is set on line 45 to “ABCDEFGHIJKLMNOPQRSTUVWXYZ” but you can change this message to any text.

message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Our code also uses a dictionary called semaphore to store the 26 characters of the alphabet (and the space character). For each key of this dictionary, we store the two angles used to define the position of both arms using the following angles:
semaphore-angles

A dictionary is used to store the angles of both flags, for each character of the alphabet.

A dictionary is used to store the angles of both flags, for each character of the alphabet.

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.