COURSES_PER_SEMESTER = 4
TOTAL_SEMESTERS = 8

def is_prefix(pre, text) :
    """
    Returns True if @pre is a prefix of @text
    
    >>> is_prefix("pre", "prefix")
    True
    >>> is_prefix("fix", "prefix")
    False
    """
    # a prefix cannot be longer than the text itself
    if len(pre) > len(text):
        return False

    # True if the first len(pre) characters match, False otherwise
    return pre == text[:len(pre)]

def count_subject(subject, schedule) :
    """
    Returns the number of times @subject appears in
    the course schedule specified by @schedule.
    @subject is specified as a string
    @schedule is specified as a list of list of strings

    >>> count_subject("CSCI", [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    2
    >>> count_subject("STAT", [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    1
    >>> count_subject("MATH", [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    0
    """
    count = 0
    for semester in schedule:
            # iterate through each course taken in current semester
            for course in semester:
                # accumulate if coure matches the prefix
                if is_prefix(subject, course):
                    count = count + 1
    return count

def most_popular_subject(subjects, schedule) :
    """
    Returns a list of the most popular (or tied for most popular) subjects that appears in
    the course schedule specified by @schedule.
    @subject is specified as a string
    @schedule is specified as a list of list of strings

    >>> most_popular_subject(["CSCI"], [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    2
    >>> most_popular_subject(["STAT"], [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    1
    >>> most_popular_subject(["MATH"], [["CSCI 134", "ARTH 101"], ["CSCI 136", "STAT 201"]])
    0
    """
    # underestimate number of times most popular subject taken
    most_popular_count = -1

    # accumulator variable for courses tied for most popular
    most_popular_list = []

    # go through every possible course subject
    for subject in subjects:

        # accumulator for current subject
        count = count_subject(subject, schedule)

        # Now that we've counted the number of courses taken with the prefix,
        # decide whether we need to update our "best answer seen so far"
        if count > most_popular_count:
            # most popular seen yet. replace list
            most_popular_count = count
            most_popular_list = [subject]
        elif count == most_popular_count:
            # tied for most popular seen yet, add to list
            most_popular_list = most_popular_list + [subject]
    
    return most_popular_list

if __name__ == "__main__":
    f05 = ["ENGL 126", "CSCI 134", "MATH 105", "PHYS 151"]
    s06 = ["LING 111", "ECON 120", "CSCI 136", "MATH 251"]
    f06 = ["ARTH 101", "ECON 110", "PSYC 101", "CSCI 237"]
    s07 = ["ARTH 102", "ECON 230", "CSCI 256", "STAT 201"]
    f07 = ["ECON 251", "ECON 252", "CSCI 361", "CSCI 373"]
    s08 = ["ECON 255", "CSCI 334", "CSCI 374", "MATH 211"]
    f08 = ["ECON 353", "ECON 475", "CSCI 336", "CSCI 371"]
    s09 = ["AMST 201", "ECON 385", "CSCI 356", "CSCI 432"]
    sched = [f05] + [s06] + [f06] + [s07] + [f07] + [s08] + [f08] + [s09]

    prefixes = ['AMST', 'ARTH', 'CSCI', 'ECON', 'ENGL', 'LING', 'MATH', 'PHYS', 'PSYC', 'STAT']

    print("most popular: ", most_popular_subject(prefixes, sched))

    for prefix in prefixes:
        print(prefix, count_subject(prefix, sched))



