zoukankan      html  css  js  c++  java
  • 实现一个迭代器类

    # 写一个实现迭代器协议的类 Primes 让此类可以生成从b开始的n个素数
    # class Primes:
    #     def __init__(self, b, n):
    #        ....
    #     ....
    # for x in Primes(10, 4):
    #     print(x)  # 11 13 17 19
    # 
    
    class Primes(object):
        @staticmethod
        def __isprime(x):
            for i in range(2, x):
                if x % i == 0:
                    return False
            return True
    
        def __init__(self, b, n):
            self.begin = b
            self.count = n
        def __iter__(self):
            self.cur_pos = self.begin  # 设置迭代的起始值
            self.cur_count = 0  # 用于记录已生成几个
            return self
    
        def __next__(self):
            # 已完成生成,不需要再生成,我停止迭代
            if self.cur_count >= self.count:
                raise StopIteration
            self.cur_count += 1  # 计数加1
            while True:
                if self.__isprime(self.cur_pos):
                    v = self.cur_pos
                    self.cur_pos += 1
                    return v
                self.cur_pos += 1  # 为下一次循环做准备
    
    
    
    for x in Primes(10, 4):
        print(x)  # 11 13 17 19
  • 相关阅读:
    linux ioctl
    pkg-config用法和gcc cflags
    boost noncopyable类
    google protobuf使用2
    跨平台编译CMake使用
    Linux epoll
    docker安装
    python 脚本转成exe可执行程序
    shell相关知识
    tcpdump使用
  • 原文地址:https://www.cnblogs.com/x931890193/p/9637406.html
Copyright © 2011-2022 走看看