PHP-Noobie
PHP-Noobie

Reputation: 31

Not able to understand Python3 enumerate()

Question: Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

class Solution:
def twoSum(self, nums, target):

    lookup={}
    for cnt, num in enumerate (nums):
        if target-num in lookup:
            return lookup[target-num], cnt
        lookup[num]=cnt

I am not able to understand the steps after for loop is used.I am new on Python, someone please help me.

Upvotes: 2

Views: 211

Answers (2)

Vinay Kumar
Vinay Kumar

Reputation: 300

enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.

For e.g.
>>>list(enumerate("abc"))

Gives

[(0, 'a'), (1, 'b'), (2, 'c')]

For easy understanding, I'm commenting your program. Go through it, you'll surely understand.
class Solution:
def twoSum(self, nums, target):
    
    # lookup is a dictionary that stores the number and its index 
    # e.g. '{7:1}'
    #     number 7 at index 1 
    lookup={}

    # As explained above cnt and num will receive values one by one along with index.
    for cnt, num in enumerate (nums):
        
        # We look if the number required to be added into the 'num' is present in dictionary
        if target-num in lookup:
            # if value found in lookup then we return the current index along with the index of number found in lookup.
            return lookup[target-num], cnt

        # After every loop insert the current value and its index into the lookup dictionary.
        lookup[num]=cnt

Hope, I answered your query in the way you wanted. Please comment below, if anything is left unanswered, I'll surely try to answer that as well.

Upvotes: 0

Tensza
Tensza

Reputation: 213

Let me help you understand by explaining what the code does and how it solves the problem.

We need to find two numbers that sum to 9, to achieve this, we can iterate over every number in the array and then look if we already encountered a number that equals the target number minus the number we are currently on. If we haven't encountered such a number yet, we store the current number and its corresponding index.

Because we need to return the indices, we want to be able to look for the number-target pairs and immediately get the index. The solution uses a dictionary to store a number (key) and return an index as (value).

We iterate over every number, if we already encountered target-number before, we can return the current index and the index of the target-number, if we haven't encountered that number, we simply store the current number and its index.

The enumerate part, simply provides an index along with the value of the array that is being iterated, in the form of (id, item).

class Solution:
    def twoSum(self, nums, target):
        # Here a dictionary is created, which will store value, index as key, value pairs.
        lookup={}
        # For every number in the array, get the index (cnt) and number (num)
        for cnt, num in enumerate (nums):
            # If we find target-num, we know that num + target-num = target
            if target-num in lookup:
                # Hence we return the index of the target-num we stored in the dict, and the index of the current value (cnt)
                return lookup[target-num], cnt
            # Otherwise we store the current number as key with its index as value
            lookup[num]=cnt

Upvotes: 1

Related Questions