full-speed-python习题解答(六)--迭代器

1.Implement an iterator class to return the square of all numbers from “a” to “b”.

class all_number(object):
    def __init__(self,a,b):
        self.a = a
        self.b = b
    
    def __iter__(self):
        return self

    def __next__(self):
        if self.a <= self.b:
            value = self.a
            self.a+=1
            return value
        else:
            raise StopIteration

2.Implement an iterator class to return all the even numbers from 1 to (n).

class even_number(object):
    def __init__(self,a,n):
        self.a = a
        self.b = n
    
    def __iter__(self):
        return self

    def __next__(self):
        if self.a <= self.b:
            if (self.a)%2 == 0:
                value = self.a
            else:
                self.a+=1
                value = self.a
            self.a+=2
            return value
                    
        else:
            raise StopIteration

3.Implement an iterator class to return all the odd numbers from 1 to (n).

class odd_number(object):
    def __init__(self,a,n):
        self.a = a
        self.b = n
    
    def __iter__(self):
        return self

    def __next__(self):
        if self.a <= self.b:
            if (self.a)%2 == 0:
                self.a+=1
                value = self.a
            else:
                value = self.a
            self.a+=2
            return value
        else:
            raise StopIteration

4.Implement an iterator class to return all numbers from (n) down to 0.

class down_to_zero(object):
    def __init__(self,a,n):
        self.a = 0
        self.b = n
    
    def __iter__(self):
        return self

    def __next__(self):
        if self.a <= self.b:
            value=self.b
            self.b-=1
            return value
        else:
            raise StopIteration

5.Implement an iterator class to return the fibonnaci sequence from the first element

up to (n). You can check the definition of the fibonnaci sequence in the function’s

chapter. These are the first numbers of the sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

55, 89, . . .

class fibonnaci(object):
    def __init__(self,n):
        self.curr = 0
        self.fol = 1
        self.i = 0
        self.n = n

    
    def __iter__(self):
        return self

    def __next__(self):
        if self.i<self.n:
            value = self.curr
            self.curr=self.fol
            self.fol+=value
            self.i+=1
            return value
        else:
            raise StopIteration

6.Implement an iterator class to return all consecutive pairs of numbers from 0 until

(n), such as (0, 1), (1, 2), (2, 3). . .

class consective_pairs(object):
    def __init__(self,n):
        self.curr = 0
        self.fol = 1
        self.n = n

    
    def __iter__(self):
        return self

    def __next__(self):
        if self.fol<=self.n:
            result = (self.curr,self.fol)
            self.curr=self.fol
            self.fol+=1
            return result
        else:
            raise StopIteration

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值