Skip to main content

Practice Problems: Iteration with Lists (Python)

Tests are given for all practice problems for you to run against your own implementation. To run the tests, either run python myfile.py or pytest myfile.py where you have placed the code you are working on in myfile.py.

Problem 1: Basic List Statistics

Tasks:

  1. calculate_sum - add up all numbers in a list
  2. find_maximum - find the largest number in a list
  3. count_positive - count how many numbers are positive
  4. calculate_average - find the mean of all numbers

Template:

def calculate_sum(numbers: list[int]) -> int:
"""write your docstring here"""
# write your implementation here
pass

def find_maximum(numbers: list[int]) -> int:
"""write your docstring here"""
# write your implementation here
pass

def count_positive(numbers: list[int]) -> int:
"""write your docstring here"""
# write your implementation here
pass

def calculate_average(numbers: list[float]) -> float:
"""write your docstring here"""
# write your implementation here
pass

def test_calculate_sum():
assert calculate_sum([1, 2, 3, 4, 5]) == 15
assert calculate_sum([]) == 0
assert calculate_sum([-1, -2, 3]) == 0
assert calculate_sum([10, -5, 3]) == 8
assert calculate_sum([0, 0, 0]) == 0

def test_find_maximum():
assert find_maximum([1, 5, 3, 9, 2]) == 9
assert find_maximum([10]) == 10
assert find_maximum([-5, -2, -8]) == -2
assert find_maximum([5, 5, 5]) == 5


def test_count_positive():
assert count_positive([1, -2, 3, -4, 5]) == 3
assert count_positive([]) == 0
assert count_positive([-1, -2, -3]) == 0
assert count_positive([1, 2, 3]) == 3
assert count_positive([0, 1, 0, 2]) == 2

def test_calculate_average():
assert abs(calculate_average([1.0, 2.0, 3.0]) - 2.0) < 0.001
assert abs(calculate_average([10.0]) - 10.0) < 0.001
assert abs(calculate_average([2.5, 3.5, 4.0]) - 3.333) < 0.01


if __name__ == "__main__":
test_calculate_sum()
test_find_maximum()
test_count_positive()
test_calculate_average()
print("All tests passed!")

Problem 2: String Processing with Iteration

Tasks:

  1. join_strings - concatenate all strings in a list with a separator
  2. count_long_words - count strings longer than a specified length
  3. build_acronym - create an acronym from the first letters of words
  4. find_longest_word - find the longest string in a list

Template:

def join_strings(words: list[str], separator: str = " ") -> str:
"""write your docstring here"""
# write your implementation here
pass

def count_long_words(words: list[str], min_length: int) -> int:
"""write your docstring here"""
# write your implementation here
pass

def build_acronym(words: list[str]) -> str:
"""write your docstring here"""
# write your implementation here
pass

def find_longest_word(words: list[str]) -> str:
"""write your docstring here"""
# write your implementation here
pass

def test_join_strings():
assert join_strings(["hello", "world"]) == "hello world"
assert join_strings(["a", "b", "c"], "-") == "a-b-c"
assert join_strings([]) == ""
assert join_strings(["single"]) == "single"
assert join_strings(["", "empty", ""], "|") == "|empty|"

def test_count_long_words():
assert count_long_words(["cat", "elephant", "dog", "butterfly"], 5) == 2
assert count_long_words([], 3) == 0
assert count_long_words(["a", "bb", "ccc"], 2) == 1
assert count_long_words(["short", "words"], 10) == 0

def test_build_acronym():
assert build_acronym(["National", "Aeronautics", "Space", "Administration"]) == "NASA"
assert build_acronym([]) == ""
assert build_acronym(["hello", "world"]) == "HW"
assert build_acronym(["", "world"]) == "W"

def test_find_longest_word():
assert find_longest_word(["cat", "elephant", "dog"]) == "elephant"
assert find_longest_word(["hello"]) == "hello"
assert find_longest_word(["same", "size"]) == "same"


if __name__ == "__main__":
test_join_strings()
test_count_long_words()
test_build_acronym()
test_find_longest_word()
print("All tests passed!")