#๐Ÿ”’ it has modulo operator, however, not making sense to me.

237 messages ยท Page 1 of 1 (latest)

old maple
#

I know how to call the function. I believe that all my functions are defined. I am struggling with the output for this particualr question. It is not really making sense. even with the comments.

boreal summitBOT
#

@old maple

Python help channel opened

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.

old maple
#
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.

low canyon
#

!d divmod

boreal summitBOT
#

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)`.
old maple
#

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.

low canyon
#

Your inputs are ints right? Therefore result of divmod is // and %

old maple
#

yes! intergers and I am using // and %

#

full_blocks = 4097 // 4096 & partial_block_remainder = 4097 % 4096

low canyon
#

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?

old maple
#

based on where I am, we have yet to learn that so we have to use the functions they have provided :/

old maple
low canyon
fresh lagoon
#

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

old maple
#

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

low canyon
#

Great. That's because you're not returning anything, you're just printing.

old maple
#

and Im unsure what to put on the first return

low canyon
#

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?

old maple
#
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 ```
low canyon
#

Yes but what variable stores the number of full blocks for the given file size?

low canyon
#

How do you find out how many times 4096 goes into file size?

old maple
#

1 filesize = 4096 so 4096 / 4097

low canyon
#

No. File size could be any number.

#

It could be 193736482

old maple
#

ok! that makes sense

low canyon
#

How many times does 4096 divide that number?

old maple
#

so 4096 is not a constant

fresh lagoon
low canyon
#

4096 is the block size. It is a constant

fresh lagoon
#

u would have to do a round up division

low canyon
#

The question is: how many times does file size get divided by the block size?

fresh lagoon
#

