Lab 9 Solutions

from testlight import *

##########
# PART 1 #
##########
course_list = ["CSCI0180", "CSCI0160", "CSCI0220", "CSCI0320",
               "CSCI1420", "CSCI1951", "APMA0330", "APMA0350",
               "APMA1650", "ENGL0200", "ENGL0510", "CLPS0150",
               "CLPS0450", "CLPS0900"]

def dept_courses(dept: str) -> list:
    return list(filter(lambda course: course[:4] == dept, course_list))

def course_num(num: str) -> list:
    return list(filter(lambda course: course[4:] == num, course_list))

def course_search(dept: str, min: int, max: int) -> list:
    return list(filter(lambda course: course[:4] == dept and int(course[4:]) >= min and int(course[4:]) <= max, course_list))

def pretty_print(dept: str):
    if dept == "CSCI":
        print("Computer Science")
    elif dept == "APMA":
        print("Applied Math")
    elif dept == "ENGL":
        print("English")
    elif dept == "CLPS":
        print("Cognitive Linguistic & Psychological Sciences")

    for course in course_list:
        if course[:4] == dept:
            print("- " + course)

def compare_depts(dept1: str, dept2: str):
    dept1_len = len(dept_courses(dept1))
    dept2_len = len(dept_courses(dept2))

    if dept1_len == 0 or dept2_len == 0:
        raise Exception("Deparment not offering courses")
    elif dept1_len > dept2_len:
        return dept1
    elif dept1_len == dept2_len:
        return "equal"
    else:
        return dept2

##########
# PART 2 #
##########

def convert(char : str) -> str:
    """obscure a single character with a number"""
    if char in ["e", "E"]:
        return "3"
    elif char in ["i", "I"]:
        return "1"
    elif char in ["o", "O"]:
        return "0"
    else:
        return char

def obscure(word : str) -> str:
    """turns uses of e and i into 3 and 1 in a word"""
    result = ""
    for char in word:
        result = result + convert(char)
    return result

def obscure_test():
    test("lowercase & capital changes", obscure("hello my name is ELI"), "h3ll0 my nam3 1s 3L1")
    test("empty string", obscure(""), "")
    test("no change", obscure("what"), "what")

# -------------------

# tests
def sorted_increasing_test():
    test("increasing", sorted_increasing([1, 2, 3]), True)
    test("decreasing", sorted_increasing([3, 2, 1]), False)
    test("decrease at end", sorted_increasing([1, 2, 3, 2]), False)

def sorted_increasing(num_list : list) -> bool:
    """checks whether list is sorted in increasing order"""
    last_num = num_list[0]
    for n in num_list:
        if n < last_num:
            return False
        else:
            last_num = n
    return True

# -----------------------

#tests
def pos_test():
    test("all pos", first_five_pos([1, 3, 5, 7, 9, 11, 13]), [1, 3, 5, 7, 9])
    test("exactly five", first_five_pos([1, 3, 5, 7, 9]), [1, 3, 5, 7, 9])
    test("empty", first_five_pos([]), [])
    test("some neg", first_five_pos([-3, -5, 2, -8, 3]), [2, 3])

def first_five_pos(num_list : list) -> list:
    """returns list of first 5 pos numbers in the given list"""
    pos_list = []
    for num in num_list:
        if num > 0:
            pos_list.append(num)
        if len(pos_list) == 5:
            return pos_list
    return pos_list # handles the case when there are fewer than 5 pos nums


##########
# PART 3 #
##########
sorted_by_dept_code = sorted(course_list)
sorted_by_course_number = sorted(course_list, key=lambda c: c[4:])

def calculate_score(word: str) -> str:
    score = 0
    for c in word:
        if c in ["a", "A", "e", "E", "i", "I", "o", "O", "u", "U"]:
            score += 1
        elif c in ["z", "Z", "x", "X", "q", "Q"]:
            score += 5
        else:
            score +=2
    return score

def sort_by_score(lst: list) -> list:
    return sorted(lst, key=calculate_score, reverse=True)