Skip to content

Instantly share code, notes, and snippets.

@gsinclair
gsinclair / guessing.py
Created Feb 18, 2021
Computer guessing game
View guessing.py
import random
UPPER_LIMIT = 1000000
target = random.randint(1, UPPER_LIMIT)
print("I've chosen a number between 1 and", UPPER_LIMIT)
num_guesses = 0
while True:
View cat-code-1.py
# --- Question 1 ------------------------------------------- #
# Return the number of jumps required to get to zero.
def light_years(n):
return 0
def test_light_years(n):
assert light_years(10) == 4
assert light_years(11) == 4
View trek.py
if False:
LOC = [4,8,12,16,20,24,28,32,36,40,44]
COST = [4,9,4,6,9,2,6,5,4,8,3]
DISTANCE = 50
else:
LOC = [2, 6, 10, 11, 14, 16, 18, 22, 25, 27, 30, 32, 36, 40, 44, 48, 51, 55, 59, 61,
65, 67, 70, 71, 74, 77, 80, 84, 87, 91, 93, 97]
COST = [5, 7, 5, 8, 9, 8, 4, 4, 9, 2, 2, 5, 4, 1, 1, 3, 6, 1, 7, 3, 5, 1, 1, 2, 9, 9,
9, 1, 8, 3, 6, 4,]
@gsinclair
gsinclair / merge.py
Created Mar 30, 2020
Given two sorted lists, return a merged sorted list
View merge.py
# Given two sorted lists A and B, return a sorted list that has all
# the elements of A and all the elements of B.
def merge(A, B):
i, j = 0, 0
result = []
while True:
if i == len(A):
# Put the rest of B into result.
while j < len(B):
result.append(B[j])
View pr2019.py
# 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 Feb 22, 2020
Work-in-progress solution to pre-release problem (Feb 2020)
View gravel.py
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 Feb 14, 2020
Is a list increasing?
View incr.py
# 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 Dec 10, 2019
Running total of digit sums, 1 .. 9999
View digit-sums.txt
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 Dec 3, 2019
Count number of ways to construct a rhyming scheme
View rhymes.py
# 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]
View wordchains.cpp
/* 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.
*