1

这里的想法是创建一个名为计算器的函数,该函数接受输入并将其与变量进行比较。将文件大小与块大小进行比较,如果有剩余,则将块大小加倍以适应使用情况。我的问题是,输入 1,我仍然得到 8192?然而,其他计算结果是正确的。我知道地板除法只产生一个整数,而不是一个浮点数(我认为这可能是一个更好的方法,因为我想要实数)但是当我尝试浮动它时,我得到了相同的回报。我也尝试过在其他地方浮动,结果与上述不正确或相同。当我颠倒模数的顺序时,答案似乎是正确的,但我被告知这是错误的,并被告知要更像您在此处看到的那样,按此顺序使用地板除法和模数。

所以我的问题是,为什么我得到输入为 1 的 8192,但其余的都是对的?

def calculator(somedata):                 # calculator function, 'somedata' is the input
    blocksize = 4096                      # what block size on the partition
                                          # calculate how many blocks are fully occupied
    fullblock = somedata // blocksize     # floor division will give back only the integer yes?
                                          # modulo to check whether there's any remainder
    bytesremain = somedata % blocksize    # which will say 1 or None unless I float yes?
                                          # return the block usage based on byte count?
    if bytesremain > 0:                   # if there's anything left over in the bytesremain
      return(blocksize * 2)               # double the block usage to 2
    return blocksize                      # otherwise return the block usage

print(calculator(1))                      # Should be 4096 
print(calculator(4096))                   # Should be 4096 
print(calculator(4097))                   # Should be 8192 
print(calculator(6000))                   # Should be 8192
4

10 回答 10

1

您可能正在寻找:

def calculator(somedata):
    blocksize = 4096
    fullblock = (somedata-1) // blocksize 
    return blocksize*(fullblock+1)
于 2020-02-16T23:11:50.140 回答
0
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 + 1) * block_size 
    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
  1. 我们使用地板除法在内存中保存记录,以查看需要多少块(4096 块)

  2. 然后我们检查在检查块大小时会留下的任何提醒块。这是因为虽然块大小为 1,但如果需要另外 0.5 个块大小,则一个块将无法伴随它。

  3. 最后我们创建了两个参数。第一个是如果部分块余数大于零且完整块大于或等于一,则块大小应加倍。

这里的计算思路是,由于要求是在存在 partial_block_remainder 的情况下添加额外的 4096 块大小,因此我们收到的每个落入 partial_block_remainder 的输出都需要再增加 4096。

如果我们担心 (calculate_storage(1)),因为 full_blocks 的输出为零,但 partial_block_remainder 可用,它将显示为 4096。

于 2021-05-13T21:45:07.343 回答
0
Something else to remember here
is the format of modulo arithmetic. The divisor is always to
the right of the modulo operator. E.g.- 2 = 6 % 4 <- This is
the divisor.   

 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 = 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
于 2020-08-27T21:44:27.540 回答
0
full_blocks = filesize // block_size 

partial_block_remainder = block_size % filesize
于 2020-07-26T18:58:49.007 回答
0
def calculate_storage(filesize):
    block_size = 4096
    full_blocks = filesize//block_size
    if filesize > block_size:
        return block_size*2
    return block_size

我认为这是文件大小所有情况的最佳解决方案!

于 2021-10-08T22:56:32.863 回答
0
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 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
于 2020-07-23T22:03:23.643 回答
0
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 + block_size
    return full_blocks * block_size
于 2021-07-26T11:11:09.207 回答
0
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*4096)+4096
    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
于 2020-06-22T12:19:55.220 回答
-1

我尝试了以下方法:

import math
def calculate_storage(filesize):
    block_size = 4096
    full_blocks = math.ceil(filesize/block_size)
    return full_blocks*block_size


print(calculate_storage(1))    
print(calculate_storage(4096)) 
print(calculate_storage(4097)) 
print(calculate_storage(9000)) 
于 2021-01-03T05:04:06.820 回答
-1
<pre><code>
def cstorage(fsize):
    bsize = 4096
    fblocks = fsize // bsize

    pbremainder = fsize % bsize

    if pbremainder > 0:
        return bsize*(fblocks+1)
    return bsize*fblocks

print(cstorage(1))  
print(cstorage(4096))
print(cstorage(4097)) 
print(cstorage(6000))
</pre></code>
于 2020-03-11T08:46:26.293 回答