Hone logo
Hone
Problems

Implementing the enumerate Function in Python

The built-in enumerate function in Python is a powerful tool for iterating over a sequence (like a list, tuple, or string) while simultaneously keeping track of the index of each element. This challenge asks you to implement your own version of enumerate to understand its underlying functionality and gain a deeper appreciation for Python's built-in features. Your implementation should mimic the behavior of the standard enumerate function.

Problem Description

You are tasked with creating a function called my_enumerate that takes an iterable (e.g., a list, tuple, string) and an optional starting index as input. The function should return an iterator that yields pairs of (index, value) for each element in the iterable, starting from the specified index. If the starting index is not provided, it should default to 0.

Key Requirements:

  • The function must accept an iterable as its first argument.
  • The function must accept an optional integer argument representing the starting index.
  • The function must return an iterator.
  • The iterator should yield tuples of the form (index, value).
  • The index should increment from the starting index with each iteration.
  • The function should handle empty iterables gracefully.

Expected Behavior:

When used in a loop, my_enumerate should behave identically to the built-in enumerate.

Edge Cases to Consider:

  • Empty iterable: Should yield nothing.
  • Starting index is negative: Should still function correctly, counting down from the starting index.
  • Iterable with non-integer indices (e.g., a dictionary's keys): Your function should still work, iterating through the keys in the order they are returned by the dictionary.

Examples

Example 1:

Input: my_enumerate(['a', 'b', 'c'])
Output: (0, 'a'), (1, 'b'), (2, 'c')
Explanation: Iterates through the list ['a', 'b', 'c'], starting the index at 0.

Example 2:

Input: my_enumerate(['a', 'b', 'c'], start=1)
Output: (1, 'a'), (2, 'b'), (3, 'c')
Explanation: Iterates through the list ['a', 'b', 'c'], starting the index at 1.

Example 3:

Input: my_enumerate([])
Output: (empty iterator)
Explanation: The iterable is empty, so the iterator yields nothing.

Example 4:

Input: my_enumerate(('x', 'y', 'z'), start=-1)
Output: (-1, 'x'), (-2, 'y'), (-3, 'z')
Explanation: Iterates through the tuple ('x', 'y', 'z'), starting the index at -1.

Constraints

  • The input iterable can be any iterable object (list, tuple, string, set, dictionary keys, etc.).
  • The starting index must be an integer.
  • The function should be efficient enough to handle iterables with a reasonable number of elements (up to 1000). While performance is not the primary focus, avoid excessively inefficient solutions.
  • The function should not modify the original iterable.

Notes

Consider using a generator function to create the iterator. A generator function allows you to produce values on demand, which can be more memory-efficient than creating a list of all the (index, value) pairs upfront. Think about how to handle the starting index correctly and how to ensure the index increments properly with each iteration.

Loading editor...
python