楼主: helio.liu
1134 1

[问答] 求问一个生成序列的问题.. [推广有奖]

  • 0关注
  • 0粉丝

高中生

42%

还不是VIP/贵宾

-

威望
0
论坛币
519 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
86 点
帖子
5
精华
0
在线时间
48 小时
注册时间
2015-1-8
最后登录
2023-10-30

楼主
helio.liu 发表于 2017-4-9 22:35:33 |AI写论文
1论坛币
Write a Python class that extends the Progression class so that each value
in the progression is the absolute value of the difference between the previous two values. You should include a constructor that accepts a pair of
numbers as the first two values, using 2 and 200 as the defaults.
不知道直接贴码会不会变形。附件是progression的源码
class Progression:
    def __init__(self, start = 0):
        self._current = start
    def _advance(self):
        self._current += 1
    def __next__(self):
        if self._current is None:
            raise StopIteration()
        else:
            answer = self._current
            self._advance()
            return answer
    def __iter__(self):
        return self
    def print_progression(self,n):
        print(' '.join(str(next(self))for j in range(n)))

[size=14.6667px]class FibonacciProgression(Progression):
[size=14.6667px]    def __init__(self, first=0,second=1):
[size=14.6667px]        super().__init__(first)
[size=14.6667px]        self._prev = second + first
[size=14.6667px]    def _advance(self):
[size=14.6667px]        self._prev, self._current = (self._current, self._current-self._prev)

[size=14.6667px]




关键词:Progression difference Fibonacci iteration Progress python
已有 1 人评分论坛币 收起 理由
三世相思2013 + 40 鼓励积极发帖讨论

总评分: 论坛币 + 40   查看全部评分

沙发
helio.liu 发表于 2017-4-11 13:02:54
自顶..

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-29 18:36