Wednesday, June 26, 2013

import copy

# Print all possible encodings of a positive integer numeric value.
# The encodings are defined as follows: 11 -> aa or ord(96 + 11) -> k.

def print_encodings(permuted_list):
    string_value = ""
    for element in permuted_list:
        string_value += chr(96 + int(element))
    print string_value

def encoding_permutation(input, idx, permuted_list):
    if (len(input) == idx):
        print_encodings(permuted_list)
        return
    if len(input) - idx >= 2:
        one_gram_list = copy.deepcopy(permuted_list)
        one_gram_list.append(input[idx])
        bi_gram_list =  copy.deepcopy(permuted_list)
        bi_gram_list.append(input[idx] + input[idx + 1])
        encoding_permutation(input, idx + 1, one_gram_list)
        encoding_permutation(input, idx + 2, bi_gram_list)
    else:
        permuted_list.append(input[idx])
        encoding_permutation(input, idx + 1, permuted_list)

encoding_permutation("1111", 0, [])

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home