#πŸ”’ Index problem

20 messages Β· Page 1 of 1 (latest)

wanton moon
#
def two_sum(numbers, target):
   res = ('',)
   for num in numbers:
       for num2 in numbers:
           if num + num2 == target and numbers.index(num) != numbers.index(num2):
              res = (numbers.index(num), numbers.index(num2))
   print(res)
   return res

The code doesnt work when theres a solution with similar indexes https://www.codewars.com/kata/52c31f8e6605bcc646000082/train/python

Codewars

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

glacial snowBOT
#

@wanton moon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wanton moon
#

Please helpπŸ™

#

Index problem

stone ridge
#

the problem here is that you re index the given number you found aka looping in the arr until the current number is the one you are looking for.
a better way of doing this would be to iterate on the length of the numbers as i and j and use numbers[i] instead of num and numbers[j] as num2

#

you also don't have to both return and print the result you can just return it

#

they want fast code or beautiful code?

#

if they want the later one you could also use enumerate that returns both the number at the given index and the index

#

if you really want to know the fastest solution there is a way to solve this with only 1 for loop over the numbers

#

nevermind I was wrong enumerate has about the same speed as only indexing

#

if you want to know the best solution I could come up with:

#
def two_sum(numbers: list[int], target: int) -> tuple[int, int]:
    # Create a dictionary to store the indices of the numbers we have seen
    num_to_index: dict[int, int] = {}

    for i, num in enumerate(numbers):
        # Calculate the complement of the current number
        complement = target - num

        # Check if the complement is already in the dictionary
        if complement in num_to_index:
            # Return the indices of the complement and the current number
            return (num_to_index[complement], i)

        # Store the index of the current number in the dictionary
        num_to_index[num] = i
stone ridge
#

@wanton moon

wanton moon
#

Oh shit im sorry

#

I forgot to say

#

I got it solved with enemurate

#

thanks for the help though, imma learn your method as well

glacial snowBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.