﻿

# Programming Examples - Python

These are example Python programs that I've written as demonstrations of particular programming techniques or types of solution for both KS3 Computing and GCSE Computer Science.  They are organised and categorised by technique/concept.  There are further examples in the Python course and Microtips playlist on the Computing and ICT in a Nutshell YouTube channel.  The idea is that students can see how the various methods can be used as part of a larger program after learning the basics of programming.

The sections are:

NB. Some examples appear twice (possibly with different descriptions) because they use multiple techniques, and some examples with similar names might approach the same problem in different ways.

## Dictionaries

• Scrabble Score - take and word and see how many points it would score in Scrabble
• OCR Town Election - answer to a question from a GCSE past paper
• Dictionary Compression - input a sentence and turn it into a list of unique words and a list of indexes that refer to the words
• Calculating Change - what is the minimum number of coins required?
• Interpreter - reads instructions from a file and interprets them (solution to one of the 2019 CyberGirl challenges)
• Dice Game - uses a dictionary to match usernames with passwords and real names
• Huffman Coding - uses a dictionary to count the frequency of each letter

## Boolean Logic

• Plurals - add an 's' to plurals and choose the verb (e.g. is/are) without using if
• Converting Denary to Binary - using bitwise logic
• Coin Flip - game that tries to avoid the use of if
• Boolean Logic - a program that takes a Boolean operator and two inputs and outputs the result
• Encryption with EOR - similar to the WWII Lorenz cipher cracked by Colossus at Bletchley Park
• Seven Segments - simulate a seven-segment display for a given number
• Stopclock - uses nested loops to count the time and Boolean logic to display it
• Four Numbers, One Variable - uses shifting and bitwise logic to store four 8-bit numbers in a single variable
• Dice Game - uses Boolean logic to display the dice faces
• Connect 4 - uses a simple algorithm to play the classic game (and bitwise logic to manipulate the board)

## Modular Arithmetic (and other Mathematical Techniques)

• Teams - put people into teams like a PE teacher!
• Shuffle - create a pack of cards and deal them out between a given number of players
• Caesar Shift Cipher* - simple encryption technique
• Prime Numbers - using modular arithmetic to determine whether a number is prime
• Fizz Buzz* - identify multiples and prime numbers
• Sieve of Eratosthenes - generate lists of prime numbers
• 100 Doors - generate a list of square numbers
• Perfect Numbers - a function to check whether a number is "perfect" (i.e. it's the sum of its factors)
• Solving Quadratics - including those with complex roots
• Monte Carlo method for estimating the value of pi
• Countdown - uses RPN to solve the numbers round in Countdown (I don't think it finds all solutions, though!)
• Spider Webs - draws spider webs and uses Pythagoras to check that they don't overlap
• Triangulate* - enter the three sides of a triangle and determine whether it's equilateral, isosceles, right-angled, etc.
• Denary to Hexadecimal Converter (uses integer division and modular arithmetic)
• Converting Binary to Denary (uses powers of two)
• Connect 4 - uses a simple algorithm to play the classic game; the board is stored using a binary representation
• 21 Game - a generalised version of the Nim variant in which the winner is the first player to count to 21.
• Light Bulb Energy Rating - a good exercise in scaling and using arrays/lists for reference
• Vedic Square - Indian multiplication table
• Arboreal Symmetry - draw (and possibly decorate) a tree

## Sorting and Searching

• Bubble Sort - gives GCSE-style explanation of Bubble Sort
• Selection Sort - gives GCSE-style explanation of Selection Sort
• Merge Sort - gives GCSE-style explanation of Merge Sort
• Insertion Sort - gives GCSE-style explanation of Insertion Sort
• Binary Search - gives GCSE-style explanation of the steps in a binary sort
• Card Trick - AQA specimen task that "guesses" a card by sorting it into the middle of the pile

## Drawing with Turtle

• Snowflakes - draw random snowflakes using a similar method to making them out of paper
• Spider Webs - draws spider webs and uses Pythagoras to check that they don't overlap
• Fireworks - using Newton's laws of motion and a dab of trigonometry to simulate a firework.

• Distribution of Random Numbers - generate a set of random numbers, write the frequencies to a .csv file
• Hashing - hashing usernames and passwords stored in a text file
• Quiz Generator - writes its output to a file as HTML to create a webpage
• Dice Game - reads valid usernames from a file and keeps the high scores in another file
• Music Quiz - hangman-style game based on song titles
• Worderama - Wordle-style word game