Run Length Encoding in Python

Overview of Python, File Processing, File Compression

You always hear programmers say that if you know 1 language in depth, the next one shouldn’t take very long to figure out.

Basics

one = 1
two = 2
some_number = 10000
# booleans
true_boolean = True
false_boolean = False
# string
my_name = "Leandro Tk"
# float
book_price = 15.80

Control Flow

if True:
print("Hello Python If")
if 2 > 1:
print("2 is greater than 1")
if 1 > 2:
print("1 is greater than 2")
else:
print("1 is not greater than 2")

Looping

loop_condition = Truewhile loop_condition:
print("Loop Condition keeps: %s" %(loop_condition))
loop_condition = False
for i in range(1, 11):
print(i)

Arrays, Lists, Collections

my_integers = [1, 2, 3, 4, 5]my_integers = [5, 7, 1, 3, 4]
print(my_integers[0]) # 5
print(my_integers[1]) # 7
print(my_integers[4]) # 4
relatives_names = [
"Toshiaki",
"Juliana",
"Yuji",
"Bruno",
"Kaio"
]
print(relatives_names[4]) # Kaiobookshelf = []
bookshelf.append("The Effective Engineer")
bookshelf.append("The 4 Hour Work Week")
print(bookshelf[0]) # The Effective Engineer
print(bookshelf[1]) # The 4 Hour Work Week

Dictionary

dictionary_example = {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
print("My name is %s" %(dictionary_tk["name"])) # My name is Leandro
print("But you can call me %s" %(dictionary_tk["nickname"])) # But you can call me Tk
print("And by the way I'm %s" %(dictionary_tk["nationality"])) # And by the way I'm Brazilian
dictionary_tk = {
"name": "Leandro",
"nickname": "Tk",
"nationality": "Brazilian"
}
dictionary_tk['age'] = 24print(dictionary_tk) # {'nationality': 'Brazilian', 'age': 24, 'nickname': 'Tk', 'name': 'Leandro'}

Loop Through Array

bookshelf = [
"The Effective Engineer",
"The 4 hours work week",
"Zero to One",
"Lean Startup",
"Hooked"
]
for book in bookshelf:
print(book)

Loop Through Dictionary

dictionary = { "some_key": "some_value" }for key, value in dictionary.items():
print("%s --> %s" %(key, value))
# some_key --> some_value

Class

class Vehicle:
pass
car = Vehicle()
print(car) # <__main__.Vehicle instance at 0x7fb1de6c2638>
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
tesla_model_s = Vehicle(4, 'electric', 5, 250)class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
def number_of_wheels(self):
return self.number_of_wheels
def set_number_of_wheels(self, number):
self.number_of_wheels = number
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity

@property
def number_of_wheels(self):
return self.__number_of_wheels

@number_of_wheels.setter
def number_of_wheels(self, number):
self.__number_of_wheels = number
class Vehicle:
def __init__(self, number_of_wheels, type_of_tank, seating_capacity, maximum_velocity):
self.number_of_wheels = number_of_wheels
self.type_of_tank = type_of_tank
self.seating_capacity = seating_capacity
self.maximum_velocity = maximum_velocity
def make_noise(self):
print('VRUUUUUUUM')
tesla_model_s = Vehicle(4, 'electric', 5, 250)
tesla_model_s.make_noise() # VRUUUUUUUM

Run Length Encoding

The basic idea behind this encoding/decoding, is the fact that when you have a uninterrupted series of 0000000s or 11111s, you can represent it as either:

Reading a File

First of all I needed a way to open files. Around here is where Python really starts to shine in my opinion. The basic programming syntax is all the same for variable declaration, if else statements, looping etc. Python’s file reading is so much simpler and than other languages and the difference gets bigger as the task increases.

# Read the entire file as a single string
with open('somefile.txt', 'rt') as f:
data = f.read()
# Iterate over the lines of the file
with open('somefile.txt', 'rt') as f:
for line in f:
# process line
...

Run Length Decoding

Given a series of numbers that alternate between 0s and 1s. Our code parses it into the full 0s and 1s. Basically decoding it.

167
240
-16
1
-12
1
-8
-5
-4
-5
0
1
-4
1
-10
1
-10
-35
0
-119
0
-33
0
-5
-6
1
-4
-5
-18
1
-72
1
-4
1
0
1
-4
1
0
1
-4
1
-6
-183
0
-7
0
-11
0
1
-4
1
-14
1
-72
1
-4
1
0
1
-4
1
0
1
-10
-197
-4
-7
-4
-7
-12
1
-74
1
-4
-5
-20
-183
0
-5
0
-7
0
1
0
1
-4
-5
0
-5
0
1
-6
1
-52
1
-20
-11
-10
-7
0
-203
-4
1
0

Code

I write about software && math. Occasionally I design && code. Find my stuff batsuuri.ca