-
-
Save AnisahTiaraPratiwi/f1bd07dd033ec9941fe68104c605a892 to your computer and use it in GitHub Desktop.
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 |
### Don't make it too important!
my code:
`def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
#full_blocks = (filesize) // block_size
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = (filesize -1) // block_size
# 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 (block_size * (partial_block_remainder +1))
return 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`
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = (filesize) // block_size
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = (filesize -1) // block_size
# 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 8192
return 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
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = (filesize + block_size) // block_size
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = (filesize + block_size) % block_size
# 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 full_blocks * 4096
return 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
@Sojiknight Thank you!
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = filesize // block_size
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = filesize % block_size
# 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 block_size + (full_blocks * block_size)
return block_size*full_blocks
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