A
A
altertable2020-06-05 01:46:02
Python
altertable, 2020-06-05 01:46:02

Python 3 have a number to be divided into more or less equal parts?

for example, we have the number 10, we need to divide this number into more or less equal parts,
for example 10 into 3 parts (4,3,3)
but how to do this?
not using 10 // 3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-06-05
@vityareabko

def split_number(number, parts):
  d, r = divmod(number, parts)
  return [d + (1 if i < r else 0) for i in range(parts)]

print(split_number(22, 4))
# [6, 6, 5, 5]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question