Skip to content

Commit 49bae4f

Browse files
committed
More on functional features
1 parent 7e7a37d commit 49bae4f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import math
2+
from typing import Iterator
3+
4+
5+
def pfactorsl(x: int) -> Iterator[int]:
6+
if x % 2 == 0:
7+
yield 2
8+
if x // 2 > 1:
9+
yield from pfactorsl(x // 2)
10+
return
11+
for i in range(3, int(math.sqrt(x) + .5) + 1, 2):
12+
if x % i == 0:
13+
yield i
14+
if x // i > 1:
15+
yield from pfactorsl(x // i)
16+
return
17+
yield x
18+
19+
20+
if __name__ == '__main__':
21+
print([item for item in pfactorsl(100000)])
22+
23+
24+
25+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import itertools
2+
from typing import Iterable, Any
3+
4+
5+
def limits(iterable: Iterable[Any]) -> Any:
6+
max_tee, min_tee = itertools.tee(iterable, 2)
7+
return max(max_tee), min(min_tee)
8+
9+
10+
def numbers():
11+
for i in range(10):
12+
yield i
13+
14+
15+
if __name__ == '__main__':
16+
ns = numbers()
17+
print(limits(ns))
18+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from decimal import *
2+
from typing import Text, Optional
3+
4+
5+
def clean_decimal(text: Text) -> Optional[Text]:
6+
if text is None:
7+
return None
8+
return Decimal(
9+
text.replace('$', '').replace(',', '')
10+
)
11+
12+
13+
def replace(s: Text, a: Text, b: Text):
14+
return s.replace(a, b)
15+
16+
17+
def clean_decimal_v2(text: Text) -> Optional[Text]:
18+
if text is None:
19+
return None
20+
return Decimal(
21+
replace(replace(text, '$', ''), ',', '')
22+
)
23+
24+
25+
def remove(s: Text, chars: Text) -> Text:
26+
if chars:
27+
return remove(
28+
s.replace(chars[0], ''),
29+
chars[1:]
30+
)
31+
return s
32+
33+
34+
def clean_decimal_v3(text: Text) -> Optional[Text]:
35+
if text is None:
36+
return None
37+
return Decimal(remove(text, '$,'))
38+
39+
40+
if __name__ == '__main__':
41+
s_: Text = '$100,000.1'
42+
print(clean_decimal(s_))
43+
print(clean_decimal_v2(s_))
44+
print(clean_decimal_v3(s_))
45+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Tuple, Callable, NamedTuple
2+
from collections import namedtuple
3+
4+
5+
example_rgb = (128, 152, 64)
6+
7+
RGB = Tuple[int, int, int]
8+
red: Callable[[RGB], int] = lambda color: color[0]
9+
print(example_rgb, red(example_rgb))
10+
11+
12+
Color = namedtuple('Color', 'red green blue name')
13+
example_color = Color(red=128, green=152, blue=64, name='name_1')
14+
print(example_color, example_color.red)
15+
16+
17+
class Color(NamedTuple):
18+
"""RGB color"""
19+
red: int
20+
green: int
21+
blue: int
22+
name: str
23+
24+
25+
example_color = Color(red=128, green=152, blue=64, name='name_2')
26+
print(example_color, example_color.red)
27+

0 commit comments

Comments
 (0)