Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def calculate_storage(filesize):
- block_size = 4096
- # Use floor division to calculate how many blocks are fully occupied
- full_blocks = filesize // block_size
- if full_blocks == 0 and filesize != 0:
- full_blocks = 1
- # Use the modulo operator to check whether there's any remainder
- partial_block_remainder = (full_blocks * block_size) % filesize
- # Depending on whether there's a remainder or not, return the right number.
- if partial_block_remainder > 0:
- return calculate_storage(partial_block_remainder) + (full_blocks * block_size)
- return full_blocks * block_size
- print(calculate_storage(1)) # Should be 4096
- print(calculate_storage(4096)) # Should be 4096
- print(calculate_storage(4097)) # Should be 8192
- print(calculate_storage(6000)) # Should be 8192
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement