-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.py
37 lines (27 loc) · 993 Bytes
/
encoder.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
29
30
31
32
33
34
35
36
37
import primes
import utils
def encode(sentence, hex_output):
sentence = " ".join(sentence.splitlines())
words = sentence.split(" ")
longest_word_length = 0
for word in words:
if len(word) > longest_word_length:
longest_word_length = len(word)
required_primes = primes.generate_primes(longest_word_length)
godel_words = []
for word in words:
godel_word = make_godel_word(word, required_primes)
if hex_output:
godel_word = format(godel_word, 'x')
godel_words.append(str(godel_word))
return "|".join(godel_words)
def make_godel_word(word, required_primes):
godel_number = 1
for index in range(len(word)):
char = word[index]
alphabet_index = get_alphabet_index(char)
godel_number = godel_number * (required_primes[index] ** alphabet_index)
return godel_number
def get_alphabet_index(char):
alphabet = utils.make_alphabet()
return alphabet.index(char) + 1