The Even Groups Problem in Python

Поделиться
HTML-код
  • Опубликовано: 31 дек 2024

Комментарии • 46

  • @Bandonnn
    @Bandonnn Год назад +16

    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

  • @adityaram7408
    @adityaram7408 Год назад +17

    def split_integers(a,b):
    x, y = divmod(a,b)
    ans = (b-y)*[x] + y*[x+1]
    return ans

  • @finnthirud
    @finnthirud Год назад +7

    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.

  • @jedi2109
    @jedi2109 Год назад +2

    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

  • @husanboyabdullayev7709
    @husanboyabdullayev7709 Год назад +1

    It'd be really cool to see more of this kinda challenge videos. I enjoyed it, thank u

  • @Is_this_username_unique
    @Is_this_username_unique Год назад +5

    Love this format

  • @stevencromb5589
    @stevencromb5589 Год назад

    Love this. Enjoyed working it out myself, and then learning something from the way you did it. Definitely looking forward to more of these

  • @Aborrajardo
    @Aborrajardo Год назад +1

    Awesome, thank you, Love how thorought you were in the explanation.

  • @gabrielushijima1525
    @gabrielushijima1525 Год назад

    I love Python challenges; please keep doing this type of content.

  • @minamagdy4126
    @minamagdy4126 Год назад +2

    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.

    • @dp.229
      @dp.229 11 месяцев назад

      You don't even need to call int(). Python can deal with + False and + True. It will be the same as +0 and +1

  • @TheFrostFyr
    @TheFrostFyr Год назад

    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)]

  • @Gijy2539
    @Gijy2539 Год назад +14

    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.

    • @blackwins7397
      @blackwins7397 Год назад

      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

    • @SmashPortal
      @SmashPortal Год назад

      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.

    • @FacuA0
      @FacuA0 Год назад

      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.

    • @minamagdy4126
      @minamagdy4126 Год назад

      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.

  • @Malloway___
    @Malloway___ Год назад

    I loved this format, very informative

  • @Chasemuss
    @Chasemuss Год назад +1

    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)

  • @mind_of_a_darkhorse
    @mind_of_a_darkhorse Год назад +2

    Great job! Very informative!

  • @eleanor-forte
    @eleanor-forte Год назад

    def split_integer(a, b):
    return [int((a+i)/b) for i in range(b)]

  • @KhanhNguyen-qo7pc
    @KhanhNguyen-qo7pc Год назад +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?

    • @epsi
      @epsi Год назад

      Yes, a bit of algebraic manipulation indicates the correct result requires
      extra_parts = remainder * [quotient + 1]

  • @hencethebeetroot
    @hencethebeetroot Год назад +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))

    • @epsi
      @epsi Год назад

      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))

  • @lucafly8847
    @lucafly8847 Год назад +1

    @b001 what theme are you using. It looks very good :)

    • @b001
      @b001  Год назад +1

      SynthWave’84

    • @lucafly8847
      @lucafly8847 Год назад

      Thanks! Will use this now :)

  • @B0nerM0aner
    @B0nerM0aner Год назад

    Really liked the video too, but can anybody tell me, what the operation on the 10th line is? Is it List comprehension?

    • @aracristiano462
      @aracristiano462 Год назад

      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

  • @kavianam
    @kavianam Год назад

    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

  • @chestnutmongrel
    @chestnutmongrel Год назад

    [(whole := a // b) + 1] * (remainder := a % b) + [whole] * (b - remainder)

  • @ramasuryamjalamanchili9156
    @ramasuryamjalamanchili9156 Год назад

    How do you instantly copy-paste each part of your code in your videos?

    • @FetchTheCow
      @FetchTheCow Год назад

      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.

  • @DavLim86
    @DavLim86 Год назад

    Do you have a udemy class or something like that?

  • @roobinroobin1489
    @roobinroobin1489 Год назад

    What is the result of this algorithm for a test case (16,3) ? Does it return [5,5,6] ?

    • @epsi
      @epsi Год назад

      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")

  • @rodrigoaraya3478
    @rodrigoaraya3478 Год назад

    What do you mean split?

  • @defrost5536
    @defrost5536 Год назад

    def split_integer(a, b):
    return [a//b + 1 if ((b - 1) - i) < a%b else a//b for i in range(b)]

  • @XenoTravis
    @XenoTravis Год назад +9

    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.

  • @someyoutube
    @someyoutube Год назад

    This is good❤

  • @rodrigoaraya3478
    @rodrigoaraya3478 Год назад

    Hola.me ha costado entender el ejercicio...
    Estoy aprendiendo las bases , fundamentos de Python...

  • @Mystery_Glitch
    @Mystery_Glitch Год назад

    Don't forget your discord server

  • @icepower3271
    @icepower3271 Год назад

    cool

  • @raodexi1379
    @raodexi1379 Год назад

    ❌ First
    ✅ Early

  • @annon010
    @annon010 Год назад

    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.