Skip to content

Commit 92c249d

Browse files
committed
python ex
1 parent 72219b8 commit 92c249d

File tree

4 files changed

+188
-11
lines changed

4 files changed

+188
-11
lines changed

Diff for: stepic/.DS_Store

0 Bytes
Binary file not shown.

Diff for: stepic/python_fullstack/groupby_example.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333

3434
#########################################
3535

36-
# tpl = (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
36+
tpl = (1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4)
3737

38-
# print('TUPLE')
39-
# print([(i, list(j)) for i, j in itertools.groupby(tpl)])
38+
print('TUPLE')
39+
print([(i, list(j)) for i, j in itertools.groupby(tpl)])
4040

41-
# print(tuple((i, tuple(j)) for i, j in itertools.groupby(tpl) ))
41+
print(tuple((i, tuple(j)) for i, j in itertools.groupby(tpl) ))
4242

4343
#########################################
4444

@@ -55,13 +55,22 @@
5555
"""
5656

5757

58-
from typing import Dict
59-
from itertools import groupby
58+
# from typing import Dict
59+
# from itertools import groupby
60+
61+
62+
# def word_frequencies(string):
63+
# return {i:len(list(j)) for i,j in groupby(sorted(string.split()))}
64+
# # Поместите свой код сюда
6065

66+
# print(word_frequencies('hello world and hello universe'))
67+
# print(word_frequencies('hello world'))
68+
69+
from itertools import groupby
6170

62-
def word_frequencies(string):
63-
return {i:len(list(j)) for i,j in groupby(sorted(string.split()))}
64-
# Поместите свой код сюда
71+
def group_integers(names):
72+
return [[*list(j)] for i, j in groupby(sorted(names))]
6573

66-
print(word_frequencies('hello world and hello universe'))
67-
print(word_frequencies('hello world'))
74+
print(group_integers([1, 2, 3, 4, 3, 2, 1]))
75+
print(group_integers([-1, -2, 3, -2, 4, -1]))
76+
print(group_integers([5, 5, 5, 5, 5]))

Diff for: stepic/python_fullstack/map_example.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import math
2+
3+
def square(n):
4+
return pow(n, 2)
5+
6+
# num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
7+
8+
# squared_list = map(square, num_list)
9+
# print(squared_list)
10+
# print(f'squared_list: {list(squared_list)}')
11+
12+
############################################
13+
14+
# float_list = [1.23, 2.24, 3.35, 4.46, 5.57, 6.68, 7.79, 8.89, 9.99]
15+
16+
# rounded_list = map(round, float_list)
17+
# ceiled_list = map(math.ceil, float_list)
18+
# floored_list = map(math.floor, float_list)
19+
20+
# print(f'rounded_list: {list(rounded_list)}')
21+
# print(f'ceiled_list: {list(ceiled_list)}')
22+
# print(f'floored_list: {list(floored_list)}')
23+
24+
#############################################
25+
26+
# my_string = "DADADA DUDUDU DIDIDI"
27+
28+
# def lower_chars(s):
29+
# return s.lower()
30+
31+
# new_str_list = map(lower_chars, my_string)
32+
# print(f'new_str_list: {list(new_str_list)}')
33+
34+
#############################################
35+
36+
# num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
37+
38+
# lambda_list = map(lambda x: x*10, num_list)
39+
# print(f'lambda_list: {list(lambda_list)}')
40+
41+
#############################################
42+
43+
# list1 = [2, 2, 2, 2, 2]
44+
# list2 = [5, 5, 5, 5, 5]
45+
46+
# def mult_func(list1, list2):
47+
# return list1 * list2
48+
49+
# mult_list = map(mult_func, list1, list2)
50+
# print(f'mult_list: {list(mult_list)}')
51+
52+
#############################################
53+
54+
tuple1 = ("Room", "Room", "Room", "Room")
55+
list1 = ["A", "B", "C", "D"]
56+
57+
def tupl_list_func(tuple1, list1):
58+
return tuple1 + " - " + list1
59+
60+
room_list = map(tupl_list_func, tuple1, list1)
61+
print(f'room_list: {list(room_list)}')

Diff for: stepic/python_fullstack/reduce_example.py

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# functools.reduce(function, iterable[, initializer])
2+
from functools import reduce
3+
4+
5+
# The Python documentation also states that reduce() is roughly equivalent to the following Python function:
6+
def reduce(function, iterable, initializer=None):
7+
it = iter(iterable)
8+
if initializer is None:
9+
value = next(it)
10+
else:
11+
value = initializer
12+
for element in it:
13+
value = function(value, element)
14+
return value
15+
16+
#######################
17+
18+
def ave_add(a, b):
19+
result = a + b
20+
print(f"{a} + {b} = {result}")
21+
return result
22+
23+
print(ave_add(2, 4))
24+
25+
nums = [1, 2, 3, 4, 5]
26+
27+
print(reduce(ave_add, nums))
28+
29+
############## Initializer
30+
31+
print(reduce(ave_add, nums, 1000))
32+
33+
print(reduce(lambda a, b: a + b, nums))
34+
35+
############################
36+
37+
from operator import add
38+
39+
print(add(2, 3))
40+
41+
print(reduce(add, nums))
42+
43+
print(sum(nums))
44+
45+
############### mult nums
46+
47+
def ave_mult(product, nums):
48+
for num in nums:
49+
product *= num
50+
51+
return product
52+
53+
print(ave_mult(1, nums))
54+
55+
##################
56+
57+
def ave_mult(a, b):
58+
return a * b
59+
60+
print(ave_mult(4, 5))
61+
62+
print(reduce(ave_mult, nums))
63+
64+
print(reduce(lambda a, b: a * b, nums))
65+
66+
########################
67+
68+
from operator import mul
69+
70+
print(mul(4, 5))
71+
72+
print(reduce(mul, nums))
73+
74+
################ reduce vs accumulate
75+
76+
from itertools import accumulate
77+
78+
print(list(accumulate(nums)))
79+
80+
print(reduce(add, nums))
81+
82+
print(list(accumulate(mul)))
83+
84+
print(reduce(mul, nums))
85+
86+
### performance vs readability
87+
88+
from timeit import timeit
89+
90+
# func
91+
92+
def add(a, b):
93+
return a + b
94+
95+
ave_add = reduce(add, range(100))
96+
print(timeit(ave_add, "import functools", globals={'add': add}))
97+
98+
# lambda
99+
ave_lambda = reduce(lambda x, y: x + y, range(100))
100+
print(timeit(ave_lambda, "import functools"))
101+
102+
# operator.add
103+
operator_add = reduce(add, range(100))
104+
print(timeit(operator_add, "import functools, operator"))
105+
106+
# sum
107+
print(sum(range(100)), globals={"sum":sum})

0 commit comments

Comments
 (0)