More results...

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

Padlock Code Challenge #8

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = The largest 3-digit prime number.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #7

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = The largest 3-digit square number.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #6

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = Total number of 3-digit combinations where one digit is equal to the sum of the other two digits.

e.g. 134 and 909 count as valid combinations whereas 124 or 569 are invalid combinations.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #5

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = Total number of 3-digit combinations where at least two digits are equal.

e.g. 030 and 558 count as valid combinations whereas 123 or 468 are invalid combinations.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #4

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = Total number of 3-digit combinations where the sum of all three digits (digit1 + digit2 + digit3) is an odd number

e.g. 034 and 555 count as valid combinations whereas 123 or 468 are invalid combinations.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #3

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = Total number of 3-digit combinations where digit1, digit2 and digit3 are all even numbers

e.g. 024 and 886 count as valid combinations whereas 124 or 456 are invalid combinations.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #2

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = Total number of 3-digit combinations where digit1 < digit2 < digit3

e.g. 123 and 358 count as valid combinations whereas 321 or 011 are invalid combinations.

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Padlock Code Challenge #1

padlock-lockedWelcome to our new series of padlock code breaking challenges.

For each of these challenges you will need to write a computer program to work out the padlock code (3-digit number). Your program will need to output the solution of the given hint. You will then be able to input this combination onto the padlock to see if it unlocks it!

Padlock Hint

code = 1 + 2 + 3 + 4 + … + 38 + 39 + 40

 

Python Code


Use the trinket below to write a program to solve the above hint.

Padlock


Input the combination on the padlock below and click on “Unlock?” to test your combination.

unlock-access

Solution...

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

Enigma Encoder

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

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

The Enigma machines are a series of electro-mechanical rotor cipher machines. The first machines were invented at the end of World War I by German engineer Arthur Scherbius and were mainly used to protect commercial, diplomatic and military communication. Enigma machines became more and more complex and were heavily used by the German army during World War II to encrypt radio signals.

The Enigma machine was used to both encrypt or decrypt Enigma messages (Enigma encryption is symmetric, which means that the same settings can be used to both encrypt or decrypt a message).

In this challenge we will create an enigma encoder program to encrypt and decrypt messages using specific Enigma settings. But before doing so we need to gain a better understanding of how the Enigma machine actually works. To do so you can use our online Enigma emulator to start encoding or decoding secret messages.

Inside the Enigma


The enigma machine is a fairly complex encryption machine which consists of four main sections:

The Keyboard

The keyboard is used to retrieve the user input. The Enigma machine is a symmetric encryption machine. Which means that it can be used to both encrypt or decrypt a message using the same settings. The keyboard is hence used to either enter the plaintext that needs to be encrypted or the ciphertext that needs to be decrypted.

They keyboard consists of 26 keys for each letter of the alphabet. This means that encrypted messages will be joined up without any spaces or punctuation signs.

Notice how the keyboard starts with the letters QWERTZ instead of QWERTY. This is due to the fact that in the German language, the letter Z is more often used than the letter.

The plugboard

Once a key is pressed on the keyboard, it goes through the plugboard which provides the first stage of the encryption process. It is based on the principles of a substitution cipher, a form of transposition encryption.

To setup the keyboards, short wires are used to connect pairs of letters that will be permuted. For instance on the picture below the letter W will be replaced with a D and the letter D with a W as a (red) wire is used to connect these two letters/plugs. Similarly, letter V will become letter Z and Z will become V.

In a code book the plugboard settings would be recorded as follows: DW VZ

The Rotors

After the plugboard, the letter goes through the three rotors in order (from right to left), each of them changing it differently using a combination of transposition cipher and Caesar cipher! On the engima M3 there are three rotor slots and five rotors to choose from. Each rotor is identified using a Roman numeral from I to V. This provides a few settings of the Enigma machine: which rotors to use, and in which order to position them. In a code book this seeting would be recorded as IV II III (Left, Middle and Right rotors).

