Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

2 of 6
added 494 characters in body
Miriam
  • 843
  • 7
  • 19

Python 3, 80 bytes

def f(x,g={}):
 for s in x:
 k=s.pop(-1)
 for n in s:g[n]=g.get(k,k)
 return g

Try it online!

Takes input as a list of lists of terms, returns a dict of variable name to value.

Original answer, 54 bytes

def f(x):g={};exec(x,g);del g['__builtins__'];return g

Try it online!

Takes input as a newline separated list of statements (as in the initial example in the question), returns a dict of variable name to value.

Assumes that neither __builtins__ nor any python keyword will be the name of a variable, so invalid. Also fails for variables with no assignment as pointed out by @caird coinheringaahing.

Miriam
  • 843
  • 7
  • 19

AltStyle によって変換されたページ (->オリジナル) /