Skip to content

Commit 327a4dd

Browse files
authored
Merge pull request #2 from revan730/master
Added some Python examples
2 parents dc29d32 + b9ff5c7 commit 327a4dd

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Python/1-range.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Range:
2+
3+
def __init__(self, first, second=None, step=1):
4+
if second is None:
5+
self.start = 0
6+
self.current = 0
7+
self.end = first
8+
self.step = step
9+
else:
10+
self.start = first
11+
self.current = first
12+
self.end = second
13+
self.step = step
14+
15+
def __iter__(self):
16+
return self
17+
18+
def __next__(self):
19+
result = self.current
20+
if (result >= self.end and self.step > 0
21+
or result <= self.end and self.step < 0):
22+
raise StopIteration
23+
else:
24+
self.current += self.step
25+
return result
26+
27+
def print_space(str):
28+
print("{}".format(str), end=' ')
29+
30+
for i in Range(10):
31+
print_space(i)
32+
print()
33+
for i in Range(3, 18):
34+
print_space(i)
35+
print()
36+
for i in Range(2, 15, 2):
37+
print_space(i)
38+
print()
39+
for i in Range(10, 0, -1):
40+
print_space(i)

Python/2-generator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def Range(first, second=None, step=1):
2+
if second is None:
3+
current = 0
4+
end = first
5+
else:
6+
current = first
7+
end = second
8+
while not (current >= end and step > 0
9+
or current <= end and step < 0):
10+
yield current
11+
current += step
12+
13+
def print_space(str):
14+
print("{}".format(str), end=' ')
15+
16+
17+
for i in Range(10):
18+
print_space(i)
19+
print()
20+
for i in Range(3, 18):
21+
print_space(i)
22+
print()
23+
for i in Range(2, 15, 2):
24+
print_space(i)
25+
print()
26+
for i in Range(10, 0, -1):
27+
print_space(i)

Python/3-primes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import math
2+
3+
def primes(n):
4+
number = 0
5+
count = 0
6+
while count < n:
7+
while True:
8+
if is_prime(number):
9+
yield number
10+
count += 1
11+
number += 1
12+
break
13+
number += 1
14+
15+
def is_prime(number):
16+
if number > 1:
17+
if number == 2:
18+
return True
19+
if number % 2 == 0:
20+
return False
21+
for current in range(3, int(math.sqrt(number) + 1), 2):
22+
if number % current == 0:
23+
return False
24+
return True
25+
return False
26+
27+
for i in primes(10):
28+
print(i)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy