- π 25 points available
- βοΈ Last updated on 9/9/2025
βΆοΈ First, run the code cell below to import unittest, the module used for the π§ Check Your Work boxes and the autograder.
# DO NOT MODIFY THE CODE IN THIS CELL
import unittest
tc = unittest.TestCase()π― Challenge 1: Average of Positive TransactionsΒΆ
π TasksΒΆ
The transactions variable contains a list of transaction amounts. Your task is to calculate the average of all positive transactions and store the result in a new variable named pos_avg.
You can assume that there will be at least one positive number in the transactions list.
π ExampleΒΆ
transactions = [10, -5, -15, 30, 20]- pos_avgis- 20(- (10 + 30 + 20) / 3).
π RulesΒΆ
- You may create additional variables as needed for your calculations.
- You must not manually compute the average by counting the positives yourself β use code to track the count.
- Use a forloop to complete this task.
# fmt: off
transactions = [15, 8, 7, 5, 15, 72, -20, -66, 9, -4, 
                -11, 21, 19, 33, -40, -9, -8, -7, 0, 
                -55, 10, 20, -30, -1, -2, -5, -10, 4, 
                7, 8, 10, 9, -50, -10, -1, -3, -8]
# fmt: on
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(f"Average of positive transactions is {pos_avg}")π§ Check Your WorkΒΆ
Run the code cell below to test your solution.
- βοΈ If the code cell runs without errors, youβre good to move on.
- β If the code cell produces an error, review your code and fix any mistakes.
_test_case = "avg-of-positive-transactions"
_points = 6
tc.assertAlmostEqual(pos_avg, 16)π― Challenge 2: Compare Lists by IndexΒΆ
π TasksΒΆ
You are given two lists: t1 and t2. Your task is to compare their values at each index using a for loop, and count the number of occurrences where the value in t1 is equal to the corresponding value in t2. Store the counted result to a variable named num_equal.
You can assume that both lists have the same length (i.e., the same number of elements).
π€ Sample Input/OutputΒΆ
t1 = [100, -50, 400, 300, 0]
t2 = [-100, 50, 400, -800, 0]The expected output of the sample input above is 2 because t1 at indexes 2 and 4 are equal to their corresponding values in t2 (400 == 400 and 0 == 0).
# fmt: off
t1 = [-51, -22, 24, -4, 17, 40, -7, 
      -59, 21, 82, -19, 25, 65, -99, 
      54, 72, -16, -57, -20, 17]
t2 = [-64, -14, 30, -87, -98, 5, -7, 
      -10, 21, -98, 93, 43, 65, 66, 
      51, -98, -16, 57, -56, -17]
# fmt: on
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(num_equal)π§ Check Your WorkΒΆ
Run the code cell below to test your solution.
- βοΈ If the code cell runs without errors, youβre good to move on.
- β If the code cell produces an error, review your code and fix any mistakes.
_test_case = "compare-lists-by-index"
_points = 6
tc.assertEqual(num_equal, 4)π― Challenge 3: Average of Odd NumbersΒΆ
π TasksΒΆ
- Find the average of only the odd numbers in the numslist.
- Store the result in a variable named result.
π HintΒΆ
Create extra variables to keep a count of the number of odd numbers and their total.
# fmt: off
nums = [10, 5, 6, 4, 3, 1, 2, 7, 8, 2, 4, 3, 1, 5, 
        3, 7, 1, 4, 6, 3, 3, 9, 6, 5, 1, 2, 1, 12, 
        8, 6, 5, 3, 2, 2, 4, 5, 1, 3, 6, 4, 1, 3, 
        2, 7, 8, 8, 8, 8, 9, 5, 1, 3, 4, 5, 6, 1, 2]
# fmt: off
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(result)π§ Check Your WorkΒΆ
Run the code cell below to test your solution.
- βοΈ If the code cell runs without errors, youβre good to move on.
- β If the code cell produces an error, review your code and fix any mistakes.
_test_case = "average-of-odd-numbers"
_points = 6
tc.assertAlmostEqual(result, 3.6666666666666665)π― Challenge 4: Smallest Odd Number in ListΒΆ
π TasksΒΆ
You are given a list called nums. Your task is to find the smallest odd number in the list and store it in a variable named smallest_odd.
You can assume that the nums list contains at least one odd number.
π€ Sample Input/OutputΒΆ
nums = [3, 37, 9, 24, 5, -82]
# Expected output: 3nums = [-12, 3, -25, -4, -8, -5, -9, -33, -90]
# YOUR CODE BEGINS
# YOUR CODE ENDS
print(smallest_odd)π§ Check Your WorkΒΆ
Run the code cell below to test your solution.
- βοΈ If the code cell runs without errors, youβre good to move on.
- β If the code cell produces an error, review your code and fix any mistakes.
_test_case = "smallest-odd-number-in-list"
_points = 7
tc.assertEqual(smallest_odd, -33)