Python Practice Lab
easyUnit 9 · Comprehensions: list, set, dict

P26. Dict and Set Comprehensions

Comprehensions: list, set, dict

Read a number n and a sentence. Then build:

LineWhat to buildComprehension
1A dictionary mapping each number from 1 to n to its squaredict comprehension
2The distinct word lengths in the sentence, sortedset comprehension
3The words longer than 4 letters, converted to uppercase, in input orderlist 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.

Hints

Saved
Custom 1

This is your own input, so Run will show what your program prints but can't say whether it's correct.