Skip to main content

Practice Problems: Iteration with Lists (Pyret)

Tests are given for all practice problems for you to run against your own implementation.

Problem 1: Basic List Statistics

Write functions that calculate basic statistics from a list of test scores using for each loops and mutable variables.

Tasks:

  1. Write a function calculate-average that finds the average of all numbers in a list
  2. Write a function count-passing that counts how many scores are 70 or above
  3. Write a function find-range that returns the difference between highest and lowest scores

Template:

fun calculate-average(scores :: List<Number>) -> Number:
# Write your docstring here
# Write your implementation here
where:
calculate-average([list: 85, 86, 87, 88, 89]) is 87
calculate-average([list: 70, 80, 90]) is 80
calculate-average([list: ]) is 0
calculate-average([list: 75]) is 75
end

fun count-passing(scores :: List<Number>) -> Number:
# Write your docstring here
# Write your implementation here
where:
count-passing([list: 85, 92, 78, 96, 88]) is 5
count-passing([list: 65, 85, 55, 95, 75]) is 3
count-passing([list: ]) is 0
count-passing([list: 80]) is 1
count-passing([list: 70, 69, 70]) is 2
end

fun find-range(scores :: List<Number>) -> Number:
# Write your docstring here
# Write your implementation here
where:
find-range([list: 85, 92, 78, 96, 88]) is 18
find-range([list: 10, 50, 30]) is 40
find-range([list: ]) is 0
find-range([list: 75]) is 0
find-range([list: 100, 100, 100]) is 0
end

Problem 2: String Processing

Process lists of words to build formatted output and gather statistics.

Tasks:

  1. Write a function create-sentence that combines a list of words into a sentence
  2. Write a function count-long-words that counts words longer than 5 characters
  3. Write a function build-initials that creates a string of first letters from each word

Template:

fun create-sentence(words :: List<String>) -> String:
# Write your docstring here
# Write your implementation here
where:
create-sentence([list: "hello", "world", "programming"]) is "hello world programming"
create-sentence([list: "the", "quick", "brown", "fox"]) is "the quick brown fox"
create-sentence([list: ]) is ""
create-sentence([list: "single"]) is "single"
create-sentence([list: "", "word", ""]) is " word "
create-sentence([list: "a", "b", "c"]) is "a b c"
end

fun count-long-words(words :: List<String>) -> Number:
# Write your docstring here
# Write your implementation here
where:
count-long-words([list: "hello", "world", "programming", "is", "fun"]) is 1
count-long-words([list: "elephant", "cat", "butterfly", "dog"]) is 2
count-long-words([list: ]) is 0
count-long-words([list: "short"]) is 0
count-long-words([list: "exactly", "five"]) is 1
count-long-words([list: "six", "seven", "eight"]) is 0
end

fun build-initials(words :: List<String>) -> String:
# Write your docstring here
# Write your implementation here
where:
build-initials([list: "hello", "world", "programming"]) is "hwp"
build-initials([list: "Alice", "Bob", "Charlie"]) is "ABC"
build-initials([list: ]) is ""
build-initials([list: "single"]) is "s"
build-initials([list: "", "word", ""]) is "w"
build-initials([list: "a", "b", "c"]) is "abc"
end