关于元组和并行分配算法
求大佬帮忙看一下这个题怎么做啊!# The Fibonacci sequence is 0, 1, 1, 2, 3, 5, ... where each
# subsequent number is equal to the the preceeding two.
# This means the next elements in the list above would be 8 (5 + 3)
# and 13 (8 + 5)
#
# The 9th element in the sequence is 21. Let's call this the `current` value.
# The 8th element in the sequence is 13. Let's call this the `last` Value.
#
# Using PARALLEL ASSIGNMENT/TUPLE UNPACKING, perform the following operations
# in a single statement
# 1. replace the value of `current` with the value of the 10th
# element in the sequence (so the sum of the 8th and 9th element)
# 2. replace the value of `last` with the value of the 9th element
# Leave this here
current = 21 # at this point, the 9th element of the sequence
last = 13 # at this point, the 8th element of the sequence
# Now, use parallel assignment to replace the value of `current` and `last`
# (put your answer below)
本帖最后由 isdkz 于 2023-3-1 22:32 编辑
它说的是 python 的两个特性:并行赋值(PARALLEL ASSIGNMENT)和 元组解包 (TUPLE UNPACKING)
题目的要求是把 current 替换成斐波那契数列的第 10 个元素的值,把 last 替换成 斐波那契数列的第 9 个元素的值,
而且题目已给出的代码中 last 和 current 恰好是斐波那契数列的第 8 个元素和第 9 个元素的值,
所以同时运用并行赋值和元组解包的代码为:
current, last = current + last, current isdkz 发表于 2023-3-1 22:31
它说的是 python 的两个特性:并行赋值(PARALLEL ASSIGNMENT)和 元组解包 (TUPLE UNPACKING)
题目的 ...
这个并行赋值我还没学所以这道题完全就云里雾里的。。。谢谢! 纯爱战士Dylan1 发表于 2023-3-1 22:38
这个并行赋值我还没学所以这道题完全就云里雾里的。。。谢谢!
不客气,这个并行赋值其实就是依赖于 python 的元组解包机制的,
学了 python 的序列解包就好懂多了,python 不止元组可以解包,凡是序列皆可解包
页:
[1]