Skip to content

'allocation_content_length' #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions maths/allocation_content_length.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#-*- coding:utf-8 -*-


def allocation_content_length(content_length, portioning):
"""
>>> content_length = 16647
>>> portioning = 4
>>> shares = content_length // portioning

>>> f'0-{shares*1}'
'0-4161'
>>> f'{shares*1+1}-{shares*2}'
'4162-8322'
>>> f'{shares*2+1}-{shares*3}'
'8323-12483'
>>> f'{shares*3+1}-{shares*4}'
'12484-16647'
"""

shares = content_length // portioning

allocation_list = [f'0-{shares*1}']
for i in range(1,portioning-1):
length = f'{shares*i+1}-{shares*(i+1)}'
allocation_list.append(length)
allocation_list.append(f'{(shares*(portioning-1))+1}-{content_length}')

return allocation_list


def main():
the_list = allocation_content_length(16647,4)
print(the_list)


if __name__ == '__main__':
main()


52 changes: 52 additions & 0 deletions maths/allocation_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#-*- coding:utf-8 -*-


def allocation_num(num, x) -> int:
Copy link
Member

@cclauss cclauss Mar 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 4 says that the function returns an int but line 26 says that we expect a list of str. Line 26 is correct and line 4 is wrong.

$ mypy --ignore-missing-imports .

maths/allocation_number.py:51: error: Incompatible return value type (got "List[str]", expected "int")
Found 1 error in 1 file (checked 95 source files)
The command "mypy --ignore-missing-imports ." exited with 1.

"""
Divide the numbers into x parts.

parameter
------------
: param num: the number
: param x: number of copies

return
------------
: return: split completed list

example
------------
>>> allocation_num(16647, 4)
['0-4161', '4162-8322', '8323-12483', '12484-16647']

>>> allocation_num(888, 888)
Traceback (most recent call last):
...
ValueError: param x can't past or be equal to the param num!

>>> allocation_num(888, 111)
Traceback (most recent call last):
...
ValueError: param x can't past or be equal to the param num!
"""


if x >= num:
raise ValueError('param x can\'t past or be equal to the param num!')

shares = num // x

allocation_list = [f'0-{shares*1}']
for i in range(1,x-1):
length = f'{shares*i+1}-{shares*(i+1)}'
allocation_list.append(length)
allocation_list.append(f'{(shares*(x-1))+1}-{num}')

return allocation_list


if __name__ == '__main__':
the_list = allocation_num(888,888)
print(the_list)