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


版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论