@tsimpson1379/

# Compression Algorithm

## Class for binary data with compression and decompression methods

Files
• main.py
• compression.py
main.py
```1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
```
```import random, replit
from compression import b

# Setup
replit.clear()
print("Welcome to this demonstration of @tsimpson1379's compression module.\nYou can of course enter your own binary data,\nbut this program will randomly generate some for you.")
percentZero = 101
while not (100 >= percentZero >= 0):
try: percentZero = int(input('What percentage of zeros do you want in your data? (compression only makes file shorter with approx 87+) '))
except: print('Enter an integer between 0 and 100 inclusive!')
data = ''.join([str(int(not random.randint(1, 100) in list(range(0, percentZero+1)))) for n in range(random.randint(1000, 2000))])  # generates a string with percentZero% zeros
replit.clear()

# File de/compression
a = b(data, '1011010110101001')
origLen = len(a.data)
print('Original:\n'+str(a)+'\nBytes: '+str(origLen))
a.compress()
newLen = len(a.data)
print('\nCompressed:\n'+str(a) + '\nBytes: '+str(newLen)+ '\n% of original size: '+str(round((newLen/origLen)*1000)/10))
a.decompress()
print('\nDecompressed:\n'+str(a)+'\nBytes: '+ str(len(a.data)))
input('\nContinue to explanation...')
replit.clear()

# Explanation
a.compress()
a.explain()```