DISCLAIMER - I am a beginner but have some experience in coding
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 store a file of a given size?
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = ___
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = ___
# 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 ___
return ___
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
CodePudding user response:
Here is the solution to your problem:
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
[EDIT]:
Explained the Code:
In the first step we basically used floor division to see how many blocks are fully occupied and then used modulo operator to see if there is a remainder!
Then finally we used the if logic to see if remainder is greater (then we will need another block) and if so we take the fully occupied blocks and added one more block to them!
CodePudding user response:
Use floor division to calculate how many blocks are fully occupied
The floor()
function will take a number and floor it
Example input:
- The floor of
12.24
will be12
- The floor of
26.999
will be26
- With
filesize = 1
,filesize / block_size
would equal to0.000244140625
and the floor of it would be0
. - With
filesize = 6000
,filesize / block_size
would equal to1.46484375
and the floor of it would be1
.
full_blocks = floor(filesize / block_size)
full_blocks
will be the number of blocks that filesize
would have filled.
Use the modulo operator to check whether there's any remainder
The modulo operator %
will calculate the remainder of a division operation.
Example input:
10 % 5
would be0
12 % 5
would be2
293 % 32
will be5
- With
filesize = 1
,filesize % block_size
would be1
- With
filesize = 4096
,filesize % block_size
would be0
- With
filesize = 6000
,filesize % block_size
would be1904
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.
As we've calculated above, if filesize
is a multiplier of block_size
(e.g 4096, 8192, 12288,...) then partial_block_remainder
would be 0
, and there are no remaining bytes.
And the remaining part is just simple math.
if partial_block_remainder > 0:
padding_size = block_size - partial_block_remainder
return filesize padding_size
return filesize
Conclusion
A quick look at your code, I've noticed that it is meaningless to calculate full_blocks
as we don't need it, so your code can be simplified into this:
def calculate_storage(filesize):
block_size = 4096
# 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:
padding_size = block_size - partial_block_remainder
return filesize padding_size
return filesize
But if you still insist on utilizing full_blocks
, here's the code:
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = floor(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 full_blocks * block_size block_size
return full_blocks * block_size