﻿

# Programming Examples - Python

These are 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 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 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
• Dictionary Compression - input a sentence and turn it into a list of unique words and a list of indexes that refer to the words
• 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

## 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
• 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
• 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)

## Sorting

• Bubble Sort - on a random set of one hundred integers
• Selection Sort - on numbers of your choice
• Merge Sort - on numbers of your choice
• 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