This video is arguably my favourite one that u made as someone who’s trynna learn how to code (and struggles with logic and efficiency), I’d love to see more like this
Scenario-based training is great. And you show how to name things, which can be so difficult since they feel like time capsules that is going to be read and understood in the future.
I started learning python a couple weeks ago and your videos have been incredibly helpful. I would absolutely love to see more content like this. After seeing you use divmod() I decided to pause the video give it a shot and came up with the snippet below. Took me waay longer than the few minutes you spent on it, but it's something lol. def split_integer(num, groups): list_1 = [] numerator, denominator = divmod(num, groups) for x in range(groups): list_1.append(numerator) for y in range(denominator): list_1[y] += 1 return list_1
Personally, I would've used list comprehension like: return [quotient + int(i > base_parts) for i in range(parts)] to save on adding lists. The idea is that the number of base-parts will determine the earlier cases that are just the quotient, while later parts will be incremented.
took the time to make my own with list comprehension, love your videos man. [(number // parts)+(i >= (parts - (number % parts))) for i in range(parts)]
This is way more efficient than what I was thinking. I would have made a list with B items and just looped over it adding one each time and removing 1 from A. so if A was 10 and B was 3 it would start [0,0,0] then go [1,0,0] then [1,1,0] etc.
I thought the same, with the added complexity that the remainder elements should be added at the end (as the examples show) instead of the beginning, so in my case, the elements were being added to the list backwards, with an index decrementing and going back to the top until all elements were put in there.
You'd have to do so backwards to have the extra increments be at the end of the list. Also, your solution, unlike his (pending implementation details of divmod()), doesn't work for negative numbers.
Decided to give this a whack, and while it's not as efficient, I think it's not terrible: def split_integer(integer: int, num_groups: int) -> list[int]: results = [0 for x in range(num_groups)] while sum(results) < integer: results[results.index(min(results))] += 1
Your video is awesome! But I still not get the part: extra_parts = remainder * [quotient + 1]. Why we + 1 instead of plus others number? Is there some kind of math intuition behind it?
managed to get it into one line at the cost of making it incomprehensible to read def split_integer(num, groups): return ([int(num / groups)] * (groups - (num % groups))) + ([int(num / groups) + 1] * (num % groups))
The walrus operator used to assign a name to the result of divmod() helps a tiny bit to reduce some of the repetition in your code, but it's still pretty unreadable: def split_integer(num, groups): return [(qr := divmod(num, groups))[0]] * (groups - qr[1]) + [qr[0]+1] * qr[1] If one could use the walrus operator to unpack a tuple, that would probably be even better as one could name the individual results of divmod(), but until issue 43143 on the CPython bug tracker (issue 87309 on the CPython GitHub) is addressed, the only other way to do it in a single line would be with a lambda, which actually looks more readable to me: def split_integer(num, groups): return (lambda q, r: [q] * (groups - r) + [q+1] * r)(*divmod(num, groups))
Yes, it is a list comprehension [ “passed” for v else “failed”] but in his case he use dictionary it is the same you can watch this video it will help you simplify python she is so cool at explaining some term of python programming
Yes. Here's a more general test function: # test_split_integer( \ # (7,3), (10,7), (16,3), \ # ) def test_split_integer(*cases): for i, case in enumerate(cases): result = split_integer(*case) q, _ = divmod(*case) passed = \ number == sum(result) and \ parts == len( \ v == q or v == q + 1 \ for v in result) print(f"{i}:", "Passed" if passed else "Failed")
Videos like this would be better if you focus more on walking through the problem before showing the code. Someone will be able to understand the code from a short walk through but it won't stick as well in the long run without bringing the viewer to think for themselves before seeing what the next line is.
I tried this before you showed how to do it, and this is my code: def split_integer(num, groups): return [num // groups] * (groups - (num % groups)) + [num // groups + 1] * (num % groups) It is compacted a bit but yea.
This video is arguably my favourite one that u made as someone who’s trynna learn how to code (and struggles with logic and efficiency), I’d love to see more like this
def split_integers(a,b):
x, y = divmod(a,b)
ans = (b-y)*[x] + y*[x+1]
return ans
Scenario-based training is great. And you show how to name things, which can be so difficult since they feel like time capsules that is going to be read and understood in the future.
I started learning python a couple weeks ago and your videos have been incredibly helpful.
I would absolutely love to see more content like this.
After seeing you use divmod() I decided to pause the video give it a shot and came up with the snippet below. Took me waay longer than the few minutes you spent on it, but it's something lol.
def split_integer(num, groups):
list_1 = []
numerator, denominator = divmod(num, groups)
for x in range(groups):
list_1.append(numerator)
for y in range(denominator):
list_1[y] += 1
return list_1
It'd be really cool to see more of this kinda challenge videos. I enjoyed it, thank u
Love this format
Love this. Enjoyed working it out myself, and then learning something from the way you did it. Definitely looking forward to more of these
Awesome, thank you, Love how thorought you were in the explanation.
I love Python challenges; please keep doing this type of content.
Personally, I would've used list comprehension like:
return [quotient + int(i > base_parts) for i in range(parts)]
to save on adding lists. The idea is that the number of base-parts will determine the earlier cases that are just the quotient, while later parts will be incremented.
You don't even need to call int(). Python can deal with + False and + True. It will be the same as +0 and +1
took the time to make my own with list comprehension, love your videos man.
[(number // parts)+(i >= (parts - (number % parts))) for i in range(parts)]
This is way more efficient than what I was thinking. I would have made a list with B items and just looped over it adding one each time and removing 1 from A. so if A was 10 and B was 3 it would start [0,0,0] then go [1,0,0] then [1,1,0] etc.
i have the same concept too, but i feel like the time complexity will be really bad when handling greater number of A and B
I was thinking something similar, but only with the remainder. I knew about list multiplication, but I didn't consider just making two separate lists.
I thought the same, with the added complexity that the remainder elements should be added at the end (as the examples show) instead of the beginning, so in my case, the elements were being added to the list backwards, with an index decrementing and going back to the top until all elements were put in there.
You'd have to do so backwards to have the extra increments be at the end of the list. Also, your solution, unlike his (pending implementation details of divmod()), doesn't work for negative numbers.
I loved this format, very informative
Decided to give this a whack, and while it's not as efficient, I think it's not terrible:
def split_integer(integer: int, num_groups: int) -> list[int]:
results = [0 for x in range(num_groups)]
while sum(results) < integer:
results[results.index(min(results))] += 1
return sorted(results)
Great job! Very informative!
def split_integer(a, b):
return [int((a+i)/b) for i in range(b)]
Your video is awesome! But I still not get the part: extra_parts = remainder * [quotient + 1]. Why we + 1 instead of plus others number? Is there some kind of math intuition behind it?
Yes, a bit of algebraic manipulation indicates the correct result requires
extra_parts = remainder * [quotient + 1]
managed to get it into one line at the cost of making it incomprehensible to read
def split_integer(num, groups):
return ([int(num / groups)] * (groups - (num % groups))) + ([int(num / groups) + 1] * (num % groups))
The walrus operator used to assign a name to the result of divmod() helps a tiny bit to reduce some of the repetition in your code, but it's still pretty unreadable:
def split_integer(num, groups):
return [(qr := divmod(num, groups))[0]] * (groups - qr[1]) + [qr[0]+1] * qr[1]
If one could use the walrus operator to unpack a tuple, that would probably be even better as one could name the individual results of divmod(), but until issue 43143 on the CPython bug tracker (issue 87309 on the CPython GitHub) is addressed, the only other way to do it in a single line would be with a lambda, which actually looks more readable to me:
def split_integer(num, groups):
return (lambda q, r: [q] * (groups - r) + [q+1] * r)(*divmod(num, groups))
@b001 what theme are you using. It looks very good :)
SynthWave’84
Thanks! Will use this now :)
Really liked the video too, but can anybody tell me, what the operation on the 10th line is? Is it List comprehension?
Yes, it is a list comprehension
[ “passed” for v else “failed”] but in his case he use dictionary it is the same you can watch this video it will help you simplify python she is so cool at explaining some term of python programming
def split_integer(a: int, b: int) -> list:
res = [0] * b
i = 0
while a > 0:
res[i] += 1
a -= 1
i = i + 1 if i + 1 < b else 0
return res
[(whole := a // b) + 1] * (remainder := a % b) + [whole] * (b - remainder)
How do you instantly copy-paste each part of your code in your videos?
My guess is he wrote the whole program, deleted the snippets from bottom to top, and then hit undo while recording to reveal the code snippets.
Do you have a udemy class or something like that?
What is the result of this algorithm for a test case (16,3) ? Does it return [5,5,6] ?
Yes.
Here's a more general test function:
# test_split_integer( \
# (7,3), (10,7), (16,3), \
# )
def test_split_integer(*cases):
for i, case in enumerate(cases):
result = split_integer(*case)
q, _ = divmod(*case)
passed = \
number == sum(result) and \
parts == len( \
v == q or v == q + 1 \
for v in result)
print(f"{i}:", "Passed" if passed else "Failed")
What do you mean split?
def split_integer(a, b):
return [a//b + 1 if ((b - 1) - i) < a%b else a//b for i in range(b)]
Videos like this would be better if you focus more on walking through the problem before showing the code. Someone will be able to understand the code from a short walk through but it won't stick as well in the long run without bringing the viewer to think for themselves before seeing what the next line is.
This is good❤
Hola.me ha costado entender el ejercicio...
Estoy aprendiendo las bases , fundamentos de Python...
Don't forget your discord server
cool
❌ First
✅ Early
I tried this before you showed how to do it, and this is my code:
def split_integer(num, groups):
return [num // groups] * (groups - (num % groups)) + [num // groups + 1] * (num % groups)
It is compacted a bit but yea.