Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
1,390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. def calculate_storage(filesize):
  2.     block_size = 4096
  3.     # Use floor division to calculate how many blocks are fully occupied
  4.     full_blocks = filesize // block_size
  5.     if full_blocks == 0 and filesize != 0:
  6.       full_blocks = 1
  7.     # Use the modulo operator to check whether there's any remainder
  8.     partial_block_remainder =  (full_blocks * block_size) % filesize
  9.     # Depending on whether there's a remainder or not, return the right number.
  10.     if partial_block_remainder > 0:
  11.         return calculate_storage(partial_block_remainder) + (full_blocks * block_size)
  12.     return full_blocks * block_size
  13.  
  14. print(calculate_storage(1))    # Should be 4096
  15. print(calculate_storage(4096)) # Should be 4096
  16. print(calculate_storage(4097)) # Should be 8192
  17. print(calculate_storage(6000)) # Should be 8192
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement