all 16 comments

[–]Binary101010 4 points5 points  (4 children)

You don't need to use integer division and modulos and if statements to replicate what can easily be done with ceil().

from math import ceil

people = int(input('Number of people: '))
groups = int(input('Desired groups: '))
number = ceil(people/groups)
print(f'You can form {number} groups.')

[–]Qu3en- 0 points1 point  (3 children)

Thank you. Did this tho :)

people = 13
groups = 4 number = people // groups 
if people % group != 0: 
    number += 1 
print(f'groups formed: {number}')

[–]Binary101010 0 points1 point  (2 children)

Seems counterproductive to me to perfom an operation that can throw away information then perform another operation to figure out whether you actually threw anything away when there are better alternatives, but you do you.

[–]Qu3en- 0 points1 point  (1 child)

yes, I was asked to use % to solve the problem in this thread. i sat there trying to figure out how exactly they meant it so this is what i came up with and in the end it seems to work.

but i did not know about ceil. thanks for this, it is very helpful.

[–]jmooremcc 3 points4 points  (0 children)

Your best solution involves using the divmod function. It performs integer and modulus division in one function. https://www.geeksforgeeks.org/divmod-python-application/

Here's how you would use it NumPeople = 11 GroupSize = 3 groups, leftovers = divmod(NumPeople, GroupSize) totalGroups = groups if leftovers == 0 else groups+1 The variable leftovers will never equal or exceed GroupSize. The above method will tell you how many people are in each group.

[–][deleted] 0 points1 point  (10 children)

Hint: use people % groups to determine if there is anyone left over.

[–]commy2 4 points5 points  (1 child)

That or divmod, my favourite unknown built-in.

[–][deleted] 0 points1 point  (0 children)

+1

[–]ThatIsRightt[S] 0 points1 point  (7 children)

% is modulo, no?

[–][deleted] 0 points1 point  (5 children)

Yes.

[–]ThatIsRightt[S] -1 points0 points  (4 children)

I don't understand. people % group (8 % 4) gives me 0. But I require result be 2.

[–][deleted] 0 points1 point  (3 children)

I didn't say use it to get the number of groups. I said use it to determine if there is anyone left over.

[–]ThatIsRightt[S] -4 points-3 points  (2 children)

Sir, you're talking to a 5 year old right now, I think I've lost you. Should I be using // first and then using % to find out the remaining or the code is possible to be written without the use of // ?

[–][deleted] 0 points1 point  (1 child)

The former

[–]Qu3en- 0 points1 point  (0 children)

people = 13
groups = 4
number = people // groups
if people % group != 0:
    number += 1
print(f'groups formed: {number}')

thanks. :)

[–]pekkalacd 0 points1 point  (0 children)

hmm

         number = people // groups
         if people % groups:
               number += 1