The Even Groups Problem in Python
Video Title: The Even Groups Problem in Python
Hey everyone so here we have another Python Challenge that I wanted to work through with you all so stick around because you may learn something new by working through this with me so this challenge is called even groups in this challenge we need to create a function called split integer that takes two
Integers as inputs A and B the integer a needs to be split into B groups as evenly as possible okay so down here we’ve got a few examples of inputs and outputs okay so looking at this first example it looks like seven is needing to be split into three groups as evenly
As possible so we have two two and three because seven can’t perfectly divide into three so there’s some left over all right and looking at example two looks like we’re trying to split three into five equal parts so we’ve got two zeros at the beginning and only three ones and
Then we’ve got 10 being split into four equal parts here okay so first let’s define our function split integer with number and parts as our inputs instead of calling them A and B I think labeling them number and parts would be be more readable okay so next what I want to do
Is actually divide my number by the number of parts we want to split it into so what I’m going to do is call the built-in div mod function in Python with number and parts as the inputs now the cool thing about this function is that it Returns the quotient and the
Remainder of this division so if I’m dividing 7 by 3 it will give me two with a remainder of one so I’m going to capture those outputs by setting this equal to quotient comma remainder now let’s think further into this if I have an input of seven and three we know we
Need to take seven and split it into three groups now if I divide 7 by 3 we know we’d get two with a remainder of one so that’s where we get our quotient variable equal to two and our remainder variable equal to one okay so with this in mind if we
Think about this since we have a remainder of one here we know we’ll have only one slot here that has a plus one if we had a remainder of two we would have two slots slots with a plus one okay so that means the rest of these
Slots should just be filled with our quotient so let’s determine how many slots we need to fill with our quotient to do that let’s call it base Parts count and I want to take our parts and subtract the remainder from it so in this case if we have three parts and
Only one remainder we should have two slots remaining to put our quotient in now what I want to do is fill in those two spots with our quotient so to do that I’ll say B base Parts is equal to base Parts count times the quotient as a
Single valued list so multiplying a list like this will just duplicate it so since base Parts count is equal to two and quotient is equal to two in this case we would get a list of 2 two okay so now that we’ve dealt with the left
Side of the list let’s now handle the rest so let’s Define a variable called extra parts now how many cells left are we dealing with well since the remainder is is one we are only dealing with one slot so we’ll say remainder times our quotient again but this time we’ll add
One to it all right so now all that’s left is to combine our base parts and our extra parts into one list so let’s Return base Parts Plus extra parts and there we go we should be done all right so now that we’ve got our function I’ve
Got a series of tests down here that I want to run so let’s run this and there we go we got all passed so hopefully you enjoyed working through this problem with me hopefully you picked up something new along the way I think I’m going to start making Python
Challenge videos like this one in hopes of teaching by doing so be sure to leave some feedback about this new format thanks for watching and I’ll see you all in the next video
Video Keywords: Python, python,programming,tech,software,engineering,program,technology,coding,code,coder,programmer,engineer,software engineer,computer,computer programming,tips,codelife,computers,function,operators,new,learn python,what is python,how to,python tips,tricks,beginner,tutorial,learn,tip,slice,of,py,slice of py,even,groups,divmod,divmod(),div mod,remainder,quotient,split,divide,separate
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
def split_integer(a, b):
return [int((a+i)/b) for i in range(b)]
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.
It'd be really cool to see more of this kinda challenge videos. I enjoyed it, thank u
Really liked the video too, but can anybody tell me, what the operation on the 10th line is? Is it List comprehension?
Love this. Enjoyed working it out myself, and then learning something from the way you did it. Definitely looking forward to more of these
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
Do you have a udemy class or something like that?
Hola.me ha costado entender el ejercicio…
Estoy aprendiendo las bases , fundamentos de Python…
What do you mean split?
What is the result of this algorithm for a test case (16,3) ? Does it return [5,5,6] ?
Don't forget your discord server
def split_integer(a, b):
return [a//b + 1 if ((b – 1) – i) < a%b else a//b for i in range(b)]
I loved this format, very informative
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?
This is good❤
cool
❌ First
✅ Early
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)
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))
@b001 what theme are you using. It looks very good 🙂
Awesome, thank you, Love how thorought you were in the explanation.
Love this format
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.
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
def split_integers(a,b):
x, y = divmod(a,b)
ans = (b-y)*[x] + y*[x+1]
return ans
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
Great job! Very informative!
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.