-1

So I am trying to figure out how to divide the given amount of people into given group sizes for example: 11 people into groups of 3, and the program giving the answer 4, since the remainder should form a group also on top, as only one group can contain less than the given group size, but I cannot figure out how to do this without if-statements, and with just simply using math operators, and I am asking for guidance.

I tried something alongside asking for the amount of people and the group size and storing them in the variables called [amount] and [size], and tried to figure out how to get the remainder in there too with the use of "//" operator, but I cannot figure it out.

Nohahhex
  • 11
  • 1
  • This sounds like a class assignment, which means there may be some restrictions on what you can do, but the [`itertools.zip_longest`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) method may get you most of the way to a solution. – larsks Feb 09 '23 at 21:38

2 Answers2

0

You mean like that?

import math
math.ceil(people_count/group_size)
AboAmmar
  • 5,439
  • 2
  • 13
  • 24
0

Okay so if it fits perfectly, i.e., remainder 0, then you just do division. And if it doesn't fit perfectly, i.e., remainder is positive, you need one more group.

Several ways to do this.

Using float division and rounding up:

num_people = 11
group_size = 3

num_groups = int(num_people / group_size + 0.5)

Using the sign of the remainder

import math

num_people = 11
group_size = 3

num_groups = (num_people // group_size) + math.sign(num_people % group_size)
cadolphs
  • 9,014
  • 1
  • 24
  • 41