Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

*Python Question* A particular data compression algorithm can reduce a file\'s s

ID: 3590310 • Letter: #

Question

*Python Question*

A particular data compression algorithm can reduce a file's size to 80 percent of its original size (so, for example, a 100-byte file will compress to 80 bytes). Before the compressed file can be stored on disk, it must be divided into blocks of 512 bytes each (so a 1000-byte compressed file will require two 512-byte blocks for storage). Write a program that does the following:
1. Prompt for and read in the number of empty/available blocks on disk (an integer value)
2. While the number of empty blocks is greater than 0:
a. Print the number of free blocks available
b. Prompt for and read in the size of an uncompressed file, in bytes (an integer value)
c. Calculate the size in bytes of the file when it is compressed (to keep the compressed size an integer, first multiply the original size by 8 and then divide the product by 10)
d. Print out the size of the compressed file, in bytes
e. Calculate the number of blocks required to hold the compressed file. Start by dividing the compressed size by 512 (using floor division); the quotient is the starting number of blocks. If the remainder of the division is greater than 0, add 1 more block to the total required.
f. Print out the number of blocks required
g. If the number of blocks needed is greater than the number of free blocks:
    i. Print out a message reporting that not enough blocks are available
    ii. Set the total number of free blocks to 0 (this will cause your loop to end)
h. Otherwise:
    i. Print a message indicating that the file has been saved
    ii. Subtract the number of blocks required from the number of free blocks
    iii. Add 1 to the count of files that were successfully "saved"
i. Update the total number of blocks used on disk

3. When your program ends, print out the number of files that were saved, and how many blocks were
used in all to save those files.

Sample Program Execution (program output is shown in italics, and user input is shown in boldface)
Enter number of free blocks: 1000
There are 1000 block(s) available
Enter file size in bytes: 75000
Compressed size: 60000 byte(s)
118 blocks of disk space will be needed
File has been saved
There are 882 block(s) available
Enter file size in bytes: 34000
Compressed size: 27200 byte(s)
54 blocks of disk space will be needed
File has been saved
There are 828 block(s) available
Enter file size in bytes: 100000
Compressed size: 80000 byte(s)
157 blocks of disk space will be needed
File has been saved
There are 671 block(s) available
Enter file size in bytes: 5200
Compressed size: 4160 byte(s)
9 blocks of disk space will be needed
File has been saved
There are 662 block(s) available
Enter file size in bytes: 50000
Compressed size: 40000 byte(s)
79 blocks of disk space will be needed
File has been saved
There are 583 block(s) available
etc.

Explanation / Answer

import math
saving=0
success=0
freeblocks=input("Enter number of free blocks:")
while(freeblocks>0):
print "There are", freeblocks," block(s) available"
uncompfile=input("Enter file size in bytes:")
if(uncompfile<0):
break
compressed=uncompfile*5/10
print "Compressed size:", compressed," byte(s)"
blockrequ=math.ceil((compressed/512)+0.5)
print blockrequ," blocks of disk space will be needed"if(freeblocks<blockrequ):
freeblocks=0
print "Not enough blocks are available"
else:
freeblocks=freeblocks-blockrequ
print "File has been saved"
saving=saving+uncompfile-compressed;
success=success+1
print "Total files saved",success
print "Total saving",saving