Advertisement
bf17

BF long to shorthand

Mar 19th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. # python 3.4.3
  2. # convert BF code to shorthand BF by by replacing 3 or more commands with a decimal number and the command
  3. # in the form +++++ becomes 5+
  4. #             ]]] becomes 3]
  5.  
  6. prevch=""
  7. nextch=""
  8. counter=1
  9. total_count=0
  10. command_set="<>+-[],."
  11.  
  12. file = open(input(("bf file: "), 'r')
  13.  
  14. print()
  15.  
  16. while True:
  17.     nextch=file.read(1)
  18.  
  19.     if(not nextch):
  20.         file.close()
  21.         break
  22.  
  23.     if(nextch in command_set):
  24.  
  25.         total_count += 1
  26.  
  27.         if(nextch != prevch):
  28.             if(counter == 1):
  29.                 print(prevch, end="")
  30.             elif(counter == 2):
  31.                 print(prevch, end="")
  32.                 print(prevch, end="")
  33.             else:
  34.                 print(counter, end="")
  35.                 print(prevch, end="")
  36.             counter=1
  37.  
  38.         else:
  39.             counter+=1
  40.  
  41.         prevch = nextch
  42.  
  43. if(counter == 1):
  44.     print(prevch, end="")
  45. elif(counter == 2):
  46.     print(prevch, end="")
  47.     print(prevch, end="")
  48. else:
  49.     print(counter, end="")
  50.     print(prevch, end="")
  51.  
  52. print()
  53. print()
  54. print(total_count, "Commands")
  55.  
  56. """
  57. Sample input BF code, Hello World implementations from the Esoteric Programming Language Wiki
  58. ++++++++++[>+++++++>++++++++++>+++>+<
  59. <<<-]>++.>+.+++++++..+++.>++.<<++++++
  60. +++++++++.>.+++.------.--------.>+.>.
  61.  
  62. bf_l2s.py returns:
  63.  
  64. 10+[>7+>10+>3+>+4<-]>++.>+.7+..3+.>++.<<15+.>.3+.6-.8-.>+.>.
  65. 111 Commands
  66.  
  67. bf_s2l.py returns the original program (without formatting), and it runs too!
  68. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement