Sample program(s): course code filtering#

Example program: copy only the 100-level courses over to my course list#

Computational formulation:

Main data:

  • COURSE_CODE_LIST - list of only course codes

  • COURSE_CODE - the element we’ll need to look at to decide whether to copy something over

  • MY_COURSES (list of course codes I care about)

Subparts:

  • PARSER to get course code number

  • FILTER to decide whether something should be grabbed

  • ADDER to add grabbed course to my course list

Logic/flow:

  • LOOP over the list of course codes

  • CONDITIONAL to grab course code or not based on filter

 1# PARSER
 2def parse_code(course_code):
 3    r = 0
 4    # go through teh course code character by character
 5    for char in course_code:
 6        # if it's a number
 7        if char.isnumeric():
 8            # grab it!
 9            r = char
10            # and be done with the loop, since we only want the first one
11            break
12    
13    # reutrn the code
14    return r
1# FILTER
2def code_filter(course_code, target_code):
3    code = parse_code(course_code)
4    # check if the code is the one we want
5    if code == target_code:
6        return True
7    else:
8        return False
 1# Putting it together
 2
 3COURSE_CODE_LIST = ['INST126', 'INST201', 'INST311', 'INST314', 'INST326', 'INST327', 'INST335',
 4 'INST346', 'INST352', 'INST354', 'INST362', 'INST377', 'INST408Y', 'INST408Z',
 5 'INST414', 'INST447', 'INST462', 'INST466', 'INST490', 'INST604', 'INST612',
 6 'INST614', 'INST616', 'INST622', 'INST627', 'INST630', 'INST652', 'INST702',
 7 'INST709', 'INST728G', 'INST728V', 'INST733', 'INST737', 'INST741', 'INST742',
 8 'INST746', 'INST762', 'INST767', 'INST776', 'INST785', 'INST794']
 9
10# to hold the courses i care about
11MY_COURSES = []
12
13# the code i care about
14MY_TARGET_CODE = "2"
15
16# LOOP over list of courses
17for course_code in COURSE_CODE_LIST:
18    
19    # CONDITIONAL to decide whether or not to copy over course
20    if code_filter(course_code, MY_TARGET_CODE):
21        # ADDER to add course to my list
22        MY_COURSES.append(course_code)
23        
24print(MY_COURSES)
['INST201']

Example program: copy only the 400-level courses and below over to my course list#

Computational formulation is same as before, only diff is we need to generalize the filter

 1# FILTER
 2def code_filter_lte(course_code, threshold):
 3    # get the first number in the code
 4    code = parse_code(course_code)
 5    
 6    # change to number data type so we can do "math"
 7    code = int(code)
 8    
 9    # check if it's less than or equal to our threshold level
10    if code <= threshold:
11        return True
12    else:
13        return False
 1# TRANSFERRER v2
 2
 3MY_UNDERGRAD_COURSES = []
 4
 5MY_THRESHOLD = 4
 6
 7for course_code in COURSE_CODE_LIST:
 8    if code_filter_lte(course_code, MY_THRESHOLD):
 9        MY_UNDERGRAD_COURSES.append(course_code)
10        
11print(MY_UNDERGRAD_COURSES)
['INST126', 'INST201', 'INST311', 'INST314', 'INST326', 'INST327', 'INST335', 'INST346', 'INST352', 'INST354', 'INST362', 'INST377', 'INST408Y', 'INST408Z', 'INST414', 'INST447', 'INST462', 'INST466', 'INST490']