﻿

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.

Nested Loops

• Squares and Triangles - using nested loops to plot shapes
• Thief!* - you know the digits in a PIN but not their order; list the possible PINs
• Twelve Days of Christmas - display the lyrics and count the presents
• Stopclock - uses nested loops to count the time and Boolean logic to display it
• Change - show the minimum number of coins required to give a certain amount of change
• Triangle Numbers - demonstration of Gauss' assertion that any natural number is the sum of at most three triangle numbers

Dictionaries

• Scrabble Score - take and word and see how many points it would score in Scrabble
• 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

Sorting

• 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 - rgives GCSE-style explanation of Insertion 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