Created
March 20, 2021 10:26
-
-
Save AnisahTiaraPratiwi/f1bd07dd033ec9941fe68104c605a892 to your computer and use it in GitHub Desktop.
Question 5 If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_storage(filesize): | |
block_size = 4096 | |
# Use floor division to calculate how many blocks are fully occupied | |
full_blocks = filesize//4096 | |
# Use the modulo operator to check whether there's any remainder | |
partial_block_remainder = filesize%4096 | |
# Depending on whether there's a remainder or not, return | |
# the total number of bytes required to allocate enough blocks | |
# to store your data. | |
if partial_block_remainder > 0: | |
return 4096*(full_blocks+1) | |
return full_blocks*4096 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Sojiknight Thank you!