Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Python 3, 141

I know I am to late, but I wanted to take the opportunity to post a shorter Python-version (and my first CodeGolf-attempt). :)

v=0
m=lambda y:(0,y)[-1!=y!=256]
i=x='+1'
d='-1'
s=k='*v'
c=o=');print(v'
while 1:
 for n in input('>>'):exec('v=m(v'+locals().get(n,'')+')')

The print-statement was kinda tricky for this. If the prompt has to end with a whitespace, add one char to the count. :)

Explanation

v is the accumulator.

m checks whether the given value is -1 or 256. If so, 0 will be returned, the value otherwise.

In the following lines the operations are assigned to the corresponding variables (as some have the same meaning (like i and x) this is shorter than instantiating a new dictionary). Those are then used in the exec below.

while 1: is the main-loop

Now the fun begins. Like @jazzpi@jazzpi's solution, it iterates over each char of the input. locals() is the dictionary of all current (visible) variables. With .get(n,'') the corresponding key will be put into the exec-string (an empty string, if the key (= other input) was not found). This then will be, when executed, concatenated with v and passed towards m. The return value will be stored in v again.

Short example:

Be n = 'i' (n = input-char), we get '+1' out of the locals-block as i is the variable with value '+1'.
The string for the exec than looks like this: 'v=m(v+1)'.
Maybe now it is easier to see, that, when executing, it will call m with the value of v+1 and store it's output in v again.

Repeat this until you are bored. :)

Python 3, 141

I know I am to late, but I wanted to take the opportunity to post a shorter Python-version (and my first CodeGolf-attempt). :)

v=0
m=lambda y:(0,y)[-1!=y!=256]
i=x='+1'
d='-1'
s=k='*v'
c=o=');print(v'
while 1:
 for n in input('>>'):exec('v=m(v'+locals().get(n,'')+')')

The print-statement was kinda tricky for this. If the prompt has to end with a whitespace, add one char to the count. :)

Explanation

v is the accumulator.

m checks whether the given value is -1 or 256. If so, 0 will be returned, the value otherwise.

In the following lines the operations are assigned to the corresponding variables (as some have the same meaning (like i and x) this is shorter than instantiating a new dictionary). Those are then used in the exec below.

while 1: is the main-loop

Now the fun begins. Like @jazzpi's solution, it iterates over each char of the input. locals() is the dictionary of all current (visible) variables. With .get(n,'') the corresponding key will be put into the exec-string (an empty string, if the key (= other input) was not found). This then will be, when executed, concatenated with v and passed towards m. The return value will be stored in v again.

Short example:

Be n = 'i' (n = input-char), we get '+1' out of the locals-block as i is the variable with value '+1'.
The string for the exec than looks like this: 'v=m(v+1)'.
Maybe now it is easier to see, that, when executing, it will call m with the value of v+1 and store it's output in v again.

Repeat this until you are bored. :)

Python 3, 141

I know I am to late, but I wanted to take the opportunity to post a shorter Python-version (and my first CodeGolf-attempt). :)

v=0
m=lambda y:(0,y)[-1!=y!=256]
i=x='+1'
d='-1'
s=k='*v'
c=o=');print(v'
while 1:
 for n in input('>>'):exec('v=m(v'+locals().get(n,'')+')')

The print-statement was kinda tricky for this. If the prompt has to end with a whitespace, add one char to the count. :)

Explanation

v is the accumulator.

m checks whether the given value is -1 or 256. If so, 0 will be returned, the value otherwise.

In the following lines the operations are assigned to the corresponding variables (as some have the same meaning (like i and x) this is shorter than instantiating a new dictionary). Those are then used in the exec below.

while 1: is the main-loop

Now the fun begins. Like @jazzpi's solution, it iterates over each char of the input. locals() is the dictionary of all current (visible) variables. With .get(n,'') the corresponding key will be put into the exec-string (an empty string, if the key (= other input) was not found). This then will be, when executed, concatenated with v and passed towards m. The return value will be stored in v again.

Short example:

Be n = 'i' (n = input-char), we get '+1' out of the locals-block as i is the variable with value '+1'.
The string for the exec than looks like this: 'v=m(v+1)'.
Maybe now it is easier to see, that, when executing, it will call m with the value of v+1 and store it's output in v again.

Repeat this until you are bored. :)

Source Link
Dave J
  • 229
  • 3
  • 6

Python 3, 141

I know I am to late, but I wanted to take the opportunity to post a shorter Python-version (and my first CodeGolf-attempt). :)

v=0
m=lambda y:(0,y)[-1!=y!=256]
i=x='+1'
d='-1'
s=k='*v'
c=o=');print(v'
while 1:
 for n in input('>>'):exec('v=m(v'+locals().get(n,'')+')')

The print-statement was kinda tricky for this. If the prompt has to end with a whitespace, add one char to the count. :)

Explanation

v is the accumulator.

m checks whether the given value is -1 or 256. If so, 0 will be returned, the value otherwise.

In the following lines the operations are assigned to the corresponding variables (as some have the same meaning (like i and x) this is shorter than instantiating a new dictionary). Those are then used in the exec below.

while 1: is the main-loop

Now the fun begins. Like @jazzpi's solution, it iterates over each char of the input. locals() is the dictionary of all current (visible) variables. With .get(n,'') the corresponding key will be put into the exec-string (an empty string, if the key (= other input) was not found). This then will be, when executed, concatenated with v and passed towards m. The return value will be stored in v again.

Short example:

Be n = 'i' (n = input-char), we get '+1' out of the locals-block as i is the variable with value '+1'.
The string for the exec than looks like this: 'v=m(v+1)'.
Maybe now it is easier to see, that, when executing, it will call m with the value of v+1 and store it's output in v again.

Repeat this until you are bored. :)