easyUnit 9 · Comprehensions: list, set, dict
P26. Dict and Set Comprehensions
Comprehensions: list, set, dict
Read a number n and a sentence. Then build:
| Line | What to build | Comprehension |
|---|---|---|
| 1 | A dictionary mapping each number from 1 to n to its square | dict comprehension |
| 2 | The distinct word lengths in the sentence, sorted | set comprehension |
| 3 | The words longer than 4 letters, converted to uppercase, in input order | list comprehension |
Input Format
- Line 1:
n - Line 2: a sentence of words separated by spaces
Output Format
- Line 1:
Squares: <dict> - Line 2:
Distinct lengths: <sorted list> - Line 3:
Long words: <list>
Constraints
- 1 ≤
n≤ 20 - 1 ≤ number of words ≤ 50
Sample Test Cases
Sample 1
Input
5 python makes coding simple and fun
Output
Squares: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Distinct lengths: [3, 5, 6]
Long words: ['PYTHON', 'MAKES', 'CODING', 'SIMPLE']Sample 2
Input
1 hi
Output
Squares: {1: 1}
Distinct lengths: [2]
Long words: []Submitting checks 2 sample cases and 2 hidden test cases.