variable value only comparing this two list
( I cannot edit my old question I couldont reach edit tool) ,SO I will send it again Finding variable value in a list. Input (uppercase letter is variable name):
[ 1, [2, X], Y, [4, Y, X]]
[ 1, U, [3, U], [4, Z, 10]]
example output:
X = 10
U = [2, 10]
Y = [3, [2, 10]]
z = [3, [2, 10]]
How can we find variable value only comparing this two list? @user I want to evaluate each variable: Giv开发者_运维技巧en X = 10, U = [2, X] = [2, 10], Y = [3,U] = [3,[2,X]] = [3,[2,10], Z = Y. So each single variable in one list can be resolved with it corresponding "equation in the other list.
- iterate over both lists simultaneously.
- for every 2 items in the same position:
if they're both numbers make sure they're equale. if not, return false if one is a variable and the other is a value, add the pair to an "assignment set" - apply the assignment set on the two lists (replace each occurrence of the var with the value)
- if you reached the end of the lists with no conflicts, return the assignment set.
精彩评论