Python OOPE Exam


2024 Sep Oppe 1 Set 1

๐Ÿš€ Function: increment_value_with_max_limit

Here’s a function to increment the value for a given key in a dictionary, making sure it never exceeds a specified maximum limit!

def increment_value_with_max_limit(d, key, inc, limit):
    """
    Increment the value of d[key] by inc, but do not let it exceed limit.

    Args:
        d (dict): Dictionary with integer values.
        key: Key whose value you want to increment.
        inc (int): The increment value.
        limit (int): The maximum allowed value.

    Returns:
        None: The dictionary is modified in-place.

    Example:
        d = {'a': 5}
        increment_value_with_max_limit(d, 'a', 10, 12)
        # Now d['a'] == 12
    """
    if key in d:
        d[key] = min(d[key] + inc, limit)

โœจ Step-by-Step Explanation

  1. Check Key Exists: Make sure the key is present in the dictionary.
  2. Increment and Cap: Add the inc value to the current value for the key. Use min(new_value, limit) to cap the result at limit if it would go over.
  3. Update In Place: The original dictionary is modified directly. No return value necessary!

๐Ÿ“ Practice Questions

  1. If d = {'x': 7}, what does increment_value_with_max_limit(d, 'x', 4, 10) do?
  2. What happens for d = {'b': 2}, increment_value_with_max_limit(d, 'b', 5, 5)?
  3. Try d = {'score': 20}, increment_value_with_max_limit(d, 'score', 3, 22)

โœ… Solutions

  1. The value becomes min(7+4, 10) = 10. So, d['x'] will be 10.
  2. The value becomes min(2+5, 5) = 5. So, d['b'] will be 5.
  3. The value becomes min(20+3, 22) = 22. So, d['score'] will be 22.

๐Ÿ’ก Now you can safely increment dictionary values without ever exceeding the maximum you set!