Each of the five rotors encrypt the letter differently using a transposition/permutation cipher and can be connected in the Enigma machine with a different Ring setting. Another setting is the initial position of the rotors: Which letters are you going to set each rotor to begin with (e.g. A/B/C../Z sometimes recorded in a codebook using numbers (01 for A, 02 for B up to 26 for Z). This creates a Caesar Shift (Caesar Cipher). On an Enigma machine, you can change the position of the rotors by turning the three wheels.

Different versions of Enigma (e.g. M4) included four rotors which made the encryption process and the number of possible settings even bigger.

On our Enigma M3 emulator, you can click on the rotors to access the Enigma rotors settings:


What makes the Enigma code particularly difficult to crack is that every time a key is pressed, the rotor on the right turns by 1 letter. Which means that the encryption settings constantly changes for each letter of a message. It also means that a single plaintext letter would be encrypted differently depending on its position in the message.

The rotors are also connected to each other so that when the rotor positioned on the right reach a specific letter, it triggers the rotor in the middle to rotate by one letter. Similarly when the rotor in the middle reaches a specific letter it triggers the rotor on the left to turn by one letter.

You will find more information on how the rotors operate on the following wikipedia page.

The Reflector

The reflector is another type of rotor inside the machine. Once the letter has gone through the three rotors from right to left, the reflector will reflect the electrical current back through the rotors, sending the encrypted letter through the rotors from left to right for another 3 stages of encryption and then through the plugboard again for a final substitution cipher. When going through the reflector, a permutation cipher is also applied to the letter.

Different versions of reflectors were used on different versions of Enigma machines. Each reflector would apply a different permutation cipher. Enigma M3 machines were equipped with either a UKW-B or UKW-C reflector. You can apply these two reflectors in the rotor settings window of our emulator (see screenshot above).

The lampboard


The lampboard is the final stage of the encryption process and is used to show the output (encrypted letter). It consists of 26 light bulbs, one for each letter of the alphabet.

The diagram below shows the journey of a letter through the encryption process of an Enigma M3.

Python Enigma Encoder


The Python program below allows you to encode and decode messages using the Enigma encryption.
You can apply your own Enigma settings by editing lines 3 to 9 of this 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: , ,

Pong Tutorial using Pygame – Adding a Scoring System

pong-gameThis tutorial is the third tutorial in a series of five Pygame tutorials:

The final stage of our tutorial focuses on adding a scoring system to our Pong game. Player A will score a point if the ball bounces against the right hand side edge of the screen while player B will score a point if the ball bounces against the left hand side edge of the screen. Both scores will be displayed at the top of the screen.

The final code for the main.py is provided below. We made three changes to the code as follows:

  • On lines 43 to 45 we are initialising both player scores to 0.
  • On lines 72 to 77 the code detects when one the player scores point.
  • On lines 96 to 101 how the scores are displayed on screen.
main.pypaddle.pyball.py
# Import the pygame library and initialise the game engine
import pygame
from paddle import Paddle
from ball import Ball
 
pygame.init()
 
# Define some colors
BLACK = (0,0,0)
WHITE = (255,255,255)
 
# Open a new window
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
 
paddleA = Paddle(WHITE, 10, 100)
paddleA.rect.x = 20
paddleA.rect.y = 200
 
paddleB = Paddle(WHITE, 10, 100)
paddleB.rect.x = 670
paddleB.rect.y = 200
 
ball = Ball(WHITE,10,10)
ball.rect.x = 345
ball.rect.y = 195
 
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
 
# Add the 2 paddles and the ball to the list of objects
all_sprites_list.add(paddleA)
all_sprites_list.add(paddleB)
all_sprites_list.add(ball)
 
# The loop will carry on until the user exits the game (e.g. clicks the close button).
carryOn = True
 
# The clock will be used to control how fast the screen updates
clock = pygame.time.Clock()
 
#Initialise player scores
scoreA = 0
scoreB = 0
 
# -------- Main Program Loop -----------
while carryOn:
    # --- Main event loop
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
              carryOn = False # Flag that we are done so we exit this loop
        elif event.type==pygame.KEYDOWN:
                if event.key==pygame.K_x: #Pressing the x Key will quit the game
                     carryOn=False
 
    #Moving the paddles when the use uses the arrow keys (player A) or "W/S" keys (player B) 
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        paddleA.moveUp(5)
    if keys[pygame.K_s]:
        paddleA.moveDown(5)
    if keys[pygame.K_UP]:
        paddleB.moveUp(5)
    if keys[pygame.K_DOWN]:
        paddleB.moveDown(5)    
 
    # --- Game logic should go here
    all_sprites_list.update()
    
    #Check if the ball is bouncing against any of the 4 walls:
    if ball.rect.x>=690:
        scoreA+=1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.x<=0:
        scoreB+=1
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.y>490:
        ball.velocity[1] = -ball.velocity[1]
    if ball.rect.y<0:
        ball.velocity[1] = -ball.velocity[1]     
 
    #Detect collisions between the ball and the paddles
    if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
      ball.bounce()
    
    # --- Drawing code should go here
    # First, clear the screen to black. 
    screen.fill(BLACK)
    #Draw the net
    pygame.draw.line(screen, WHITE, [349, 0], [349, 500], 5)
    
    #Now let's draw all the sprites in one go. (For now we only have 2 sprites!)
    all_sprites_list.draw(screen) 
 
    #Display scores:
    font = pygame.font.Font(None, 74)
    text = font.render(str(scoreA), 1, WHITE)
    screen.blit(text, (250,10))
    text = font.render(str(scoreB), 1, WHITE)
    screen.blit(text, (420,10))
 
    # --- Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
     
    # --- Limit to 60 frames per second
    clock.tick(60)
 
#Once we have exited the main program loop we can stop the game engine:
pygame.quit()
import pygame
BLACK = (0,0,0)
 
class Paddle(pygame.sprite.Sprite):
    #This class represents a paddle. It derives from the "Sprite" class in Pygame.
    
    def __init__(self, color, width, height):
        # Call the parent class (Sprite) constructor
        super().__init__()
        
        # Pass in the color of the Paddle, its width and height.
        # Set the background color and set it to be transparent
        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
 
        # Draw the paddle (a rectangle!)
        pygame.draw.rect(self.image, color, [0, 0, width, height])
        
        # Fetch the rectangle object that has the dimensions of the image.
        self.rect = self.image.get_rect()
        
    def moveUp(self, pixels):
        self.rect.y -= pixels
        #Check that you are not going too far (off the screen)
        if self.rect.y < 0:
          self.rect.y = 0
          
    def moveDown(self, pixels):
        self.rect.y += pixels
        #Check that you are not going too far (off the screen)
        if self.rect.y > 400:
          self.rect.y = 400

import pygame
from random import randint

BLACK = (0, 0, 0)
 
class Ball(pygame.sprite.Sprite):
    #This class represents a ball. It derives from the "Sprite" class in Pygame.
    
    def __init__(self, color, width, height):
        # Call the parent class (Sprite) constructor
        super().__init__()
        
        # Pass in the color of the ball, its width and height.
        # Set the background color and set it to be transparent
        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)
        self.image.set_colorkey(BLACK)
 
        # Draw the ball (a rectangle!)
        pygame.draw.rect(self.image, color, [0, 0, width, height])
        
        self.velocity = [randint(4,8),randint(-8,8)]
        
        # Fetch the rectangle object that has the dimensions of the image.
        self.rect = self.image.get_rect()
        
    def update(self):
        self.rect.x += self.velocity[0]
        self.rect.y += self.velocity[1]
          
    def bounce(self):
        self.velocity[0] = -self.velocity[0]
        self.velocity[1] = randint(-8,8)
Tagged with: