bf17 icon

BF long to shorthand

bf17 | PRO | 03/19/17 06:33:43 PM UTC | 0 ⭐ | 342 👁️ | Never ⏰ | []
Python |

1.32 KB

|

None

|

0 👍

/

0 👎

# python 3.4.3
# convert BF code to shorthand BF by by replacing 3 or more commands with a decimal number and the command
# in the form +++++ becomes 5+
#             ]]] becomes 3]
 
prevch=""
nextch=""
counter=1
total_count=0
command_set="<>+-[],."
 
file = open(input(("bf file: "), 'r')
 
print()
 
while True:
    nextch=file.read(1)
 
    if(not nextch):
        file.close()
        break
 
    if(nextch in command_set):
 
        total_count += 1
 
        if(nextch != prevch):
            if(counter == 1):
                print(prevch, end="")
            elif(counter == 2):
                print(prevch, end="")
                print(prevch, end="")
            else:
                print(counter, end="")
                print(prevch, end="")
            counter=1
 
        else:
            counter+=1
 
        prevch = nextch
 
if(counter == 1):
    print(prevch, end="")
elif(counter == 2):
    print(prevch, end="")
    print(prevch, end="")
else:
    print(counter, end="")
    print(prevch, end="")
 
print()
print()
print(total_count, "Commands")
 
"""
Sample input BF code, Hello World implementations from the Esoteric Programming Language Wiki
++++++++++[>+++++++>++++++++++>+++>+<
<<<-]>++.>+.+++++++..+++.>++.<<++++++
+++++++++.>.+++.------.--------.>+.>.
 
bf_l2s.py returns:
 
10+[>7+>10+>3+>+4<-]>++.>+.7+..3+.>++.<<15+.>.3+.6-.8-.>+.>.
111 Commands
 
bf_s2l.py returns the original program (without formatting), and it runs too!
"""

Comments