Lab 8: Lists | CMSC 105 Elementary Programming - Fall 2024

Lab 8: Lists

Points Possible: 100

Due: Thursday, October 24th

In this lab, we will write programs using Python lists.

Submission:

  1. Submit a Python file for each of the programs.

Pair programming

Pair programming is a software development technique in which two programmers work together at one computer. One, the driver, writes code while the other, the navigator, reviews each line of code as it is typed in. The two programmers switch roles frequently.

Program 1: Color counter

Write a function called countColor that takes as input a string of colors separated by spaces called colorString and a second input called colorToCount. The function should count colorToCount in the string of colors. Please test this function by reading input values from the user (input statement).

Please use lists to solve this problem.

Sample run:

Enter colors: Red Blue Red Green Yellow
Enter color to count: Red
Red appears 2 times

Hint:

colorString = "Red Yellow Orange Green Blue"
print(colorString)
colorList = colorString.split()
print(colorList)

Save the program is colors.py and attach it to the Blackboard lab assignment.

Grading Rubric for the program:
(Headers and comments: 5 points, input: 5 points, computation: 20 points, result: 5 points)
Total: 35 points

Program 2: Find Names

Write a function findName that takes as input a string of names separated by spaces and a second input argument called nameToFind. The function should return the first position of nameToFind in the string of names and return -1 if nameToFind doesn’t exist in the string of names. Please test this function by reading input values from the user (input statement).

Please solve this using lists.
Hint: The list.index(x) function can be used in finding the nameToFind in a list of names.

Sample run:

Enter names: Della Shweta Bob Steve Shweta
Enter a name to find: Shweta
Shweta exists at the position number 2

Save the program is names.py and attach it to the Blackboard lab assignment.

Grading Rubric for the program:
(Headers and comments: 5 points, input: 5 points, computation: 20 points, result: 5 points)
Total: 35 points

Program 3: Scores

Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average.

Sample run:

Enter scores: 90 80 85 65 55
The average score is 75.0
3 scores are above and equal to the average
2 scores are below the average

Save the program is scores.py and attach it to the Blackboard lab assignment.

Hint:

# Convert the scores string to a list by using the split function.
scores_list_of_strings = scores.split()

# Convert the scores list from a list of strings
# to a list of numbers
# Start with an empty list
scores_list = []

# For each score string in the scores list
for score_string in scores_list_of_strings:
    # Append the score to the scores list
    # after using the eval function to convert
    # to the score string to a number.
    scores_list.append(eval(score_string))

Grading Rubric for the program:
(Headers and comments: 5 points, input: 5 points, computation: 15 points, result: 5 points)
Total: 30 points