Created
October 22, 2021 11:52
-
-
Save jjfiv/0976ee1c63a8e2c4e1ca8c8f51e625dc to your computer and use it in GitHub Desktop.
CS145 F2021 Project 5 Starter Code
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
import os | |
from typing import List | |
def diff_compress(numbers: List[int]) -> List[int]: | |
previous = 0 | |
encoded = [] | |
for n in numbers: | |
encoded.append(n - previous) | |
previous = n | |
return encoded | |
def test_diff_compress(): | |
assert diff_compress([]) == [] | |
assert diff_compress([9]) == [9] | |
assert diff_compress([0, 2, 4]) == [0, 2, 2] | |
assert diff_compress([1, 2, 3, 4, 5, 200]) == [1, 1, 1, 1, 1, 195] | |
assert diff_compress([1, 2, 1, 2]) == [1, 1, -1, 1] | |
def diff_uncompress(compressed: List[int]) -> List[int]: | |
uncompressed = [] | |
## Q1. write a diff_uncompress that is the inverse of diff_compress | |
return uncompressed | |
def test_diff_uncompress(): | |
assert diff_uncompress([]) == [] | |
assert diff_uncompress([1]) == [1] | |
## Q2. write things here to make sure Q1 works! | |
## Q3: count_digits (figure out semantics from test_count_digits) | |
def count_digits(n: int) -> int: | |
# Integer division and remainders (by 10) are going to be your friend here. | |
return 0 | |
def test_count_digits(): | |
assert count_digits(9) == 1 | |
assert count_digits(19) == 2 | |
assert count_digits(12345) == 5 | |
# Negatives will have a similar trick to is_prime. | |
assert count_digits(-1234) == 4 | |
# Zero may be a special case. | |
assert count_digits(0) == 1 | |
## Q4: fibonacci.txt | |
def save_fibonacci(n: int) -> None: | |
# write the first n entries of the fibonacci sequence to the file 'fibonacci.txt'. | |
# might be easiest to fill out a list, first, e.g., | |
fibs = [1, 1] | |
def test_save_fibonacci(): | |
if os.path.exists("fibonacci.txt"): | |
os.remove("fibonacci.txt") | |
save_fibonacci(100) | |
assert os.path.exists("fibonacci.txt") | |
with open("fibonacci.txt") as fp: | |
# yeah, this is new: | |
written = [int(line.strip()) for line in fp.readlines()] | |
assert written[:5] == [1, 1, 2, 3, 5] | |
assert written[-1] == 354_224_848_179_261_915_075 # that's a big number | |
## Q5: insert_sorted | |
def insert_sorted(dest: list, item) -> None: | |
# insert item into destination in sorted order. | |
# consider the len(dest) locations you might insert | |
# TODO | |
# ... this case is useful when you couldn't find an item in dest that is bigger than item. | |
dest.append(item) | |
def test_insert_sorted(): | |
dest = [] | |
insert_sorted(dest, "C") | |
assert dest == ["C"] | |
insert_sorted(dest, "B") | |
assert dest == ["B", "C"] | |
insert_sorted(dest, "A") | |
assert dest == ["A", "B", "C"] | |
insert_sorted(dest, "D") | |
assert dest == ["A", "B", "C", "D"] | |
def roman_parse(letters: str) -> List[int]: | |
"""I'm giving this to you, correct, for [read_roman_numerals]""" | |
numbers = [] | |
values = {"M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1} | |
for ch in letters.upper(): | |
numbers.append(values[ch]) | |
return numbers | |
def test_roman_parse(): | |
assert roman_parse("MCMXLV") == [1000, 100, 1000, 10, 50, 5] | |
assert roman_parse("xiv") == [10, 1, 5] | |
assert roman_parse("") == [] | |
## Q6: Implement roman_sum | |
def roman_sum(numbers: List[int]) -> int: | |
total = 0 | |
# TODO: add values unless they're smaller than the value that comes after. | |
# You only need to handle (what wikipedia calls Standard Form) | |
# More information: https://en.wikipedia.org/wiki/Roman_numerals#Standard_form | |
return total | |
def test_roman_sum(): | |
assert roman_sum([]) == 0 | |
assert roman_sum([5, 1, 1]) == 7 # VII | |
assert roman_sum([1, 5]) == 4 # IV | |
assert roman_sum([1, 10]) == 9 # IX | |
assert roman_sum([1000, 100, 1000, 50, 10, 10, 1, 5]) == 1974 # MCMLXXIV | |
assert roman_sum([1000, 100, 1000, 10, 50, 5]) == 1945 # MCMXLV | |
## Q7: Implement read_roman_numerals | |
def read_roman_numerals(path: str) -> List[int]: | |
output = [] | |
# run every line through: | |
# roman_parse (might need to strip whitespace first!) | |
# roman_sum (to compute the value!) | |
# return the list of numbers in the file | |
return output | |
def test_read_roman_numerals(): | |
PATH = "numerals.txt" | |
with open(PATH, "w") as out: | |
for example in ["", "CDL", "XVI", "MCCLXIV", "MCMLXXIV"]: | |
print(example, file=out) | |
found = read_roman_numerals(PATH) | |
assert found == [0, 450, 16, 1264, 1974] | |
if __name__ == "__main__": | |
import pytest | |
pytest.main([__file__]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment