-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-to-πie.py
55 lines (51 loc) · 2.47 KB
/
text-to-πie.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from math import sqrt
from sys import stdin, stdout
#['e', ',', '[', ']', '0', '1', 'p', 'i']
class BFGenerator(object):
"""Takes a string and generates a brainfuck code that, when run,
prints the original string to the brainfuck interpreter standard
output"""
def text_to_brainfuck(self, data):
"""Converts a string into a BF program. Returns the BF code"""
glyphs = len(set([c for c in data]))
number_of_bins = max(max([ord(c) for c in data]) // glyphs,1)
# Create an array that emulates the BF memory array as if the
# code we are generating was being executed. Initialize the
# array by creating as many elements as different glyphs in
# the original string. Then each "bin" gets an initial value
# which is determined by the actual message.
# FIXME: I can see how this can become a problem for languages
# that don't use a phonetic alphabet, such as Chinese.
bins = [(i + 1) * number_of_bins for i in range(glyphs)]
code="p" * number_of_bins + "["
code+="".join(["1"+("p"*(i+1)) for i in range(1,glyphs)])
code+="0"*(glyphs-1) + "i]"
code+="p" * number_of_bins
# For each character in the original message, find the position
# that holds the value closest to the character's ordinal, then
# generate the BF code to move the memory pointer to that memory
# position, get the value of that memory position to be equal
# to the ordinal of the character and print it (i.e. print the
# character).
current_bin=0
for char in data:
new_bin=[abs(ord(char)-b)
for b in bins].index(min([abs(ord(char)-b)
for b in bins]))
appending_character=""
if new_bin-current_bin>0:
appending_character="1"
else:
appending_character="0"
code+=appending_character * abs(new_bin-current_bin)
if ord(char)-bins[new_bin]>0:
appending_character="p"
else:
appending_character="i"
code+=(appending_character * abs( ord(char)-bins[new_bin])) +"e"
current_bin=new_bin
bins[new_bin]=ord(char)
return code.replace("p","Π")
if __name__=="__main__":
bfg=BFGenerator()
print(bfg.text_to_brainfuck(input("Set text>")))