Skip to main content
added explanation
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

Japt, 15 bytes

OvUr" +%d"_rS'(

Test it online!

How it works

This challenge was practically made for Japt. I shall attempt to explain how:

Japt is basically a shortened version of JavaScript. In fact, after making some changes to the code ("transpiling" to JS), it is evaluated directly as JavaScript. What comes in handy here is that a space in Japt becomes ) in JavaScript (a feature I occasionally regret adding, as it makes it hard to write "ungolfed" Japt code), and missing parentheses are automatically added.

What this all means for this challenge is that we can obtain the correct result simply by changing all spaces after an operator (before a number) to left-parentheses:

5*6  +  7*8   -3  *5    Original string
5*6  +((7*8   -3  *5    Japt code that evaluates to the correct number

When transpiled to JS, the changes mentioned above are made, which results in this:

     5*6  +((7*8   -3  *5    Japt code
(((((5*6))+((7*8)))-3))*5    transpiled JS code

The JS code is then evaluated, which gives the expected result.

OvUr" +%d"_rS'(    Implicit: U = input string
  Ur" +%d"         Replace each match of / +\d/ in U with
          _rS'(      the match with each Space replaced with a '('.
Ov                 Evaluate the result as Japt.
                   Implicit: output result of last expression

Japt, 15 bytes

OvUr" +%d"_rS'(

Test it online!

Japt, 15 bytes

OvUr" +%d"_rS'(

Test it online!

How it works

This challenge was practically made for Japt. I shall attempt to explain how:

Japt is basically a shortened version of JavaScript. In fact, after making some changes to the code ("transpiling" to JS), it is evaluated directly as JavaScript. What comes in handy here is that a space in Japt becomes ) in JavaScript (a feature I occasionally regret adding, as it makes it hard to write "ungolfed" Japt code), and missing parentheses are automatically added.

What this all means for this challenge is that we can obtain the correct result simply by changing all spaces after an operator (before a number) to left-parentheses:

5*6  +  7*8   -3  *5    Original string
5*6  +((7*8   -3  *5    Japt code that evaluates to the correct number

When transpiled to JS, the changes mentioned above are made, which results in this:

     5*6  +((7*8   -3  *5    Japt code
(((((5*6))+((7*8)))-3))*5    transpiled JS code

The JS code is then evaluated, which gives the expected result.

OvUr" +%d"_rS'(    Implicit: U = input string
  Ur" +%d"         Replace each match of / +\d/ in U with
          _rS'(      the match with each Space replaced with a '('.
Ov                 Evaluate the result as Japt.
                   Implicit: output result of last expression
Source Link
ETHproductions
  • 50.1k
  • 6
  • 94
  • 240

Japt, 15 bytes

OvUr" +%d"_rS'(

Test it online!