Hone logo
Hone
Problems

Dictionary Comprehension Challenge: Squaring and Filtering Numbers

This challenge focuses on the efficient creation of dictionaries in Python using dictionary comprehensions. You will be asked to transform a list of numbers into a dictionary where keys are the original numbers and values are their squares, but only for numbers that meet a specific condition. This exercise is fundamental for writing concise and readable Python code for data transformation.

Problem Description

Your task is to create a Python dictionary from a given list of integers. The dictionary should map each number from the input list to its corresponding square. However, there's a condition: only numbers that are greater than a specified threshold should be included in the resulting dictionary.

Requirements:

  • Use a dictionary comprehension to generate the output dictionary.
  • The keys of the dictionary should be the numbers from the input list.
  • The values of the dictionary should be the squares of the corresponding keys.
  • Only include key-value pairs where the key (the original number) is strictly greater than the provided threshold.

Expected Behavior:

The function should accept two arguments: a list of integers and an integer threshold. It should return a dictionary containing the squared values for numbers exceeding the threshold.

Edge Cases:

  • An empty input list.
  • A threshold that is higher than all numbers in the list.
  • A threshold that is lower than all numbers in the list.
  • The input list contains duplicate numbers.

Examples

Example 1:

Input:
numbers = [1, 2, 3, 4, 5, 6]
threshold = 3
Output:
{4: 16, 5: 25, 6: 36}
Explanation: The numbers greater than 3 are 4, 5, and 6. Their squares are 16, 25, and 36 respectively, forming the key-value pairs in the output dictionary.

Example 2:

Input:
numbers = [10, 20, 5, 15, 25]
threshold = 10
Output:
{15: 225, 20: 400, 25: 625}
Explanation: Numbers greater than 10 are 15, 20, and 25. Their squares form the dictionary: {15: 15*15, 20: 20*20, 25: 25*25}.

Example 3:

Input:
numbers = [7, 8, 9]
threshold = 10
Output:
{}
Explanation: No numbers in the list are greater than the threshold of 10, so an empty dictionary is returned.

Constraints

  • The input list numbers will contain between 0 and 1000 integers.
  • Each integer in numbers will be between -1000 and 1000, inclusive.
  • The threshold will be an integer between -1000 and 1000, inclusive.
  • The solution should be efficient, ideally with a time complexity proportional to the size of the input list.

Notes

  • Dictionary comprehensions provide a concise way to create dictionaries. The general syntax is {key_expression: value_expression for item in iterable if condition}.
  • Consider how to handle negative numbers when squaring them. The square of a negative number is positive.
  • If the input list contains duplicates, the comprehension will naturally handle them by overwriting previous entries if a duplicate key is encountered later in the list.
Loading editor...
python