Uke 5

Oppgave 1

def multiply_3(x):
    return x * 3

Oppgave 2

def make_question(text):
    return f"{text}?"

Oppgave 3

def name_age(name, age):
    return f"{name} er {age} år gammel."

Oppgave 4

def my_math(a, b):
    y = 2 * a + b
    return y

Oppgave 5

def egen_max(a, b):
    max = (a + b + abs(a - b)) // 2
    return max


def egen_min(a, b):
    min = (a + b - abs(a - b)) // 2
    return min


def egen_len(streng):
    teller = 0
    for bokstav in streng:
        teller += 1
    return teller


def egen_abs(tall):
    if tall >= 0:
        return tall
    else:
        return -tall
def my_round(x, n):
    factor = 10**n
    x *= factor # shift decimal
    x += 0.5 # for correct rounding up/down
    x = int(x) # truncate rest of positions
    x /= factor # shift back decimal
    return x

Oppgave 6

def hvem_eldst(name_1, age_1, name_2, age_2):
    if max(age_1, age_2) == age_1:
        name = name_1
        age = age_1
    else:
        name = name_2
        age = age_2

    return f"{name} er {age} år og eldst."

Oppgave 7

def hyd_pres(dens, g, z):
    P = dens * g * z  # calculate pressure in Pa
    P = P / 1e4  # convert to dbar
    return P

Oppgave 8

def stones_to_pounds(val_stones):
    return val_stones * 14


def stones_to_kg(val_stones):
    return val_stones / 0.15747


def pounds_to_kg(val_pounds):
    return val_pounds / 2.20462


# one option (used this one in soln below)
def imperial_to_metric(val_stones, val_pounds):
    kg_from_st = stones_to_kg(val_stones)
    kg_from_lb = pounds_to_kg(val_pounds)
    kg_sum = kg_from_st + kg_from_lb
    return round(kg_sum, 2)


# another option
def imperial_to_metric_2(val_stones, val_pounds):
    st_to_lb = stones_to_pounds(val_stones)
    sum_lb = st_to_lb + val_pounds
    kg_sum = pounds_to_kg(sum_lb)
    return round(kg_sum, 2)

Oppgave 9

def draw_haiku_frame(rad_1, rad_2, rad_3):
    len_rad_1 = len(rad_1)
    len_rad_2 = len(rad_2)
    len_rad_3 = len(rad_3)

    max_len = max(len_rad_1, len_rad_2, len_rad_3)

    line_2 = "@" * (max_len + 4)
    line_3 = "@ " + " " * (max_len - len_rad_1) + rad_1 + " @"
    line_4 = "@ " + " " * (max_len - len_rad_2) + rad_2 + " @"
    line_5 = "@ " + " " * (max_len - len_rad_3) + rad_3 + " @"
    line_6 = line_2

    framed = line_2 + "\n" + line_3 + "\n" + line_4 + "\n" + line_5 + "\n" + line_6

    return framed

Oppgave 10

from math import sqrt


def side_length(x1, y1, x2, y2):
    """Returns the length of the side between the points (x1,y1) and (x2,y2)"""
    return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)


def mid(xy1, xy2, xy3, a, b, c):
    """
    Returns midpoint coordinate given:
    coordinates xy1, xy2, xy3
    and sidelengths a (opposite (x1,y1)), b (opposite (x2,y2)), c (opposite (x3,y3))
    """
    return (xy1 * a + xy2 * b + xy3 * c) / (a + b + c)


def incircle_radius(a, b, c):
    """Returns the radius of the incircle of a triangle with sidelengths a, b and c"""
    s = (a + b + c) / 2
    return sqrt(((s - a) * (s - b) * (s - c)) / s)


def find_incircle(x1, y1, x2, y2, x3, y3):
    """
    Finds and prints the center and radius of the incircle
    of a triangle with corners (x1,y1), (x2,y2), (x3,y3)
    """

    print(f"Your triangle is ({x1}, {y1}), ({x2}, {y2}), ({x3}, {y3}).\n")

    print("First we calculate the sidelengths.")
    side1 = side_length(x2, y2, x3, y3)  # side opposite (x1,y1)
    side2 = side_length(x3, y3, x1, y1)  # side opposite (x2,y2)
    side3 = side_length(x1, y1, x2, y2)  # side opposite (x3,y3)

    print("Then we find the center.")
    center_x = mid(x1, x2, x3, side1, side2, side3)
    center_y = mid(y1, y2, y3, side1, side2, side3)
    print(f"The center is {center_x, center_y}.\n")

    print("Finally we calculate the radius of the incircle.")
    radius = incircle_radius(side1, side2, side3)
    print(f"The incircle has radius {radius}.")


print("Define your triangle:")
x1 = int(input("x1 = "))
x2 = int(input("x2 = "))
x3 = int(input("x3 = "))

y1 = int(input("y1 = "))
y2 = int(input("y2 = "))
y3 = int(input("y3 = "))

find_incircle(x1, y1, x2, y2, x3, y3)