encoding.py 1.73 KB
Newer Older
Sanjay Krishnan committed
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
'''ecoding.py provides utilities for compressing a file of data
using a 'delta' encoding (change over the previous) element.
'''
import struct

#TODO 1. Write a function that loads the input data 
#and returns a list of numbers
def load_orig_file(in_filename):
	'''load_orig_file takes an input file and returns a list of
	   numbers. The file is formatted with a single integer
	   number on each line
	'''

	raise ValueError('Not implemented')


#TODO 2. Write a function that performs the delta encoding
def delta_encoding(data):
	'''delta_encoding takes a list of integers and performs
	   a delta encoding represent each element as a difference
	   from the previous one. The first element is kept as is.

	   delta_encoding encoding returns a list where the first
	   element is the original value and all the rest of the
	   elements are deltas from the first value.
	'''

	raise ValueError('Not implemented')


#TODO 3. Apply a shift to all the elements so all the deltas are positive
def shift(data, offset):
	'''shift adds 'offset' to all of the elements to ensure that
	   every value in the delta encoding is positive.
	'''
	raise ValueError('Not implemented')

#GIVEN, should be obvious what it does
def unshift(data, offset):
	return shift(data,-offset)

#TODO 4. Convert the encoded data into a byte array and write
#to disk.
def write_encoding(data, out_file):
	raise ValueError('Not implemented')

#GIVEN, read encoded file
def read_encoding(out_file):
	f = open(out_file, 'rb')
	encoded = f.read()
	return struct.unpack("B"*len(encoded), encoded)


#TODO 5. Write a function that performs the delta encoding
def delta_decoding(data):
	'''delta_decoding takes a delta encoded list and return 
	   the original data.
	'''
	raise ValueError('Not implemented')