This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Initialisation | |
### - Input number of items | |
### - Set up all arrays for all tasks | |
### - item numbers | |
### - description | |
### - reserve | |
### - number of bids | |
### - highest bid | |
TESTING = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |