开发者

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.


  1. iterate over both lists simultaneously.
  2. 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"
  3. apply the assignment set on the two lists (replace each occurrence of the var with the value)
  4. if you reached the end of the lists with no conflicts, return the assignment set.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