Sample Questions
1. Write a function that accepts a list of words and returns a dictionary where:
● keys = word lengths
● values = number of words with that length
Sample Input:
["cat", "dog", "tree", "book", "pen"]
Expected Output:
{3: 3, 4: 2}
2. Write a function that accepts a string and returns a list of characters that appear more than
once, in the order they first repeat.
Sample Input:
"programming"
Expected Output:
["r", "g", "m"]
3. Write a function that accepts a string and returns a dictionary counting the frequency of each
word.
Sample Input: "this is a test this is"
Expected Output: {"this": 2, "is": 2, "a": 1, "test": 1}
4. Write a function that accepts a string and returns a dictionary counting only numeric
characters.
Sample Input: "a1b22c3"
Expected Output: {"1": 1, "2": 2, "3": 1}
5. Write a function that accepts a sentence and returns the shortest word and how many times it
appears.
Sample Input: "this is a simple test test"
Expected Output: ("a", 1)
6. Write a function that accepts a sentence and returns a dictionary of word lengths as keys and
their frequencies as values.
Sample Input: "hi hello hi"
Expected Output: {2: 2, 5: 1}
7. Write a function that accepts a dictionary of words and frequencies and returns a new
dictionary containing only words with length greater than 5.
Sample Input: {"apple": 2, "banana": 3, "kiwi": 1, "grapefruit": 4}
Expected Output: {"banana": 3, "grapefruit": 4}
8. Write a function that accepts a dictionary of keys and strings and returns the number of values
that are all uppercase.
Sample Input: {"a": "HELLO", "b": "World", "c": "TEST"}
Expected Output: 2
9. Write a function that accepts a dictionary of keys and strings and returns a new dictionary
where all values are reversed.
Sample Input: {"a": "cat", "b": "dog"}
Expected Output: {"a": "tac", "b": "god"}
[Link] a function that accepts a list of dictionaries with name and age keys and returns the
names of people older than 25.
Sample Input: [{"name": "Ann", "age": 22}, {"name": "Bob", "age": 30}]
Expected Output: ["Bob"]
11. Write a function that accepts a list of dictionaries with name and city keys and returns how
many cities end with the letter "y".
Sample Input: [{"name": "A", "city": "Kandy"}, {"name": "B", "city": "Colombo"}]
Expected Output: 1
[Link] a function that uses a for loop to group a list of words by their last letter.
Sample Input: ["cat", "bat", "dog", "dig"]
Expected Output: {"t": ["cat", "bat"], "g": ["dog", "dig"]}
13. Write a function that uses a for loop to group a list of numbers by even and odd.
Sample Input: [1, 2, 3, 4, 5]
Expected Output: {"even": [2, 4], "odd": [1, 3, 5]}
14. Write a function that uses a while loop to count the number of consonants in a string.
Sample Input: "banana"
Expected Output: 3
15. Write a function that uses a while loop to build a reversed version of a string.
Sample Input: "hello"
Expected Output: "olleh"
16. Write a function that accepts a list of strings and returns a dictionary counting how many
strings start with a vowel and how many start with a consonant.
Sample Input: ["apple", "banana", "orange", "grape"]
Expected Output: {"vowel": 2, "consonant": 2}
17. Write a function that accepts a dictionary of names and sentences and returns the names
whose sentences contain more consonants than vowels.
Sample Input: {"Ann": "idea", "Bob": "strong"}
Expected Output: ["Bob"]
18. Write a function that accepts a sentence and returns a dictionary where:
● keys = words
● values = number of consonants in each word
Sample Input:
"apple sky banana"
Expected Output:
{"apple": 3, "sky": 3, "banana": 3}
19. Write a function that accepts a list of dictionaries with keys "name" and "score" and returns a
list of names where:
● score is above the average score
Sample Input:
[{"name": "Ann", "score": 80}, {"name": "Bob", "score": 60}, {"name": "Cara", "score": 90}]
Expected Output:
["Ann", "Cara"]
20. Write a function that accepts a string and returns True if:
● no character appears more than once
otherwise return False.
Sample Input:
"lamp" → True
"letter" → False
21. Write a function that accepts a list of words and returns a new list containing only words that:
● have at least one vowel
● and do not contain repeated characters
Sample Input:
["lamp", "sky", "moon", "idea"]
Expected Output:
["lamp", "idea"]
22. Write a function that accepts a dictionary of product names and prices and returns:
● a new dictionary where each price is labeled "low" if < 100, otherwise "high"
Sample Input:
{"pen": 50, "book": 150, "bag": 200}
Expected Output:
{"pen": "low", "book": "high", "bag": "high"}
23. Write a function that accepts a sentence and returns:
● the word with the highest number of vowels
● and the vowel count
Sample Input:
"sky education apple idea"
Expected Output:
("education", 5)
24. Write a function that accepts a list of strings and returns a dictionary grouping them into:
● "uppercase" → all uppercase words
● "lowercase" → all lowercase words
● "mixed" → everything else
Sample Input:
["HELLO", "world", "PyThOn", "CODE"]
Expected Output:
{"uppercase": ["HELLO", "CODE"], "lowercase": ["world"], "mixed": ["PyThOn"]}
25. Write a function that uses a while loop to process a list of words and returns:
● how many words are palindromes
● how many are not
Sample Input:
["madam", "apple", "radar", "tree"]
Expected Output:
{"palindrome": 2, "non_palindrome": 2}