一个用元组表示的链表 Python

huangapple go评论152阅读模式
英文:

A linked list as a tuple Python

问题

给定一个已排序的元组链表(以元组形式表示,其中元组包含一个数字和指向下一个元组的链接),需要实现一个反转链表的函数。下面是一个示例输入和输出:

输入:
x = (1, (3, (6, (8, None))))

输出:
(8, (6, (3, (1, None))))

以下是一个修改后的函数,可以正确反转链表:

  1. def reverse(linked_list: tuple):
  2. current_pair = linked_list
  3. reversed_list = None
  4. while current_pair:
  5. value, rest_of_list = current_pair
  6. new_pair = (value, reversed_list)
  7. reversed_list = new_pair
  8. current_pair = rest_of_list
  9. return reversed_list

使用这个函数,你可以将链表反转,得到正确的结果。

英文:

A connected list is given, which is implemented as a tuple (a number, link to the following pair) of the form in which the values are already sorted:
x = (1, (3, (4, (7, (9, None)
It is necessary to implement a function that reverses the list:
example of a call:
reverse((1, (3, (6, (8, None)))))
Result:
(8, (6, (3, (1, None))))

This is what i've done, i know it's incorrect cause first element would be doubled then

  1. def reverse(linked_list: tuple):
  2. last_pair = (linked_list[0], None)
  3. while linked_list[1]:
  4. new_list = (linked_list[0], last_pair)
  5. return new_list
  6. return reverse(linked_list[1])

This is the result:
(1, (1, None))

I have no idea how to do it in correct way, there is nothing about linked lists as tuples on the internet

答案1

得分: 0

在问题中的实现没有评估传递给 reverse() 的所有部分。

以下是实现你的目标的一种方式:

  1. def reverse(t):
  2. result = None
  3. while True:
  4. a, b = t
  5. result = a, result
  6. if (t := b) is None:
  7. break
  8. return result
  9. print(reverse((1, (3, (6, (8, None))))))
  10. **输出:**
  11. (8, (6, (3, (1, None))))
英文:

The implementation in the question does not evaluate all parts of the argument passed to reverse().

Here's one way to achieve your objective:

  1. def reverse(t):
  2. result = None
  3. while True:
  4. a, b = t
  5. result = a, result
  6. if (t := b) is None:
  7. break
  8. return result
  9. print(reverse((1, (3, (6, (8, None))))))

Output:

  1. (8, (6, (3, (1, None))))

答案2

得分: 0

如果您为"reverse"序列提供了额外的参数,您还可以通过递归函数成功地完成:

  1. def reverse(linked_t: tuple, rev_seq=None):
  2. while linked_t[1] is not None:
  3. rev_seq = (linked_t[0], rev_seq)
  4. return reverse(linked_t[1], rev_seq)
  5. else:
  6. return linked_t[0], rev_seq
  7. print(reverse((1, (3, (6, (8, None))))))
  8. ----------
  9. (8, (6, (3, (1, None))))
英文:

If you supply an additional argument for "reverse" sequence you can also succeed with recursive function:

  1. def reverse(linked_t: tuple, rev_seq=None):
  2. while linked_t[1] is not None:
  3. rev_seq = (linked_t[0], rev_seq)
  4. return reverse(linked_t[1], rev_seq)
  5. else:
  6. return linked_t[0], rev_seq
  7. print(reverse((1, (3, (6, (8, None))))))

  1. (8, (6, (3, (1, None))))

huangapple
  • 本文由 发表于 2023年1月9日 02:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定