Uke 9

Oppgave 1

shopping_list = {
    "brød": 2,
    "pizza": 3,
    "poteter": 10,
    "kaffe": 1,
    "ost": 1,
    "epler": 14,
}

print(shopping_list)

Oppgave 2

collatz_dict = {}


def collatz_sequence(n):
    sequence = [n]
    while n > 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = 3 * n + 1
        sequence.append(n)
    return sequence


def first_ten_collatz():
    for n in range(1, 11):
        collatz_dict[n] = collatz_sequence(n)
    # alternate solution using dictionary comprehension:
    # collatz_dict = {n: collatz_sequence(n) for n in range(1, 11)}
    return collatz_dict

Oppgave 3

my_dict = {
    0: 0,
    1: "vafler",
    "two": 2,
    5: 4,
}

my_list = [0, 1, "boller", 4]

print("Dictionary Keys:")
for key in my_dict.keys():
    print(key)

print("\nDictionary Values:")
for val in my_dict.values():
    print(val)

# alternate solution
# for value in my_dict.values():
#     print(value)

print("\nDictionary keys/value:")
for key, value in my_dict.items():
    print(key, value)

print("\nList values:")
for element in my_list:
    print(element)

print("\nList indices/value:")
for i, e in enumerate(my_list):
    print(i, e)

Oppgave 4

in_storage = {
    "Ancillary Justice": 1_046,
    "The Use of Weapons": 372,
    "1984": 5_332,
    "The Three-Body Problem": 523,
    "A Fisherman of the Inland Sea": 728,
}

while True:
    title = input("Tittel: ")

    if title:
        number = in_storage.get(title, 0)
        print(f'Vi har {number} av "{title}"\n')
    else:
        print("Ha det!")
        break

Oppgave 5

weather_stations = {
    "Bergen": {
        "Wind speed": 3.6,
        "Wind direction": "northeast",
        "Precipitation": 5.2,
        "Device": "WeatherMaster500",
    },
    "Trondheim": {
        "Wind speed": 8.2,
        "Wind direction": "northwest",
        "Precipitation": 0.2,
        "Device": "ClimateDiscoverer3000",
    },
    "Svalbard": {
        "Wind speed": 7.5,
        "Wind direction": "southwest",
        "Precipitation": 1.1,
        "Device": "WeatherFinder5.0",
    },
}


for location, details in weather_stations.items():
    print(
        f"The weather in {location}:\n"
        f"The wind speed is {details['Wind speed']} m/s in the {details['Wind direction']} direction and the precipitation is {details['Precipitation']} mm.\nThe measurement was done using the {details['Device']} weather station.\n"
    )

Oppgave 6

in_storage = {
    "Ancillary Justice": 1_046,
    "The Use of Weapons": 372,
    "1984": 5_332,
    "The Three-Body Problem": 523,
    "A Fisherman of the Inland Sea": 728,
}

while True:
    title = input("Tittel: ")

    if title:
        number = in_storage.setdefault(title, 10)
        print(f'Vi har {number} av "{title}"\n')
    else:
        print("Ha det!")
        break

Oppgave 7

def word_comparison(word1, word2):
    word1 = set(word1)
    word2 = set(word2)
    # print(word1)
    # print(word2)

    common = word1.intersection(word2)
    word1_unique = word1.difference(word2)
    word2_unique = word2.difference(word1)

    compared = {
        "In common": common,
        "Unique to first word": word1_unique,
        "Unique to second word": word2_unique,
    }

    return compared

Oppgave 8

# inventory.py
stuff = {"rope": 1, "torch": 6, "gold coin": 42, "dagger": 1, "arrow": 12}


def displayInventory(inventory):
    print("Inventory:")
    item_total = 0
    for k, v in inventory.items():
        print(v, k)  # print the dictionary value/key pairs
        item_total += v  # increment items
    print(f"Total number of items: {item_total}")
    return item_total

Oppgave 9

inv = {"gold coin": 42, "rope": 1}
dragonLoot = ["gold coin", "dagger", "gold coin", "gold coin", "ruby"]


def displayInventory(inventory):
    print("Inventory:")
    item_total = 0
    for k, v in inventory.items():
        print(v, k)  # print the dictionary value/key pairs
        item_total += v  # increment items
    print(f"Total number of items: {item_total}")
    return item_total


def addToInventory(inventory, addedItems):
    for i in addedItems:
        inventory.setdefault(i, 0)
        inventory[i] += 1
    return inventory

Oppgave 10

numbers = {
               0 : '',
               1 : 'one',
               2 : 'two',
               3 : 'three',
               4 : 'four',
               5 : 'five',
               6 : 'six',
               7 : 'seven',
               8 : 'eight',
               9 : 'nine',
               10: 'ten',
               11: 'eleven',
               12: 'twelve',
               13: 'thirteen',
               14: 'fourteen',
               15: 'fifteen',
               16: 'sixteen',
               17: 'seventeen',
               18: 'eighteen',
               19: 'nineteen',
               20: 'twenty',
               30: 'thirty',
               40: 'forty',
               50: 'fifty',
               60: 'sixty',
               70: 'seventy',
               80: 'eighty',
               90: 'ninety',
               100: 'hundred',
               1000: 'thousand',
}        


def handle_hundreds(hundreds, rest):
    # can only deal with numbers up to 999
    # digit value should be between 0 and 9
    assert 0 <= hundreds <= 9

    if hundreds == 0:
        return ''

    text = numbers[hundreds] + ' ' + numbers[100]
    if rest > 0:
        text += ' and '

    return text



def handle_1_to_99(n):
    if n <= 20:
        return numbers[n]

    tens, ones = divmod(n,10)
    assert tens > 1
    text = numbers[10*tens]
    if ones:
        text += '-' + numbers[ones]
    return text



def number_name(n):
    if n == 1000:
        return 'one thousand'

    result = ''

    hundreds, rest = divmod(n,100)
    assert 0 <= rest < 100

    result += handle_hundreds(hundreds, rest)
    result += handle_1_to_99(rest)

    return result

def all_numbernames(n):
    total = 0
    for n in range(1,n+1):
        name = number_name(n)
        # remove spaces and dash
        name = name.replace(' ','')
        name = name.replace('-','')
        total += len(name)
    return total

def solve_euler_17():
    return all_numbernames(1000)