Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #ES7 Code Golf Progression
  2. This time, I have a program that takes numerical expressions with the parentheses, exponentiation, multiplication, division, addition, and subtraction and outputs each successive step used to solve the expression using order of operations. Example inputs:
  3. ```
  4. 1+1
  5. (51*5)/8
  6. 63/(91*(5+779))
  7. ```
  8. Spaces are the only types of whitespace allowed.
  9.  
  10. Before I start, I almost always think about the algorithm I will use to solve the problem:
  11.  
  12. - Take an input using the browser prompt, cleaning out whitespace.
  13. - If there are parentheses, jump to the innermost nested parentheses and begin evaluation.
  14. - Recursively replace (in order) exponents, multiplication/division, and addition/subtraction in the form of "[number][operation][number]=[result]".
  15. - Replace the innermost parentheses with the result from step 3 and go back to step 2 unless the result is only a number.
  16. - Of course, this algorithm is subject to change, but it should be enough to get going.
  17.  
  18. ##INITIAL PROGRAM
  19. ```js
  20. i=c=prompt().replace(/\s/g,'')
  21. R=(t,r)=>{
  22. T=t.replace(r,a=>(alert(a+`=`+eval(a)),eval(a)))
  23. return t!=T?R(T,r):t
  24. }
  25. while(isNaN(i)){
  26. z=(x=c.match(/\(([^()]+)\)/))?(c=x[1]):0
  27. c=R(R(R(c,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/)
  28. i=c=z?i.replace(/\(([^()]+)\)/,c):c
  29. }
  30. ```
  31. I always shorten the variables down before code-golfing to save time and effort. If you have trouble keeping track of the variables, just comment what they do:
  32. ```js
  33. // R => recursive replace & output function
  34. // i => input (will become output)
  35. // c => copy of i, used to evaluate innermost parentheses
  36. // z => checks if i has any parentheses left
  37. ```
  38. I also did some light golfing in my initial program to save time later on.
  39.  
  40. ##FIRST GOLFS
  41.  
  42. At this point, I'll do some easy saves.
  43.  
  44. Lambdas can return the last statement, which means this:
  45. ```js
  46. R=(t,r)=>(T=t.replace(r,a=>(alert(a+`=`+eval(a)),eval(a))),t!=T?R(T,r):t)
  47. ```
  48. Some rearrangements make the lambda even shorter:
  49. ```js
  50. R=(t,r,T=t.replace(r,a=>(alert(a+`=`+eval(a)),eval(a))))=>t!=T?R(T,r):t
  51. ```
  52. Aliasing is useful (both for "replace" and the parentheses regex):
  53. ```js
  54. i=c=prompt()[b='replace'](/\s/g,'')
  55. R=(t,r,T=t[b](r,a=>(alert(a+`=`+eval(a)),eval(a))))=>t!=T?R(T,r):t
  56. while(isNaN(i)){
  57. z=(x=c.match(p=/\(([^()]+)\)/))?(c=x[1],1):0
  58. c=R(R(R(c,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/)
  59. i=c=z?i[b](p=/\(([^()]+)\)/,c):c
  60. }
  61. ```
  62. isNaN(i) can be shortened in this case:
  63. ```js
  64. while(!+i){...}
  65. ```
  66. ##BIG GOLF #1
  67.  
  68. At this point, I see a possible big save in byte count.
  69. ```js
  70. b='replace'
  71. i=`(${prompt()[b](/\s/g,'')})`
  72. R=(t,r,o=a=>(alert(a+`=`+eval(a)),eval(a)),T=t[b](r,o))=>t!=T?R(T,r,o):t
  73. R(i,/\(([^()]+)\)/,(X,Y)=>R(R(R(Y,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/))
  74. ```
  75. Changes made:
  76.  
  77. - Completely took out the while loop, using the recursive replace to parse the parentheses.
  78. - Modified R to also have a function argument (which defaults to alerting the expression and result).
  79. - Wrapped the input in parentheses before passing it to R to evaluate the last expression correctly.
  80.  
  81. ##FINAL GOLFS
  82.  
  83. Aliasing "replace" now costs instead of saves bytes, so I'll take that out:
  84. ```js
  85. i=`(${prompt().replace(/\s/g,'')})`
  86. R=(t,r,o=a=>(alert(a+`=`+eval(a)),eval(a)),T=t.replace(r,o))=>t!=T?R(T,r,o):t
  87. R(i,/\(([^()]+)\)/,(X,Y)=>R(R(R(Y,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/))
  88. ```
  89. Since i is only used one other time, I'll just inline it:
  90. ```js
  91. R=(t,r,o=a=>(alert(a+`=`+eval(a)),eval(a)),T=t.replace(r,o))=>t!=T?R(T,r,o):t
  92. R(`(${prompt().replace(/\s/g,'')})`,/\(([^()]+)\)/,(X,Y)=>R(R(R(Y,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/))
  93. ```
  94. I can save a few bytes using a lambda rather than prompt for input:
  95. ```js
  96. I=>(R=(t,r,o=a=>(alert(a+`=`+eval(a)),eval(a)),T=t.replace(r,o))=>t!=T?R(T,r,o):t,R(`(${I.replace(/\s/g,'')})`,/\(([^()]+)\)/,(X,Y)=>R(R(R(Y,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/)))
  97. ```
  98. I can also replace the second replace with R:
  99. ```js
  100. I=>(R=(t,r,o=a=>(alert(a+`=`+eval(a)),eval(a)),T=t.replace(r,o))=>t!=T?R(T,r,o):t,R(`(${R(I,' ','')})`,/\(([^()]+)\)/,(X,Y)=>R(R(R(Y,/\d+\*\*\d+/),/\d+[*\/]\d+/),/\d+[+-]\d+/)))
  101. ```
  102. And that's the final, fully-golfed program at 177 bytes, much shorter than the ~270-byte initial program.
  103.  
  104. Annotated:
  105. ```js
  106. I=>(
  107. R= // recursive replace/expression output
  108. (t, // string to be modified
  109. r, // regex
  110. o=a=>(alert(a+`=`+eval(a)),eval(a)), // replacement function
  111. // defaults to expression output + return eval'd result
  112. T=t.replace(r,o) // the crucial replacement part
  113. )=>t!=T?R(T,r,o):t, // keep replacing t until it has no replacements left
  114. R(`(${R(I,' ','')})`, // take the input, strip all spaces, and wrap in parentheses
  115. /\(([^()]+)\)/, // evaluate expressions in parentheses inside-out
  116. (X,Y)=>R(R(R(Y, // replace innermost parentheses with result of:
  117. /\d+\*\*\d+/), // eval **
  118. /\d+[*\/]\d+/), // eval * /
  119. /\d+[+-]\d+/) // eval + -
  120. )
  121. )
  122. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement