Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint, shuffle
- def size_split(typ=0, size=10**5, spli=2, minn=1):
- """
- Splits a given size into unknown number of random positive integers, ensuring
- that each integer is at least `minn`. The sum of the integers will equal
- the initial size.
- Parameters:
- typ (int): Type 0 means constant spliting while type 1 means random.
- size (int): The total size to be split (default is 100,000).
- spli (int): The number of parts to split the size into (default is 2).
- minn (int): The minimum value each integer in the split must have (default is 1).
- Returns:
- list: A list of known number in type 0 or unknown number of positive integers in type 1 that sum up to the
- original size.
- Notes:
- - If the size is less than n, an empty list will be returned.
- - The output will be shuffled randomly.
- """
- if typ == 0:
- if size % spli:
- raise ValueError("spli should divide size.")
- return [size // spli] * spli
- else:
- arr = []
- tsize = size
- while size >= spli:
- x = randint(1, size // spli)
- arr.append(x)
- size -= x
- arr.sort(reverse=True)
- sumi = size
- while arr and arr[-1] <= minn:
- sumi += arr.pop()
- if sumi>=minn:
- arr.append(sumi)
- else:
- arr[-1] += sumi
- assert sum(arr) == tsize
- shuffle(arr)
- return arr
- # Example Usage
- if __name__ == "__main__":
- result1 = size_split(typ=0, size=10**5, spli=1, minn=10)
- print(result1)
- result2 = size_split(typ=1, size=1000, spli=100, minn=1)
- print(result2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement