Skip to content

Instantly share code, notes, and snippets.

@AnisahTiaraPratiwi
Created March 20, 2021 10:26
Show Gist options
  • Save AnisahTiaraPratiwi/f1bd07dd033ec9941fe68104c605a892 to your computer and use it in GitHub Desktop.
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…
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
@degaart
Copy link

degaart commented Oct 1, 2021

def calculate_storage(filesize):
    block_size = 4096

    # Use floor division to calculate how many blocks are fully occupied
    full_blocks = (filesize + block_size -  1) // block_size

    return full_blocks * block_size

assert calculate_storage(1) == 4096
assert calculate_storage(4096) == 4096
assert calculate_storage(4097) == 8192
assert calculate_storage(6000) == 8192


@Haroon-Atif
Copy link

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

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

@azizur-rahaman
Copy link

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+1)
return full_blocks*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

@KiraKami-dev
Copy link

I know this won't make much of a sense & its wrong method too
Can someone guide me?

`def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = block_size // filesize
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = full_blocks
# 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
return 8192

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`

@30nosha
Copy link

30nosha commented Jun 27, 2022

Who knows, what number should we assign to variable so that it could print(3)?

number = 10
if number > 11:
print(0)
elif number != 10:
print(1)
elif number >= 20 or number < 12:
print(2)
else:
print(3)

@MonalisaSaha24
Copy link

These above conditions don't seem to be perfect.

@30nosha
Copy link

30nosha commented Jul 8, 2022

These above conditions don't seem to be perfect.

These conditions above are from google certification course.

@arulebin
Copy link

arulebin commented Oct 3, 2022

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_sizefull_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

@Laiba-Akram
Copy link

Who knows, what number should we assign to variable so that it could print(3)?

number = 10 if number > 11: print(0) elif number != 10: print(1) elif number >= 20 or number < 12: print(2) else: print(3)

The above condition Solution is 2 because number is less then 12 match condition , The or operator returns True if either operand is true.

@analystden
Copy link

analystden commented Oct 5, 2022

def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = block_size // filesize
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = block_size % filesize
# 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 * 2
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

@SoulAffinis
Copy link

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

@KienVuTeam
Copy link

### 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`

Copy link

ghost commented Jan 11, 2023

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

@Sojiknight
Copy link

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

@tadeletekeba13
Copy link

@Sojiknight Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment