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 Answer

[Python (削除) 3 (削除ここまで) 2]Python (削除) 3 (削除ここまで) 2, (削除) 80 (削除ここまで) (削除) 75 (削除ここまで) 69 bytes

-5 bytes thanks to @Sisyphus
-6 bytes thanks to @xnor

g={}
for s in input():
 k=s.pop()
 for n in s:g[n]=g.get(k,k)
print g

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

[Python (削除) 3 (削除ここまで) 2], (削除) 80 (削除ここまで) (削除) 75 (削除ここまで) 69 bytes

-5 bytes thanks to @Sisyphus
-6 bytes thanks to @xnor

g={}
for s in input():
 k=s.pop()
 for n in s:g[n]=g.get(k,k)
print g

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

Python (削除) 3 (削除ここまで) 2, (削除) 80 (削除ここまで) (削除) 75 (削除ここまで) 69 bytes

-5 bytes thanks to @Sisyphus
-6 bytes thanks to @xnor

g={}
for s in input():
 k=s.pop()
 for n in s:g[n]=g.get(k,k)
print g

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}
deleted 753 characters in body
Source Link
Miriam
  • 843
  • 7
  • 19

[Python (削除) 3 (削除ここまで) 2], (削除) 80 (削除ここまで) 75(削除) 75 (削除ここまで) 69 bytes

-5 bytes thanks to @Sisyphus by changing
-6 bytes thanks to Python 2 and a full program@xnor

x=input()
g={}
for s in xinput():
 k=s.pop(-1)
 for n in s:g[n]=g.get(k,k)
print g

Try it online! Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

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.

[Python (削除) 3 (削除ここまで) 2], (削除) 80 (削除ここまで) 75 bytes

-5 bytes thanks to @Sisyphus by changing to Python 2 and a full program

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

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

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.

[Python (削除) 3 (削除ここまで) 2], (削除) 80 (削除ここまで) (削除) 75 (削除ここまで) 69 bytes

-5 bytes thanks to @Sisyphus
-6 bytes thanks to @xnor

g={}
for s in input():
 k=s.pop()
 for n in s:g[n]=g.get(k,k)
print g

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}
added 101 characters in body
Source Link
Miriam
  • 843
  • 7
  • 19

Python 3 [Python (削除) 3 (削除ここまで) 2], 80(削除) 80 (削除ここまで) 75 bytes

-5 bytes thanks to @Sisyphus by changing to Python 2 and a full program

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

Try it online! Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

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.

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.

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

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.

[Python (削除) 3 (削除ここまで) 2], (削除) 80 (削除ここまで) 75 bytes

-5 bytes thanks to @Sisyphus by changing to Python 2 and a full program

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

Try it online!

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

Explanation

def f(x,g={}): # Save a few bytes by defining g as a default argument.
 for s in x:
 k=s.pop(-1) # Take the last term, which is the value we'll be using.
 for n in s: # For all *other* values:
 g[n]=g.get(k,k) # .get(k, k) means "get the value called k, if not found use k raw" (numbers will not be found)
 return g

Note that it never actually differentiates between numbers and variables, just trusts that the input won't try to assign to a number. This means you can actually use it to assign to a number - this input:

[9, 5],
['b', 9],
['c', 'a', 'b'],
['a', 2],
['b', 9]

Will result in this output:

{9: 5, 'b': 5, 'c': 5, 'a': 2}

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.

added 734 characters in body
Source Link
Miriam
  • 843
  • 7
  • 19
Loading
added 494 characters in body
Source Link
Miriam
  • 843
  • 7
  • 19
Loading
Post Undeleted by Miriam
Post Deleted by Miriam
Source Link
Miriam
  • 843
  • 7
  • 19
Loading

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