Uke 11¶
Oppgave 1
def mean(data):
"""
Find the arithmetic mean value of a list of integers.
mean = sum of all elements / length of list
You can assume that data has the correct type: [ int, int, ... ]
"""
return sum(data) / len(data)
def median(data):
"""
Find the median value of a list of integers.
median = middle of the sorted values
If the list has an even length, take the mean of the two middle values
You can assume that data has the correct type: [ int, int, ... ]
"""
sorted_data = sorted(data)
middle = len(data) // 2
if len(data) % 2 == 0:
i0 = middle - 1 # Ex, len(list) is 8, i0 is 3
i1 = i0 + 1 # and i1 is 4
return (sorted_data[i0] + sorted_data[i1]) / 2
else:
return sorted_data[middle] # Ex, len(list) is 3, result is 1
def mode(data):
"""
Find the modal value of a list of integers.
mode = the value that is most often in the list
If there are several modes, it's enough to return one of them.
You can assume that data has the correct type: [ int, int, ... ]
"""
histogram = {}
for n in data:
histogram.setdefault(n, 1)
histogram[n] += 1
max_count = 0
max_n = None
for n, count in histogram.items():
if count > max_count:
max_count = count
max_n = n
return max_n
def mode_v2(data):
"""
Find the modal value of a list of integers.
mode = the value that is most often in the list
If there are several modes, it's enough to return one of them.
You can assume that data has the correct type: [ int, int, ... ]
"""
from collections import Counter
histogram = Counter(data)
return histogram.most_common(1)[0][0] # tuple of (mode, frequency)
Oppgave 2
from datetime import date
count = 0
for year in range(1901, 2001):
for month in range(1, 13):
first = date(year, month, 1)
if first.isoweekday() == 3:
count += 1
Oppgave 3
from itertools import combinations
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
def card_combinations(k, n):
card_combos = []
for hand in combinations(deck, k):
if sum(hand) == n:
card_combos.append(hand)
return card_combos
def card_combinations_v2(k, n):
card_combos = [
hand
for hand in combinations(deck, k)
if sum(hand) == n
]
return card_combos
Oppgave 4
import random
from collections import Counter
def kast_2():
return random.randint(1,6) + random.randint(1,6)
def kast_n_2(n):
return [ kast_2() for _ in range(n) ]
def print_histo(xs):
ctr = Counter(xs)
all = sum(ctr.values())
for item, count in sorted(ctr.items()):
num_star = (100*count)//all
print(f'{item:2} {"*"*num_star}')
Oppgave 5
from decimal import Decimal
class NoPrice(Exception):
pass
class NotEnoughPaid(Exception):
pass
def cash_register_events(cash_register_filename):
"""Read cash register events from file and store
in two dictionaries and a variable"""
purchases = {}
returns = {}
payment = 0
with open(cash_register_filename, encoding="utf8") as cash_register_file:
for line in cash_register_file:
event, item = line.rstrip().split(sep=";")
if event == "buy":
purchases.setdefault(item, 0)
purchases[item] += 1
elif event == "return":
returns.setdefault(item, 0)
returns[item] += 1
elif event == "pay":
payment += Decimal(item)
return purchases, returns, payment
def prices(prices_filename):
"""Read product prices from file and store in dictionary."""
prices = {}
with open(prices_filename, encoding="utf8") as prices_file:
for line in prices_file:
item, price = line.split(sep=";")
prices[item] = Decimal(price)
return prices
def receipt_content(prices_filename, cash_register_filename):
"""Construct contents of a receipt of the cash register events,
given the store prices."""
price_dict = prices(prices_filename)
purchases, returns, payment = cash_register_events(cash_register_filename)
total = 0
purchases_returns = []
for item, number in sorted(purchases.items()):
if item not in price_dict:
raise NoPrice
cost = number * price_dict[item]
total += cost
purchases_returns.append((number, item, cost))
for item, number in sorted(returns.items()):
if item not in price_dict:
raise NoPrice
cost = number * price_dict[item]
total -= cost
purchases_returns.append((-number, item, -cost))
vat = total * Decimal("0.2")
if payment < total:
raise NotEnoughPaid
change = total - payment
return purchases_returns, total, vat, payment, change
def receipt(prices_filename, cash_register_filename):
"""Construct a receipt of the cash register events,
given the store prices."""
# get receipt content from receipt_content()
purcases_returns, total, vat, payment, change = receipt_content(
prices_filename, cash_register_filename
)
# the formatted lines of the receipt
receipt_lines = [f"{'Nr.':>4} {'Item':18} {'Price':>10}"]
for nr, item, price in purcases_returns:
receipt_lines.append(f"{nr:4d} {item:18} {price:10.2f}")
receipt_lines.append(f"Total: {total}")
receipt_lines.append(f"Of which VAT: {vat:.2f}")
receipt_lines.append(f"Payment: {payment}")
receipt_lines.append(f"Change {change}")
# add some dividers
max_len = max(len(line) for line in receipt_lines)
divider = "-" * max_len
receipt_lines.insert(1, divider)
receipt_lines.insert(-4, divider)
receipt_lines.insert(-2, divider)
return "\n".join(receipt_lines)
if __name__ == "__main__":
print(receipt("uke_11/prices.txt", "uke_11/cash_register.txt"))