#๐ it has modulo operator, however, not making sense to me.
237 messages ยท Page 1 of 1 (latest)
@old maple
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = 4097 // 4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = 4097 % 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 print("There is block bytes avaiable in this file")
else: return 1
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
Here is the question I am being asked: .
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 store a file of a given size?
Doing math with code, still trying to do this manually.
First, just fyi:
!d divmod
divmod(a, b)```
Take two (non\-complex) numbers as arguments and return a pair of numbers consisting of their quotient and remainder when using integer division. With mixed operand types, the rules for binary arithmetic operators apply. For integers, the result is the same as `(a // b, a % b)`. For floating\-point numbers the result is `(q, a % b)`, where *q* is usually `math.floor(a / b)` but may be 1 less than that. In any case `q * b + a % b` is very close to *a*, if `a % b` is non\-zero it has the same sign as *b*, and `0 <= abs(a % b) < abs(b)`.
ok so the floor division and modulo operator do the same thing the difference is floor division will give the answer to the problem to the nearest whole, the modulo will provide the remainder only.
Your inputs are ints right? Therefore result of divmod is // and %
yes! intergers and I am using // and %
full_blocks = 4097 // 4096 & partial_block_remainder = 4097 % 4096
Yes, I'm just saying you could've used divmod. Not a big deal, just an fyi.
Second: what's your question? What seems wrong?
based on where I am, we have yet to learn that so we have to use the functions they have provided :/
I need to calculate the storage function. I need to figure out the total number of bytes needed to store a file or any size
Ok, and what output are you seeing?
so the goal is to tell whats the smallest valuse for n so that
n is an integer
filesize < blocksize*n
than return n*blocksize
Based on the code before this is the output I see py There is block bytes avaiable in this file None There is block bytes avaiable in this file None There is block bytes avaiable in this file None There is block bytes avaiable in this file None
I should be getting numbers where it says none
Great. That's because you're not returning anything, you're just printing.
and Im unsure what to put on the first return
print returns None, so return print(...) returns None.
Let's start with full blocks. What variable stores the number of full blocks needed for the file size?
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = 4097 // 4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = 4097 % 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 calculate_storage(block_size)
else: return 1
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 ```
Yes but what variable stores the number of full blocks for the given file size?
1 block = 4096
filesize
File size is a parameter to this function. Thats the input
How do you find out how many times 4096 goes into file size?
1 filesize = 4096 so 4096 / 4097
ok! that makes sense
How many times does 4096 divide that number?
so 4096 is not a constant
no bc what if u got a file with the size of 4096*4097 bytes?
4096 is the block size. It is a constant
u would have to do a round up division
The question is: how many times does file size get divided by the block size?
my faveorite method is -(-x//y)
so 1 file with 1 byte will still use 4096 bytes of storage...what am I missing here?
!e
print(-(-2//3))
print(-(-2//2))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 1
Let's just focus on nice round numbers like 4096 and 8192
yes
If file size is 8192, how many times does 4096 divide it?
8192 // 4096 or vice versa?
That's right (the first)
so 8192 // 4096
:white_check_mark: Your 3.12 eval job has completed with return code 0.
2
here is what my code looks like now ```py
def calculate_storage(filesize):
block_size = 4096
# Use floor division to calculate how many blocks are fully occupied
full_blocks = 8192 // 4096
# Use the modulo operator to check whether there's any remainder
partial_block_remainder = 4097 % 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 print("There is block bytes avaiable in this file")
else: return 1
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```
So, going back to this code, how do you think you should change this line: full_block = ...
Let's focus on this line: full_blocks = 8192 // 4096
8192 shouldn't be 8192. Filesize is a parameter.
It could be anything.
So, instead of writing "8192", change that to "filesize"
Does that make sense?
So it needs to be full_block = (filesize) // 4096
Almost
I need to define filesize?
No, filesize is a parameter. See the first line: def calculate_storage(filesize):
This means that when you call "calculate_storage", you have to give it a parameter (an argument, to be precise): filesize.
u need to round up
i like to use -(-x//y)
That's not an option here. They're working from a template provided by the teacher.
Ok, now, 4096 is already defined as "block_size".
block_size = 4096
So, instead of typing 4096 again, what do you think you should do?
full_block = filesize // block_size
full_block = filesize // block_size ?? since I have already defined filesize and block size
Yes!
ok, let me put that in and see what I get!
Well, not yet... you're not ready to run it yet
Change partial_block_remainder = 4097 % 4096 next... you should know what to do, right?
so the code should look like this now: ```py
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 print("There is block bytes avaiable in this file")
else: return 1
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```
Yup, except those **'s aren't in the code, right?
no they are not! I bolded them
just for this thread
Yup, now... look at the if statement
Let's focus on the "else" part... where partial_block_remainder is 0
right! so I need to remove the else? bc I added that to the template
The else is fine
ok!
What do you think you should return when partial_block_remainder is 0?
It should return 0 because there is no file to take up space
Are you sure?
based on the question even 1 filesize will take up 4096 bytes
I want to say it should return 0 because there is no remainder
for the partial-block_remainder output
What if the # of full blocks is 1?
Yes, so what should be returned from the function?
the partial_block_remainder should return the remainder for the full_blocks avaiable
Let's back up a sec.
The function returns a single value.
If you input 4096, then what should be the return from the function?
it should also be 1?
Read the question again, it tells you in first sentence.
partial_block_remainder will be the output
Not at all.
If you input 4096, full_blocks will be 1 and partial_block_remainder is 0.
What should the output of the function be, if 4096 is the input?
4096?
Yes
I want the output to tell me that I have 4096 bytes
in this particular case because it has only 1 full block which equal 4096
Yup
So, if partial_block_remainder is 0 (there's no partial block), what's the formula for the result?
if the filesize is 8192, then I will need to figure out the full_blocks avaible first.
You already did this... you already calculate full_blocks and partial_block_remainder
ok, so the # of full blocks is 2 with a remainder of 0 as well
bc there are 2 full blocks totalling (4096*2)
yup, keep going
I would want to return how many bytes are being used for any file. So I would want partial_block_remainder*filesize ?
But partial_block_remainder might be 0
so if its 0 it will run the else and give me a 1
But you don't want 1.
If its 0 the file will still take up 4096
I want you to replace the "return 1" with "return something"
You need to think through what that something is.
else partial_block _remainder < 0: print(full_block)
Not print. Print just prints something. It doesn't "return" anything
And how can the remainder be <0?
to correct it: ```py
else partial_block _remainder < 0: return full_blocks
I would need to make it and = instead?
And "else" doesn't have a condition. It's just "else:"
else: return full_blocks
If the sky is blue, leave umbrella at home. Else: Grab my umbrella.
What would that return for 4096 filesize?
it would return 1?
And is that correct?
because anything divided by itself is 1
no because its not assisting me in figuring out what to do with equal to 0
If the input is 4096, the output should be 4096, right?
Because it uses 1 full block, and 1 full block is 4096 bytes.
if the filesize is 4096 then yes the output should 4096!
Great, so what should you return when full_blocks = 1, and num-available_blcosk = 0?
Excellent, so there's your return statement
to be clear: return full_blocks * block_size
I got this outputpy 0 1 4096 4096
It says in the template that I should have this: py 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
so my answers are the filesize....
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 full_blocks*block_size
else: return 1
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```
That's technically correct. So is: "full_blocks * block_size".
(since filesize = full_blocks * block_size)
right! So I need else to retrun full_block*block_size just like the if statement?
because ultimately I want to return filesize
Your choice, you could return either filesize or full_blocks*block_size.
But, you still have a problem when partial_block_remainder > 0
The result should not be full_blocks * block_size.
it should not be filesize returned. instead it should be a full blocks*filesize returned?
full_block // filesize?
gosh, this is getting a little confusing again because I am still pressed to find how they are calculating the storage space with just full_block and block_size
I want to return calculate_storage(filesize) which is calculate_storage(full_blocks*block_size)
Given a 4097 byte file, what is full_blocks and partial_block_remainder ?
1 = full_blocks // 0 = partial_blocks_remainder
What?
I think you need to take a step back and think about this:
You have a bucket that fits 4096 apples.
You have 4097 apples. How many buckets do you need?
you need 2 buckets!
Great. What if you have 5000 apples?
4097 divided by 5000 means 2 buckets still
yup (you mean 4096, tho)
right eright!
full_blocks is the number of full buckets.
partial_block_remainder tells you if you need an extra bucket for the spillover.
So, using that, if partial_block_remainder >0, how many buckets do you need?
ok, give me one moment about to eat something and ill be right back!
number of buckets needed is full_blocks + (filesize // block_size)
I will need to return the totla number of partial_blocks_remainder added to the current full_blocks
the equation for that is definitely throwing me off
I know I need to add block size mulipltied by the full_blocks. But how do I add partial_blocks_remainder
would I need (full_blocks*block_size)+partial_block_remainder
If file size is 5000 what is remainder?
And what is result? Just think through this, use paper if you need to
Great, and what result should it be?
filesize - block_size = the action for if
wait..
(filesize - block_size)+full_blocks
Do the math, is that the answer you want?
This is the partial_blocks_reminder + full blocks. giving me the total least number of "buckets" needed to have all the files in the storage.
let me try to run that code and show you my output
since I think its right
Read the second sentence of instructions. If there's even just 1 byte in remainder, then an entire block is added.
so partial_block_remainder+full_block
oh my goodness. partial block remainder is going to be in bytes
full_blocks is number of buckets
That's right: if remainder > 0, how many buckets do you need?
you need however many partial_blocks_remainders there are added to current full_blocks to get the total number of "buckets"
no, if I have 3 apples, I need to add 1 bucket!
Exactly
its 1 bucket for every 4096 apples
And you can't have more than 4095 apples left, since otherwise you'd have a full bucket
so I feel like I understand that but what is not clicking for me in the equation??
So, if remainder > 0, and you have 2 full buckets, how many buckets do you need?
Yes, or 'full_buckets+1'
Remainder can't be >4095.
so I need to set another condition!
is it because we already defined what a full_bucket it?
Modulus. How could the remainder be larger than the divisor?
so the code knows that it cannot go over the full_bucket amount
ohhhhhh
in this case the divisor is 4095
we still want a positive number! we cannot have a negative remainder
it means nothing is left
im going to go ahead and get ready for another study session. iI'll be back soon!
Going to let this simmer bc the mini food break definitely helped!
Ok, you basically have it now. If there's a remainder, you need full buckets + 1, otherwise you just need full buckets
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.