More results...

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

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 locate 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.

See the Pen pigpen cipher – encoder by 101 Computing (@101Computing) on CodePen.


Press the “Edit on CodePen” button at the top of this codepen to open in full screen mode

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

Is my credit card valid?

debit-cardsIn this challenge we will use the Luhn Algorithm to check if a debit card or credit card number is a valid card number.

This method is used every time you scan or enter your credit card number (e.g. when paying online) to check if your card number is a valid credit card number. This method can be used to quickly detect credit card numbers that have been mistyped or to detect when someone is trying to enter a fake/made up credit card number. Note that it’s not 100% efficient, so is only used as a pre-check to deter some invalid card numbers. If this first test passes, other more robust checks will be performed for the online transaction to be approved.

The Luhn Algorithm


The Luhn Algorithm consists of 4 key steps:
Luhn-Algorithm

Python Challenge


Write program that will ask the end-user to enter a 16-digit card number.
Your program will then apply the Luhn algorithm to decide and output whether or not this card number is valid.

Valid or Invalid Card Numbers?


Test your program with the following card numbers to find the 2 invalid card numbers:

# Card Card Number Valid or Invalid?
#1 credit-card-1 4137 8947 1175 5904
#2 credit-card-2 6499 8024 5027 3568
#3 credit-card-3 8504 1721 9127 3888
#4 credit-card-4 0043 6687 8348 5480
unlock-access

Solution...

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

Cryptographic Challenge: Base64 to Hex

cryptography

ChallengeBase64Hexadecimal
Your challenge is to write a piece of code, using the programming language of your choice, to code and to decode messages encrypted using Base 64 encoding and Hexadecimal (Base 16) encoding.

Your progamme will first ask the user if they want to encode or decode a message. It will then ask the user to enter either the plain text (using the base64 character set) or the ciphertext (hexadecimal).

Your program will then output the decoded (plaintext) or the encoded message (ciphertext).

For instance:
Plain:  Code/Breaker
Cipher:  0A875EFC1ADE6A47AB

The following diagram will help you understand how the conversion from Base 64 to Hexadecimal works:
cryptography-base64-hexadecimal

Code Breaking Challenge:
You will then be able to decrypt the following encoded message:

Cipher:
Plain Text:

Base64

Base64 is a binary-to-text encoding scheme where each character (plaintext) is coded using 6 bits of data. Hence a base64 character set contains exactly 64 characters:

Index Char Index Char Index Char Index Char
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

Hexadecimal


Hexadecimal is a base-16 number system. The hexadecimal numbers are 0-9 and then use the letters A-F to represent the decimal values 10 to 15.

Each digit in hexadecimal can be coded in binary using 4 bits as follows:

Binary Decimal Hexadecimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F

Extension Task


Using the same approach, we can encode message in a range of encoding schemes such as Hexadeximal (4-bits per character), Base32 (5 bits per character), Base64 (6 bits per character), Extended ASCII (8 bits per character).

You can now update your code to cater for a range of encoding/decoding techniques between all these encoding schemes.

Tagged with: ,

Name the Logic Gate

Look at the logic gates diagrams below. For each of these diagrams, complete the Truth Table corresponding to the diagram. Can you name the logic gate that each diagram is equivalent to?

Diagram #1Diagram #2Diagram #3Diagram #4Diagram #5Diagram #6Diagram #7
What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:


What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:

What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Output
0
1

Logic Gate:


What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:


What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:


What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:


What logic gate is this diagram equivalent to?

What logic gate is this diagram equivalent to?

Truth Table:

Input A Input B Output
0 0
0 1
1 0
1 1

Logic Gate:


Check Your Answers


You can now recreate these logic gates circuits using our online logic gates circuit simulator to verify your answers.

Tagged with:

Intersection Point

intersection-point-two-linesThe aim of this challenge is to write a script that allows the user to input the equation of two straight lines (Line 1: y=ax+b, Line 2: y=cx+d). The program will then calculate the coordinates of the intersection point if such a point exists!

Flowchart


intersection-point-flowchart

Your Code


Complete your code here, using the steps described on the above flowchart.

Testing


Once your code is complete, you can check it works by completing the following tests:

Test # Input Values Expected Output Actual Output
#1 a = 5
b = -10
c = -3
d = 22
(4,10)
#2 a = 0
b = 20
c = 2
d = -50
(35,20)
#3 a = -1
b = 0
c = 2
d = 0
(0,0)
#4 a = 5
b = 10
c = 5
d = 30
Both lines are parallel

Using Python Turtle to trace the lines!


The following code uses Python Turtle to trace both lines.

Extension Task


Can you write a computer program that takes the (x,y) coordinates of two different points and calculates the linear equation (y = ax + b) of the straight line joining these two dots.

To do so you will need to:

  • Retrieve four inputs: x1, y1, x2, y2 (The coordinates of the two points to join)
  • Calculate the slope of the straight line: a = (y2 – y1) / (x2 – x1)
  • Calculate b = y1 – ax1
  • Display the linear equation: y = ax + b

Note that in case of a vertical line (if x1 == x2), you will not be able to calculate the slope a. The equation of your straight line would then be x = x1.

unlock-access

Solution...

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