These videos are great! I suggest talking a bit slower though so less advanced coders like myself have an easier time following :)
@MsNennifer3 жыл бұрын
You could always play the video at a slower speed.
@NN-uq6de2 жыл бұрын
@@MsNennifer Or he can go a little slower and explain things more thoroughly on each topic
@Code08662 жыл бұрын
@@NN-uq6de This is for intermediate-expert level programmers. You need to study more if you cant follow.
@rebeccatom21882 жыл бұрын
@@Code0866 ok nerd
@satyamagrawal68542 жыл бұрын
your runtime will decrease significantly if you use for i in range(0,len(nums)): rather than enumerate
@cvxcfv3 жыл бұрын
Probably the best explanation out of the 4 videos I've invested watching
@1pomii3 жыл бұрын
so what was the point of this? to show everyone you can talk fast?
@njokuiyke014 жыл бұрын
Hi, at what point did the elements get added to the dictionary
@justinnguyen80493 жыл бұрын
it was at line 18, values[elem] = i, that the element got put into the dictionary.
@RegiiPad4 жыл бұрын
You can have more than one pair for the same target. The enhanced version of your code follows: 1 #!/usr/bin/python 2 3 from collections import namedtuple 4 import os 5 import sys 6 7 Pair = namedtuple('Pair', 'x1, x2, v1, v2') 8 9 def two_sum(list, target): 10 values = dict() 11 pairs = [] 12 for i, elem in enumerate(list): 13 comp = target - elem 14 if comp in values: 15 pairs.append(Pair(values[comp], i, list[values[comp]], list[i])) 16 pass 17 values[elem] = i 18 pass 19 return pairs 20 pass 21 22 def main(): 23 arr = [1, 2, 3, 4, 5, 6, 7] 24 tgt = 8 25 results = two_sum(arr, tgt) 26 27 for p in results: 28 print("Numbers are [{}]={} and [{}]={} to add {}".format( 29 p.x1, p.v1, p.x2, p.v2, tgt)) 30 pass 31 pass 32 33 if __name__ == '__main__': 34 main() 35 print("** END **") 36 pass 37 pass
@KiteHQ4 жыл бұрын
! you should join our fb group and help out others looking to learn Python facebook.com/groups/505658083720291
@RegiiPad4 жыл бұрын
Kite applied today
@codewithchandan61463 жыл бұрын
Hy can you solve my code please
@KiteHQ4 жыл бұрын
If you liked this video, join the Kite Developer Community on Facebook for access to more resources + support from fellow Python developers. Time to level up! facebook.com/groups/505658083720291
@damanvirdi1682 жыл бұрын
nice and crisp explanation
@Samantha-no4es4 жыл бұрын
Can someone explain line 14? When did anything get added to the dictionary? To me, it seems to be empty so I don't yet understand why we're checking if the compliment is there :o
@SouthernHadoken4 жыл бұрын
It got added on line 18. That the else in if/else.
@universal43344 жыл бұрын
@@SouthernHadoken then all the elements added to dic . Finally returns empty list.
@faisalabdulkadir94393 жыл бұрын
Why do we map the elements and indexes to the dict after our if statement??
@SayfSentinel4 жыл бұрын
Hi Your solution does not apply for an array like [3,2,4] and a target of 6 When you enter your if statement target minus 3 return 3 which is already in your array and stop the loop
@cristianmurrachavez53804 жыл бұрын
No, because its searching in the dict not in the array and the number is only added after asking
@BrendanMetcalfe4 жыл бұрын
Where is the code from the example?
@harrisonooi2963 жыл бұрын
@0:56
@frenchmike3 жыл бұрын
reading the answers at that pace with not detail or visuals it no better than handing me the code, no need for a video for that.
@aditiparashar91712 жыл бұрын
you are amazing
@BrendanMetcalfe4 жыл бұрын
Thanks!
@TheJmization3 жыл бұрын
wtf this is on easy mode wtf
@seemeyeah13624 жыл бұрын
I was under the impression line 18 would include the elif statement, and an else statement for line 20. How comes this isn't the case?
@judahmcnicholl83343 жыл бұрын
I know its a bit late for an answer, but when returning out of a function nothing below it gets executed, so if that statement is hit you have found the solution.
@marufmazumder82943 жыл бұрын
values[elem] = i ; ====> why is that?
@killagarcia3923 жыл бұрын
by doing so we map the key (element from our list) to the key value (index) {element:index,...}
@aakashjana62254 жыл бұрын
I think you have over complicated the problem just run a nested loop where the second loop starts from one position after the current iterator value of the outer loop add the values stored in both the indexes and check if the equal your target that's it for i in range(0,len(arr)): for j in range(i, len(arr)): If are[i]+arr[j]==target: return i, j return False
@manastrivedi38414 жыл бұрын
It might seem like the solution you gave us optional but actually, it has a complexity of O(n^2)
@7minutesdead4 жыл бұрын
Since this has a complexity of O(n^2), Leetcode won't accept it because your time limit will exceed on the later test cases that have very large input lists (I tried to do the same thing more or less). The solution in the video is much faster and won't time out.
@ras_mag3 жыл бұрын
This solution is not optimal you are scanning the whole list for the solution.