Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

please help me with this python beginner level problem. Please explain also

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading