We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Currying Practice

Doc2Doc should include a feature for image resizing, allowing users to adjust image dimensions to specified ranges. This ensures that images in documents fit and aren't freakishly large or hilariously small.

Assignment

Complete the new_resizer function using currying. It should make sure the image dimensions are never smaller than the minimum width and height, or larger than the maximum width and height specified.

Check the example below to see how the function is intended to be called.

Example

If our new_resizer function returns a set_min_size function, and set_min_size returns a resize_image function, we would use it like this:

from collections.abc import Callable

ResizeFunc = Callable[[int, int], tuple[int, int]]
SetMinSizeFunc = Callable[..., ResizeFunc]

# Step 1: Create the resizer with maximum dimensions
set_min_size: SetMinSizeFunc = new_resizer(800, 600)

# Step 2: Set the minimum dimensions
resize_image: ResizeFunc = set_min_size(200, 100)

# Step 3: Resize the image
new_width: int
new_height: int
new_width, new_height = resize_image(1000, 500)

# Step 4: Output the result
print(new_width, new_height)  # Output: 800, 500

# With currying syntax
print(new_resizer(800, 600)(200, 100)(1000, 500))  # Output: (800, 500)

Tip

If you have a value and an upper bound, using min(value, upper_bound) returns the value capped at the upper bound, i.e., the value will be returned as is unless it exceeds the upper bound.

value: int = 50
min(value, 100)  # returns 50

value = 120
min(value, 100)  # returns 100

The opposite works for lower bounds. Just use max instead.

value: int = 50
max(value, 0)  # returns 50

value = -2
max(value, 0)  # returns 0