Disculpa, pero esta entrada está disponible sólo en Inglés Estadounidense. For the sake of viewer convenience, the content is shown below in the alternative language. You may click the link to switch the active language.

The homework of the first week of Learning Machines consisted in implementing a process called Run-length encoding in Python. This is a simple compressing algorithm that counts in sequential order the number of repetitions of a datum (in this case, character), in order to store the information as pairs of (count, datum).

Here is the file with my code and here is the Github repository I’ll be using for the homeworks.

# Learning Machines - Fall 2017
# Homework 1
# Sejo Vega-Cebrián
# Implement run-length encoding in Python
# ---
# FUNCTIONS
# Run-length encoding
def encode(data):
    resultList = [] # Start with an empty list
    prevChar = data[0] # Get the first character
    count = 1 # Count it once
    for i in range(1,len(data)): # Iterate though all the characters
        char = data[i] # Get current character
        if char != prevChar: # If there's a new character...
            resultList.append( (count, prevChar) ) # Store it
            prevChar = char # Use it as the "previous character"
            count = 1 # Restart the count
        else: # Otherwise increment count
            count += 1
    # After the loop, store the results of the last character
    resultList.append( (count, prevChar) )
    return resultList # Return the result

# corresponding decoding
def decode(dataList):
    result = '' # Start with an empty string
    for t in dataList: # For each pair in the list
        count = t[0] # Get the number of repetitions
        char = t[1] # Get the character to write
        for i in range(0,count): # For the given number of repetitions
            result += char # Add the character to the string
    return result # Return the result
# ---
# SCRIPT
# Set some data
originalData = "aaabbbbbaaabbbc"
# Encode it and print the resulting list
encoded = encode(originalData)
print(encoded)
# Decode the list and print the resulting string
decoded = decode(encoded)
print(decoded)

This exercise was useful to return to Python.