Skip to content

Instantly share code, notes, and snippets.

View gsinclair's full-sized avatar

Gavin Sinclair gsinclair

  • Sydney, Australia
View GitHub Profile
# Initial data, for testing.
ItemNo = [31, 42, 27, 81, 99, 46]
Description = ["Vase", "Chair", "Painting", "Piano", "Tuba", "Model car"]
NumBids = [3, 0, 1, 9, 7, 0]
Reserve = [19, 80, 300, 120, 99, 5]
Bid = [25, 0, 50, 437, 105, 0]
Buyer = [108, None, 199, 176, 155, None]
Result = [None, None, None, None, None, None]
N = 6
@gsinclair
gsinclair / gravel.py
Created February 22, 2020 23:19
Work-in-progress solution to pre-release problem (Feb 2020)
def name(sack_type):
if sack_type == 'S': return 'sand'
elif sack_type == 'G': return 'gravel'
elif sack_type == 'C': return 'cement'
def input_sack_details(n):
global NREJECTS
while True:
prompt = "Sack " + str(n) + "> "
user_input = input(prompt)
@gsinclair
gsinclair / incr.py
Created February 14, 2020 04:11
Is a list increasing?
# Determine whether a list L is increasing. Return True or False.
def incr(L):
if len(L) < 2:
# An empty list or a single-value list is counted as increasing.
return True
answer = True
i = 1
while True:
if i >= len(L):
# We got to the end without returning False, so the answer must be True.
@gsinclair
gsinclair / digit-sums.txt
Created December 10, 2019 10:22
Running total of digit sums, 1 .. 9999
n sum total
1 1 1
2 2 3
3 3 6
4 4 10
5 5 15
6 6 21
7 7 28
8 8 36
@gsinclair
gsinclair / rhymes.py
Created December 3, 2019 03:40
Count number of ways to construct a rhyming scheme
# Jennivine's question about the number of rhyming schemes (AABA, etc.)
# for N lines using K letters.
# Given a function, return a cached version of that function.
def cache_function(fn):
cache = dict()
def fn_memo(*args):
if args in cache:
return cache[args]
/* Word chains (ORAC Challenge Problems 2)
*
* Build a tree with all the letters in it, marking the word depth and (boolean)
* terminator status of each letter. Remember where the leaves are (in a set). Then it's
* an easy matter to find the leaf with the greatest word depth, build the correct path,
* and emit all the words in that path.
*
* Attempt 1 failed -- all tests had memory limit exceeded. No memory limit was stated in
* the problem.
*
@gsinclair
gsinclair / pr2019.py
Created May 14, 2019 04:29
Pre-release code for the auction thingy
### Initialisation
### - Input number of items
### - Set up all arrays for all tasks
### - item numbers
### - description
### - reserve
### - number of bids
### - highest bid
TESTING = False
@gsinclair
gsinclair / next_factor.py
Created April 13, 2019 06:14
Creating a next_factor function in Python
### Part 0: We would like to have this function to use, but it's probably in your code already.
def is_factor(f, n):
return (n % f == 0)
### Part 1: First we write a testing function.
def test_next_factor():
nf = next_factor
assert nf(10,3) == 5
@gsinclair
gsinclair / prp2.py
Created March 31, 2019 08:52
Pre-release-practice 2 (reaction times for students from Saturn and Mars)
# Pre-release practice 2
#
# Students are aged 12-16.
# Students belong to house Saturn or Mars.
# We will represent that with "S" or "M".
ALLOWABLE_AGES = range(12,17)
ALLOWABLE_HOUSES = ["S", "M"]
# The program gets information from the user to store
@gsinclair
gsinclair / week3.py
Last active March 29, 2019 04:32
Week 3 of Informatics, covering the relative efficiency of lists and sets, an example of list comprehensions, and Euclid's GCD algorithm.
import random
import time
# 'timed' runs a function several times and prints the amount
# of time taken in seconds.
def timed(name, f, ntimes):
start = time.time()
for i in range(ntimes):
f()
elapsed = time.time() - start