🛠️ Scheduled Maintenance | We’ll be undergoing scheduled maintenance and upgrades between 00:00 PST Jan 26th until 00:00 PST Jan 28th. There may be brief interruption of services in that period. We apologize for the inconvenience.

×
The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Two sum solution – why dictionary

Two sum solution – why dictionary

Super Learner
0
Votes
1
Answer

Hello,

I would like to ask about the solution to Two sums.
My idea is just to return i,j which goes trough for cycles as indices.

def two_sum(L, target):
    for i in range(len(L)):
        for j in range(i+1, len(L)-1):
            result = L[i] + L[j]
            if result == target:
                return [i, j]
    return -1

What I don’t understand is why should we used a dictionary in the solution of this exercise.
Does it bring any benefit?
Thanks,
Best,
Maros J

Now I get it, my solution will not work with an empty list or a list with 1 element.

3 weeks
1 Answer

365 Team
0
Votes

Hi Maros!
Thanks for reaching out.
Please accept my apologies for the delayed response.
Thank you for letting us know you can proceed with the course!
Good luck and please feel free to post another question should you encounter any difficulties. Thank you.
Best,
Martin
The 365 Team