-1
\$\begingroup\$

In honor to the 1995 classic Ghost In The Shell (who's live action remake was released today) I propose a challenge where you create a shell with a ghost in it.

The challenge:

Create a REPL shell that evaluates mathematical statements and has a chance between (but not including) 0 and 100% of returning the wrong numerical answer for any given expression.

Your program or function must loop indefinitely and

  • Read a line from stdin
  • evaluate the expression (see requirements on evaluation)
  • print the result
  • repeat

Evaluation requirements

  • Whitespace is ignored
  • Only integers need to be supported (other types acceptable but not mandatory)
  • Leading zeros do not need to be supported
  • Evaluation may follow common order of operation or left to right evaluation
    • 5+5*5 may equal 30 or 50
  • Only addition, subtraction, multiplication, and division need to be supported
    • If floats are supported, division need not automatically cast to float (eg: python 2.7 division)
  • Parenthesis do not need to be supported
  • Statements such as python's eval() are allowed

Example implementation (python 3.6):

import random
while True:
    statement = input()
    if random.random() > .5:
        print(eval(statement))
    else:
        print(eval(statement)+1)

Shortest code in bytes wins

\$\endgroup\$
  • 6
    \$\begingroup\$ This is waaaay underspecified. You don't even mention order of operations. And it'll probably end up being pretty close to a duplicate of codegolf.stackexchange.com/q/568/31716 \$\endgroup\$ – DJMcMayhem Mar 31 '17 at 18:36
  • 3
    \$\begingroup\$ That "ask for clarification" is part of the question would suggest that this should be in the sandbox. \$\endgroup\$ – fəˈnɛtɪk Mar 31 '17 at 18:38
  • 2
    \$\begingroup\$ According to the spec I can just output 4 every time. There is a probability between 0% and 1% of the output being correct... Equal to the probability that the user enters an expression that evaluates to 4. \$\endgroup\$ – Level River St Mar 31 '17 at 21:51
  • 1
    \$\begingroup\$ I think you should mention that any given input should have the possibility of producing either a correct or incorrect output. This prevents things like print 4 which are against the spirit of the question \$\endgroup\$ – math junkie Mar 31 '17 at 22:32
  • 1
    \$\begingroup\$ @math_junkie you can always use random(0,2^32)... \$\endgroup\$ – fəˈnɛtɪk Mar 31 '17 at 22:53
3
\$\begingroup\$

Python 2, 36 34 33 bytes

while 1:n=input();print n|id(n)%7

Try it Online!

Make sure cache output is disabled for this to work properly.

\$\endgroup\$
  • \$\begingroup\$ I like the use of the id for randomness \$\endgroup\$ – Aaron Apr 1 '17 at 17:36
2
\$\begingroup\$

Javascript, 48 bytes

while(x=prompt())console.log(eval(x)+new Date%2)

while(x=prompt())console.log(eval(x)+new Date%2)

If you enter the inputs too quickly you can get the outputs in the wrong order...

\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 16 bytes

While 1
Input X
Disp X+(rand>.5
End

Explanation:

While 1         # 3 bytes, While true
Input X         # 3 bytes, Take user input, calculate value of expression, store in X
Disp X+(rand>.5 # 9 bytes, 50% chance of printing X, 50% chance of X+1
End             # 1 byte (no \n), End while loop
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.