Lab 9: More Lists | CMSC 105 Elementary Programming - Fall 2024

Lab 9: More Lists

Program 1: Concatenate Lists

concatenate.py

''' 
Author name: <Your name here>
This program will concatenate the two lists using the
list elements index-wise (i.e. create a new list and print it).
You may assume that both `list1` and `list2` are the same length.
'''

list1 = ['W','ar','learn','pyt']
list2 = ['e','e','ing','hon']

list3 = []

for i in range(len(list1)):
    list3.append(list1[i] + list2[i])
    
print(list3)

Program 2: Find Minimum

min.py

''' 
Author name: <Your name here>
This program will contain a function `findMin(list_of_numbers)`
that takes as input argument a list `list_of_numbers` and
returns the index of the minimum value in the list.
'''

# This function will return the index of the minimum value in the list.
def findMin(list_of_numbers):
    # Create a variable that will store the index of the minimum value
    # in the list, intialize it to 0 (first index).
    min_index = 0
    # Create a variable to store the current minimum value in the list,
    # initialize it to the first value in the list.
    min_value = list_of_numbers[0]
    
    # For each index in the range 0 up to the length of the list of numbers.
    for i in range(len(list_of_numbers)):
        # Check to see if the next item in the list is less than the stored minimum value.
        if list_of_numbers[i] < min_value:
            # It is less than, so update the minimum.
            min_value = list_of_numbers[i]
            # Also, update the min_index variable.
            min_index = i

    # Return the index of the minimum value in the list.
    return min_index
        
    
# Call the findMin function to test it.
print(findMin([2,4,3,0,1]))

Program 3: Grades

grades.py

''' 
Author name: <Your name here>
This program will analyze a list of student grades.
The program will allow the user to input grades, and then it will
perform various operations such as finding the highest and lowest grades,
calculating the average, sorting the grades, and determining if a
specific grade exists in the list.
'''

# Function to calculate the average of a list
# params: grades list
# returns: average grade
def calculate_average(grades):
    # Create a variable to hold the sum of the grades. 
    sum_of_grades = 0
    # For each grade in the grades list.
    for grade in grades:
        # Add the grade to the sum of the grades variable.
        sum_of_grades += grade
    # Return the average be dividing the sum of the grades by the length of the grades list.
    return sum_of_grades / len(grades)
# Also acceptable: return sum(grades) / len(grades)

# Function to find the maximum grade
# params: grades list
# returns: maximum grade
def find_max(grades):
    # Initialize a variable to store the maximum grade.
    max_grade = grades[0]
    # For each grade in the grades list.
    for grade in grades:
        # If the grade is less than the maximum grade found so far.
        if grade > max_grade:
            # Update the maximum grade.
            max_grade = grade
    return max_grade
# Also acceptable:  return max(grades)


# Function to find the minimum grade
# params: grades list
# returns: minimum grade
def find_min(grades):
    # Initialize a variable to store the minimum grade.
    min_grade = grades[0]
    # For each grade in the grades list.
    for grade in grades:
        # If the grade is less than the minimum grade found so far.
        if grade < min_grade:
            # Update the minimum grade.
            min_grade = grade
    return min_grade
# Also acceptable:  return min(grades)


# Function to display the menu
def display_menu():
    print("\nMenu:")
    print("1. Display all grades")
    print("2. Find the highest grade")
    print("3. Find the lowest grade")
    print("4. Calculate the average grade")
    print("5. Sort the grades")
    print("6. Check if a grade exists")
    print("7. Remove a grade")
    print("8. Exit")

# Main program starts here
grades = []

# Get the number of grades
num_grades = int(input("How many grades do you want to input? "))

# Input grades from the user
for i in range(num_grades):
    grade = int(input(f"Enter grade {i + 1}: "))
    grades.append(grade)

# Main loop for the menu
while True:
    display_menu()
    choice = int(input("Enter your choice: "))
    
    if choice == 1:
        # Display all grades
        print("Grades:", grades)
    
    elif choice == 2:
        # Find the highest grade
        if grades:
            print("Highest grade:", find_max(grades))
        else:
            print("No grades to display.")
    
    elif choice == 3:
        # Find the lowest grade
        if grades:
            print("Lowest grade:", find_min(grades))
        else:
            print("No grades to display.")
    
    elif choice == 4:
        # Calculate the average grade
        avg = calculate_average(grades)
        print("Average grade:", avg)
    
    elif choice == 5:
        # Sort and display grades
        sorted_grades = sorted(grades)
        print("Sorted grades:", sorted_grades)
    
    elif choice == 6:
        # Check if a grade exists
        check_grade = int(input("Enter the grade to check: "))
        if check_grade in grades:
            print("Grade", check_grade, "exists in the list.")
        else:
            print("Grade", check_grade, "does not exist.")
    
    elif choice == 7:
        # Remove a grade
        remove_grade = int(input("Enter the grade to remove: "))
        if remove_grade in grades:
            grades.remove(remove_grade)
            print("Grade", remove_grade, "removed.")
        else:
            print("Grade", remove_grade, "not found.")
    
    elif choice == 8:
        # Exit the program
        print("Exiting the program.")
        break
    
    else:
        print("Invalid choice. Please try again.")