Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Based on this closed question on stack overflow, what is the shortest way to stimulate scope for variables in an input. For simplicity, print is replaced with >.

copying critical information from the linked source:

[ and ] start and end a new scope.

Within scopes any changes to a variable outside of that scope is not permanent and is reverted when the scope has left, but the outer values are used to determine if a variable is undefined and its starting value.

Input will always start with [ and will end with matching ]
If its not defined then value should be 0
All input that is not a variable is a number with no decimal points. as in 10 or 582
> a means print the contents of a
a 10 assigns 10 to a
a b assigns the contents of b to a
Variables should be considered strings: one 1 is valid syntax
It is not required for the input to contain the print command >

Note on scoring:

You may chose how to input and output the "code", but it should be explained in the answer.

I answered this on the linked page in java, but I did not go for shortest code.
Comment any suggestions or questions, so I can improve my future posts. Shortest code by character wins. Good luck.

share|improve this question
What is meant by "All input that is not a variable is an integer"? And why mention Java, which is actually quite different? – Peter Taylor Feb 7 '13 at 8:17
The interesting thing is, > is a special variable that outputs its value when assigned to! – CalculatorFeline Mar 2 at 15:59
up vote 1 down vote accepted

Python 2, 165

def f(v):
 while 1:
    if'['in s:f(dict(v))
    elif']'in s:break
    elif'>'in s:print v.get(s[1],0)

Character count assumes tabs are used for the second level of indentation.

share|improve this answer
Nice solution. c=b in v and v[b]or 0 can be replaced with c=v.get(b,0). – primo Feb 7 '13 at 8:38
Thanks. Great suggestion; using .get helps a lot. – grc Feb 7 '13 at 9:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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