my faveorite method is -(-x//y)

old maple
#

so 1 file with 1 byte will still use 4096 bytes of storage...what am I missing here?

fresh lagoon
#

!e
print(-(-2//3))
print(-(-2//2))

boreal summitBOT
low canyon
low canyon
#

If file size is 8192, how many times does 4096 divide it?

old maple
#

8192 // 4096 or vice versa?

low canyon
#

That's right (the first)

old maple
#

so 8192 // 4096

low canyon
#

Run that by itself: print(8192//4096)

#

!e print(8192//4096)

boreal summitBOT
old maple
# low canyon Run that by itself: print(8192//4096)

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

low canyon
#

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?

old maple
#

So it needs to be full_block = (filesize) // 4096

low canyon
#

Almost

old maple
#

I need to define filesize?

low canyon
#

This means that when you call "calculate_storage", you have to give it a parameter (an argument, to be precise): filesize.

fresh lagoon
low canyon
#

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?

old maple
#

full_block = filesize // block_size

old maple
old maple
#

ok, let me put that in and see what I get!

low canyon
#

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?

old maple
#

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

low canyon
old maple
#

just for this thread

low canyon
#

Yup, now... look at the if statement

#

Let's focus on the "else" part... where partial_block_remainder is 0

old maple
#

right! so I need to remove the else? bc I added that to the template

low canyon
#

The else is fine

old maple
#

ok!

low canyon
#

What do you think you should return when partial_block_remainder is 0?

old maple
#

It should return 0 because there is no file to take up space

low canyon
#

Are you sure?

old maple
#

based on the question even 1 filesize will take up 4096 bytes

low canyon
#

I said partial_block_remainder

#

Not full_blocks

old maple
#

I want to say it should return 0 because there is no remainder

#

for the partial-block_remainder output

low canyon
old maple
#

there are 4096 bytes being used

#

if there is only 1 full block

low canyon
#

Yes, so what should be returned from the function?

old maple
#

the partial_block_remainder should return the remainder for the full_blocks avaiable

low canyon
#

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?

old maple
#

it should also be 1?

low canyon
#

Read the question again, it tells you in first sentence.

old maple
low canyon
#

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?

old maple
#

4096?

low canyon
#

Yes

old maple
#

I want the output to tell me that I have 4096 bytes

low canyon
#

Exactly.

#

And, what if it's (filesize) 8192?

old maple
#

in this particular case because it has only 1 full block which equal 4096

low canyon
#

Yup

#

So, if partial_block_remainder is 0 (there's no partial block), what's the formula for the result?

old maple
#

if the filesize is 8192, then I will need to figure out the full_blocks avaible first.

low canyon
old maple
#

ok, so the # of full blocks is 2 with a remainder of 0 as well

#

bc there are 2 full blocks totalling (4096*2)

low canyon
#

yup, keep going

old maple
#

I would want to return how many bytes are being used for any file. So I would want partial_block_remainder*filesize ?

low canyon
#

But partial_block_remainder might be 0

old maple
#

so if its 0 it will run the else and give me a 1

low canyon
#

But you don't want 1.

old maple
#

If its 0 the file will still take up 4096

low canyon
#

I want you to replace the "return 1" with "return something"

#

You need to think through what that something is.

old maple
#

else partial_block _remainder < 0: print(full_block)

low canyon
#

Not print. Print just prints something. It doesn't "return" anything

#

And how can the remainder be <0?

old maple
#

to correct it: ```py
else partial_block _remainder < 0: return full_blocks

old maple
low canyon
#

And "else" doesn't have a condition. It's just "else:"

old maple
#

else: return full_blocks

low canyon
#

If the sky is blue, leave umbrella at home. Else: Grab my umbrella.

low canyon
old maple
#

it would return 1?

low canyon
#

And is that correct?

old maple
#

because anything divided by itself is 1

#

no because its not assisting me in figuring out what to do with equal to 0

low canyon
#

If the input is 4096, the output should be 4096, right?

#

Because it uses 1 full block, and 1 full block is 4096 bytes.

old maple
#

if the filesize is 4096 then yes the output should 4096!

low canyon
#

Great, so what should you return when full_blocks = 1, and num-available_blcosk = 0?

old maple
#

It would return 4096!

#

bc full_block*block_size = filesize

low canyon
#

Excellent, so there's your return statement

old maple
#

OH MY GOSH!!!!

#

Let me run this really quick

low canyon
#

to be clear: return full_blocks * block_size

old maple
#

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

low canyon
#

Show your code

#

I believe you only fixed one half of the if statement

old maple
#
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```
low canyon
#

Why "return 1"?

#

If there's no partial block remainder, what should be the result?

old maple
#

the filesize right?

#

so I just need to put filesize instead of 1

low canyon
#

(since filesize = full_blocks * block_size)

old maple
#

right! So I need else to retrun full_block*block_size just like the if statement?

#

because ultimately I want to return filesize

low canyon
#

But, you still have a problem when partial_block_remainder > 0

#

The result should not be full_blocks * block_size.

old maple
#

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)

low canyon
#

Given a 4097 byte file, what is full_blocks and partial_block_remainder ?

old maple
#

1 = full_blocks // 0 = partial_blocks_remainder

low canyon
#

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?

old maple
#

you need 2 buckets!

low canyon
#

Great. What if you have 5000 apples?

old maple
#

4097 divided by 5000 means 2 buckets still

low canyon
#

yup (you mean 4096, tho)

old maple
#

right eright!

low canyon
#

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?

old maple
#

ok, give me one moment about to eat something and ill be right back!

old maple
#

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

low canyon
#

If file size is 5000 what is remainder?

#

And what is result? Just think through this, use paper if you need to

old maple
#

5000-4097

#

or 4096 so, 4 apples remaining

low canyon
#

Great, and what result should it be?

old maple
#

filesize - block_size = the action for if

#

wait..

#

(filesize - block_size)+full_blocks

low canyon
#

Do the math, is that the answer you want?

old maple
#

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

low canyon
old maple
#

so partial_block_remainder+full_block

#

oh my goodness. partial block remainder is going to be in bytes

#

full_blocks is number of buckets

low canyon
#

That's right: if remainder > 0, how many buckets do you need?

old maple
#

you need however many partial_blocks_remainders there are added to current full_blocks to get the total number of "buckets"

low canyon
#

No.

#

If you have 3 apples left over, do you need to add three buckets?

old maple
#

no, if I have 3 apples, I need to add 1 bucket!

low canyon
#

Exactly

old maple
#

its 1 bucket for every 4096 apples

low canyon
#

And you can't have more than 4095 apples left, since otherwise you'd have a full bucket

old maple
#

so I feel like I understand that but what is not clicking for me in the equation??

low canyon
#

So, if remainder > 0, and you have 2 full buckets, how many buckets do you need?

old maple
#

You need 3 buckets!

#

but it cannot be over 4095 otherwise I need 2 more buckets

low canyon
#

Yes, or 'full_buckets+1'

low canyon
old maple
#

so I need to set another condition!

low canyon
#

No

#

Remainder is either >0 or 0

#

Remainder can't be >4095: do you know why?

old maple
#

is it because we already defined what a full_bucket it?

low canyon
#

Modulus. How could the remainder be larger than the divisor?

old maple
#

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!

low canyon
boreal summitBOT
#
Python help channel closed

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.