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:
- Write a function
calculate-averagethat finds the average of all numbers in a list - Write a function
count-passingthat counts how many scores are 70 or above - Write a function
find-rangethat 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:
- Write a function
create-sentencethat combines a list of words into a sentence - Write a function
count-long-wordsthat counts words longer than 5 characters - Write a function
build-initialsthat 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