fork download
  1. class myContainer:
  2.  
  3. def __init__(self, data):
  4.  
  5. self.data = data
  6.  
  7. self.len = len(data)
  8.  
  9. self.index = self.len
  10.  
  11. def __iter__(self):
  12.  
  13. return self
  14.  
  15. def __next__(self):
  16.  
  17. if self.index == 0:
  18.  
  19. raise StopIteration
  20.  
  21. self.index -= 1
  22.  
  23. return self.data[ self.index ]
  24.  
  25. class myPrimes:
  26.  
  27. def __init__( self, n ):
  28.  
  29. self.n = n
  30.  
  31. self.index = -1
  32.  
  33. self.data = self.aggregateData(n)
  34.  
  35. self.len = len(self.data)
  36.  
  37. def __iter__(self):
  38.  
  39. return self
  40.  
  41. def isPrime(self,n):
  42.  
  43. #first statement
  44. prime = True
  45. #second statement
  46. i = 2
  47. #third statement
  48. while i * i <= n and prime is True:
  49. prime = n % i != 0
  50. i += 1
  51. #return value
  52. return prime
  53.  
  54. def aggregateData(self,n):
  55.  
  56. list = []
  57.  
  58. for i in range(2, n + 1):
  59.  
  60. if self.isPrime(i):
  61.  
  62. list.append(i)
  63.  
  64. return list
  65.  
  66. def __next__(self):
  67.  
  68. if self.index >= self.len - 1:
  69. raise StopIteration
  70.  
  71. self.index += 1
  72.  
  73. return self.data[ self.index ]
  74.  
  75. def main():
  76.  
  77. ob = myPrimes(100)
  78.  
  79. result = [x for x in ob]
  80.  
  81. print(result)
  82.  
  83. ob2 = myContainer("spam")
  84.  
  85. result2 = [x for x in ob2]
  86.  
  87. print(result2)
  88.  
  89. main()
  90.  
Success #stdin #stdout 0.02s 9168KB
stdin
Standard input is empty
stdout
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
['m', 'a', 'p', 's']