Uke 6

Oppgave 1

def remove_sevens(xs):
    while 7 in xs:
        xs.remove(7)
    return xs


def every_other(xs):
    return xs[:7:2]


def reverse(xs):
    return xs[::-1]


def double_values(xs):
    doubled = []
    for x in xs:
        doubled.append(x * 2)
    return doubled


# alternate solution using list comprehensions
def double_values_lcomp(xs):
    return [x * 2 for x in xs]


def unique_values(xs):
    uniques = []
    for x in xs:
        if x not in uniques:
            uniques.append(x)
    return uniques

Oppgave 2

# loop over indexes
def render_image(grid):
    R = len(grid)
    K = len(grid[0])

    result = ""
    for k in range(K):
        for r in range(R):
            result += grid[r][k]
        result += "\n"
    
    return result[:-1] # clip the last \n


# we can also loop over lines directly in the inner loop
def render_image_v2(grid):
    cols = len(grid[0])  # number of columns

    lines = []
    for index in range(cols):
        line = ""
        for row in grid:
            line += row[index]
        lines.append(line)
    return "\n".join(lines)


# we rotate our array 90 degrees clockwise
# (see this answer: https://stackoverflow.com/questions/8421337/rotating-a-two-dimensional-array-in-python)
def render_image_v3(grid):
    lines = []
    for line in zip(*grid):
        lines.append("".join(line))
    return "\n".join(lines)


# as an unreadable one liner 
def render_image_v4(grid):
    return "\n".join(["".join(line) for line in zip(*grid)])

Oppgave 3

# VERY useful to have this as a separate function
# don't want to mix the if-else logic with the looping later
def matching_base(base):
    if base == "A":
        return "T"
    elif base == "T":
        return "A"
    elif base == "C":
        return "G"
    elif base == "G":
        return "C"


def complement(dna_in):
    result = ""
    for b in dna_in:
        # attach new letter in front to get reversed result!
        result = matching_base(b) + result
    return result


def complement_v2(dna_in):
    return "".join([ matching_base(b) for b in dna_in[::-1])

Oppgave 4

# this was one example in week 6
# def render_histogram_horizontal(values):
#     # Instansiere en variabel å bygge på.
#     rows = []
# 
#     # Kjør en for-løkke som bygger opp verdien i variabelen.
#     for value in values:
#         rows.append("*" * value)
# 
#     return "\n".join(rows)    
# 

# use that to build a 2D array, then switch it around like in oppg_3
def render_histogram(values):
    vmax = max(values)
    # need to add spaces, too, to get a regular 2D shape
    bars = [ (" " * (vmax - v) + "*" * v) for v in values ]
    #print(bars) # debug
    # switch order with any method from oppg.3
    lines = ["".join(line) for line in zip(*bars)]
    #print(lines)
    return "\n".join(lines)
    
    


# Alternative solution by a working python developer
def render_histogram_2(values):
    height = max(values)
    output_rows = []
    for row_i in range(height):
        row = "".join(["*" if value >= height - row_i else " " for value in values])
        output_rows.append(row)
    return "\n".join(output_rows)