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

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Find the result of some assignment statements

In this challenge, the goal is to find the values of some variables after a number of assignments are done. An example input:

a = 5
b = 4
c = a = b
a = 2
b = a

This would result in:

a = 2
b = 2
c = 4

Each statement will be one of the following:

  • A variable name ([a-z_]+)
  • A numeric value ([0-9]+)
  • An assignment operation, with a variable name on the left and a statement on the right

You may assume that the input will be a list of statements, formatted however you want. Variable names will have differing lengths (if you need a hard value to gold within, assume 16 chars max).

Note that statements can contain more or less than one assignment (such as a, 23, or a = b = c = 4), and that variables can appear that are never assigned to. Assume no undefined variables are used as values in an assignment (such as a = undefined_variable), and that no variable will be on both sides of an assignment (such as a = a or a = a = 1).

You can take input any way you wish (such as a string with a character to delimit statements, a list formatted as [["a", 5], ["b", "a"]], etc.), and output can be in any consistent format (such as a hash map of names to values, or a list of values in the order that the variables first appeared).

Test cases:

a = 5 -> a = 5
b = 512, c = a = 2 -> a = 2, b = 512, c = 2
def, 2, e = 8, 101 -> e = 8
 -> 
a -> 
fgh = 4, i = 3, fgh = i -> fgh = 3, i = 3
j = k = l = m = n = 14 -> j = 14, k = 14, l = 14, m = 14, n = 14
s = t = u = 6, t = v = 7 -> s = 6, t = 7, u = 6, v = 7
o = 3, o = p -> [undefined]
q = r -> [undefined]
w = w = 2 -> [undefined]
x = 4, x = x -> [undefined]

This is , so shortest answer per language wins!

Answer*

Draft saved
Draft discarded
Cancel
8
  • \$\begingroup\$ your answer needs to work even when keywords are used as names. function = 3 fails: Try it online! \$\endgroup\$ Commented Sep 22, 2020 at 11:45
  • \$\begingroup\$ since this isn't actually an answer, I'd recommend deleting it before it accumulates any downvotes. You can edit in the corrected version and undelete it later! Feel free to come talk to us in the R chatroom for help! I think using a list to store the name-value pairs will be a good approach, though :-) \$\endgroup\$ Commented Sep 22, 2020 at 14:32
  • \$\begingroup\$ Undeleted now! Still using the function's local environment. \$\endgroup\$ Commented Sep 22, 2020 at 16:31
  • \$\begingroup\$ Nice! A couple of things: parse is perfectly fine taking a text vector and it will evaluate each element in turn. text= can be shortened to t=. And you don't need to reassign i, you can use the gsub directly in the parse, down to 145 bytes! \$\endgroup\$ Commented Sep 22, 2020 at 16:58
  • \$\begingroup\$ I do know there's an answer less than 100 bytes so keep trying interesting golfs! :-) \$\endgroup\$ Commented Sep 22, 2020 at 17:01

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