Home > Enterprise >  index value of the tuples
index value of the tuples

Time:06-16

write a function named sums that takes 3 arguments: 2 lists of integers nums1 and nums2, and an integer target; then return a tuple containing indices i and j of the two numbers in nums1 and nums2, respectively, where the numbers at that indices add up to target (i.e, nums1[i] num2[j] = target). If there is no solution, return “Not found”. The function sums must contain exactly only one loop (either a for-loop or a while-loop), otherwise you will get no point. A nested-loop is considered as more than one loop.

def sums(nums1, nums2, target):
nums1 = [11, 2, 15, 7, 8]
nums2 = [2, 3, 4, 5]
target_number = 9
for i in range(len(nums1)):
    for j in range(len(nums2)):
        total = nums1[i]   nums2[j]
        if total == target:
            a = (nums1[i], nums2[j])
            print(a)

This is the code that i have written and i am struggling to get the index values can anyone please help!

CodePudding user response:

You can solve it in O(n) with a dictionary and a single for loop;

nums1 = [11, 2, 15, 7, 8]
nums2 = [2, 3, 4, 5]
target_number = 9

def sums(nums1, nums2, target):
    d = dict(zip(nums2, range(len(nums2))))
    for i,n in enumerate(nums1):
        remainder = target-n
        if remainder in d:
            return (i, d[remainder])
    return 'Not found'

sums(nums1, nums2, target_number)

Output: (3, 0)

Be aware that while there is a single for loop, the code is looping to construct the dictionary, it's just not visible directly.

CodePudding user response:

Here's an alternative solution using basic index incrementing. It's not as efficient as using a dictionary and finding the difference, as this naively iterates over b as many times as there are items in a. In the worst case, with two equal-length lists, this solution is O(n^2).

Although it may actually be faster in smaller cases, if there happens to be a bottlneck around the creation of any of the intermediate data structures that @mozway uses. I'm only speculating on this, I don't actually know.

def target_sum_idxs(a, b, target):
    i = j = 0
    while i < len(a):
        if a[i]   b[j] == target:
            return i, j
        j  = 1
        if j == len(b):
            i  = 1
            j = 0
    return "Not found"

Demo:

>>> target_sum_idxs(nums1, nums2, target=9)
(3, 0)

>>> target_sum_idxs(nums2, nums1, target=9)
(0, 3)

>>> target_sum_idxs(nums1, nums2, target=999)
'Not found'

CodePudding user response:

just one little thing.

Check that the variable in the if statement is really your parameter, not the "target_number" variable. Also, in the if statement, you need to stop execution, maybe you can use a break or return the tuple.

I would use something like this:

def sums(nums1, nums2, target):
    for i in range(len(nums1)):
        for j in range(len(nums2)):
            total = nums1[i]   nums2[j]
            if total == target:
                a = (i, j)
                return a
                
    return "Not found"
  • Related