Løsningsforslag 9

Oppgave 1
def filter_high_temperatures(path_input, path_output, threshold_temp):
    with open(path_input) as file, open(path_output, "w") as outfile:
        for line in file:
            if float(line.split()[2]) >= threshold_temp:
                outfile.write(line)
Oppgave 2
#Fra lab 6 oppgave 2 V23
def kmers(dna, k):
    kmers = []
    n = len(dna)
    for i in range(n-k+1):
        kmer = dna[i:i+k]
        kmers.append(kmer)
    return kmers

#Fra lab 7 oppgave 2 V23
def kmercounter(seq,k):
    ret = {}
    k_list = kmers(seq,k)
    for k in k_list:
        ret[k] = k_list.count(k)
    return ret


def kmercount(filepath, k):
    s = []

    with open(filepath) as file:
        for line in file:
            if not line.startswith(">"):
                s.append(line.strip())
    
    return kmercounter("".join(s),k)
Oppgave 3
def first_letter_last_word(file_name):
    return_string = ""
    with open(file_name, "r") as file:
        for line in file:
            return_string += line.split()[-1][0]
    return return_string
Oppgave 4
def first_letter_last_word(file_name):
    return_string = ""
    try:
        with open(file_name, "r") as file:
            for line in file:
                return_string += line.split()[-1][0]
    except FileNotFoundError:
        return ""
    
    return return_string
Oppgave 1
def add_together(a, b, c, d):

    try:
        return a + b + c + d
    except Exception as e:
        print(f"Failed with error: {e}")
        return None