CSc 250 Lab 5

In this lab, you will be tasked with writing several functions that require you to use lists and while/for loops. You should create one file named lab5.py and put all of the functions you complete, along with the test-cases, in the same file. Submit just this file to D2L when you are finished.

Problem 1

Write a functions named up_low. This function will take one parameter, which you can assume is a list of strings. This function should loop through all elements. If any element begins with an upper-case letter, it should replace the element with and _all_ upper-case version of the string. Otherwise, it should convert the string to be all lower-case. See the tests below for examples:
l1 = ['Ben', 'ben', 'jOSEph', 'JAmeS']
up_low(l1)
print(l1)
l1 = ['aAAAA', 'Bbbbb', 'CcCc', 'dDdD']
up_low(l1)
print(l1)
Should produce this output:
['BEN', 'ben', 'joseph', 'JAMES']
['aaaaa', 'BBBBB', 'CCCC', 'dddd']
You'll probably want to use the `isupper()`, `upper()`, and `lower()` functions to acomplish this.

Problem 2

Write a function named longest. This function will accept two parameters, each of which will be lists on strings (not necessarily of the same length). This function should determine if the first or the second argument has a greater number of total characters in all of the elements. For each list, go through each element and sum the lengths of all the strings. Whichever sum is greater is the "longer" list. Return 1 if the first is longer, 2 if the second is longer, and 3 for a tie. For example, these tests:
l1 = ['this is a string!']
l2 = ['a', 'b', 'c']
print(longest(l1, l2))

l1 = ['one', 'two', 'three']
l2 = ['four', 'five', 'six']
print(longest(l1, l2))

l1 = ['Joe', 'Reed']
l2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
print(longest(l1, l2))
Should produce:
1
3
2
You might want to create a helper function, which just computes the character-length of a single list.

Problem 3

Create a function named check_ok_applicants. This function will look through a list of applicanta to University of Nowherere (made up by me!) and determine which applicants may be accepted. The criteria for being accepted is that an applicant must be 27 years old or greater, less than 50, and have a BS, MS, or PhD. The function will take three lists as parameters. The first will be a list of the applicants names, the second a list of their ages, and the third a list of education level. These three lists will be of the same length, and the elements at the same intexes correspond to the same applicant. The function should return a new list with the names of the acceptable applicants. This test case:
names = ['Bob', 'Anna', 'Martha', 'Marty', 'Kate', 'Karen', 'Chris', 'Jane']
ages = [24, 19, 23, 34, 23, 30, 70, 51]
educs = ['MS', 'HS', 'NA', 'MS', 'NA', 'BS', 'PhD', 'MS']
print(check_ok_applicants(names, ages, educs))
Should produce this output:
['Marty', 'Karen']

Problem 4

Create a function named remove_triples. This function will have one list parameter. This function will iterate through the elements of the list and determine if there are any elements that appear three times in-a-row. When it finds this, it will remove two of the three elements, so that only one will remain. It should return the number of times it had to do this removal. This test case:
content = [1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 8]
print(remove_triples(content))
print(content)

content = [5, 5, 5, 5, 5, 5, 5]
print(remove_triples(content))
print(content)
Should produce this output:
2
[1, 2, 3, 4, 5, 6, 7, 8]
3
[5]

You must turn in at least 3 of the problems on D2L to get credit for this.

Solutions: lab5.py