Skip to content

Commit 46cefdd

Browse files
committed
finish reduce
1 parent 8941975 commit 46cefdd

File tree

2 files changed

+102
-130
lines changed

2 files changed

+102
-130
lines changed

built_in_functions/.ipynb_checkpoints/reduce-checkpoint.ipynb

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,161 +5,147 @@
55
"metadata": {},
66
"source": [
77
"# reduce()\n",
8-
"As the name suggests, filter creates a list of elements for which a function returns true. Here is a simple example:"
8+
"\n",
9+
"`reduce(function_to_apply, list_to_apply_to)` is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. For example, if you wanted to compute the product of a list of integers.\n",
10+
"\n",
11+
"So the normal way you might go about doing this task in python is using a basic for loop:"
912
]
1013
},
1114
{
1215
"cell_type": "code",
13-
"execution_count": 13,
16+
"execution_count": 4,
1417
"metadata": {},
1518
"outputs": [
1619
{
1720
"name": "stdout",
1821
"output_type": "stream",
1922
"text": [
20-
"[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n",
21-
"[-5, -4, -3, -2, -1]\n"
23+
"24\n"
2224
]
2325
}
2426
],
2527
"source": [
26-
"def negative(x):\n",
27-
" return x < 0\n",
28-
"\n",
29-
"number_list = list(range(-5, 5))\n",
30-
"less_than_zero = list(filter(negative, number_list))\n",
28+
"product = 1\n",
29+
"list = [1, 2, 3, 4]\n",
30+
"for num in list:\n",
31+
" product = product * num\n",
3132
"\n",
32-
"print(number_list)\n",
33-
"print(less_than_zero)"
33+
"print(product)"
3434
]
3535
},
3636
{
3737
"cell_type": "markdown",
3838
"metadata": {},
3939
"source": [
40-
"Just like `map()` you can use a lambda function for more concise code instead of defining one separately."
40+
"Now let’s try it with `reduce()`:"
4141
]
4242
},
4343
{
4444
"cell_type": "code",
45-
"execution_count": 14,
45+
"execution_count": 5,
4646
"metadata": {},
4747
"outputs": [
4848
{
4949
"name": "stdout",
5050
"output_type": "stream",
5151
"text": [
52-
"[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n",
53-
"[-5, -4, -3, -2, -1]\n"
52+
"24\n"
5453
]
5554
}
5655
],
5756
"source": [
58-
"number_list = list(range(-5, 5))\n",
59-
"less_than_zero = list(filter(lambda x: x < 0, number_list))\n",
57+
"def multiply(x, y):\n",
58+
" return x*y\n",
6059
"\n",
61-
"print(number_list)\n",
62-
"print(less_than_zero)"
60+
"from functools import reduce\n",
61+
"product = reduce(multiply, [1, 2, 3, 4])\n",
62+
"print(product)"
6363
]
6464
},
6565
{
6666
"cell_type": "markdown",
6767
"metadata": {},
6868
"source": [
69-
"The filter resembles a for loop but it is a builtin function and faster.\n",
70-
"Note: If map & filter do not appear beautiful to you then you can also use \n",
71-
" list/dict/tuple comprehensions."
69+
"Just like `filter()` and `map()`, `reduce()` takes a function to apply, so we can use a `lambda` function\n",
70+
"for more concise code:"
7271
]
7372
},
7473
{
7574
"cell_type": "code",
76-
"execution_count": 15,
75+
"execution_count": 7,
7776
"metadata": {},
7877
"outputs": [
7978
{
8079
"name": "stdout",
8180
"output_type": "stream",
8281
"text": [
83-
"[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n",
84-
"[-5, -4, -3, -2, -1]\n"
82+
"24\n"
8583
]
8684
}
8785
],
8886
"source": [
89-
"number_list = list(range(-5, 5))\n",
90-
"less_than_zero = [x for x in number_list if x < 0]\n",
91-
"\n",
92-
"print(number_list)\n",
93-
"print(less_than_zero)"
87+
"from functools import reduce\n",
88+
"product = reduce((lambda x, y: x * y), [1, 2, 3, 4])\n",
89+
"print(product)"
9490
]
9591
},
9692
{
9793
"cell_type": "markdown",
9894
"metadata": {},
9995
"source": [
100-
"You can use `filter()` for many things that require some condition, but just to give another example, you\n",
101-
"can use it to get just the even or odd numbers of a list"
96+
"You can use `reduce()` for a lot of things, so I'll just give a concise example for creating a\n",
97+
"Factorial function:"
10298
]
10399
},
104100
{
105101
"cell_type": "code",
106-
"execution_count": 1,
102+
"execution_count": 26,
107103
"metadata": {},
108104
"outputs": [
109105
{
110106
"name": "stdout",
111107
"output_type": "stream",
112108
"text": [
113-
"[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n",
114-
"[-4, -2, 0, 2, 4]\n",
115-
"[-5, -3, -1, 1, 3]\n"
109+
"120\n"
116110
]
117111
}
118112
],
119113
"source": [
120-
"number_list = list(range(-5, 5))\n",
121-
"even_numbers = list(filter(lambda x : x % 2 == 0, number_list))\n",
122-
"odd_numbers = list(filter(lambda x : x % 2 == 1, number_list))\n",
114+
"def factorial(n):\n",
115+
" return reduce((lambda x,y : x*y), range(1, n+1))\n",
123116
"\n",
124-
"print(number_list)\n",
125-
"print(even_numbers)\n",
126-
"print(odd_numbers)"
117+
"print(factorial(5))"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"This is the same as using a for loop, but in one line instead of four:"
127125
]
128126
},
129127
{
130128
"cell_type": "code",
131-
"execution_count": 3,
129+
"execution_count": 25,
132130
"metadata": {},
133131
"outputs": [
134132
{
135-
"ename": "TypeError",
136-
"evalue": "'int' object is not iterable",
137-
"output_type": "error",
138-
"traceback": [
139-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
140-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
141-
"\u001b[0;32m<ipython-input-3-1da792b5c7a0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
142-
"\u001b[0;31mTypeError\u001b[0m: 'int' object is not iterable"
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"120\n"
143137
]
144138
}
145139
],
146140
"source": [
147-
"number_list = list(range(-5, 5))\n",
148-
"\n",
149-
"def gen():\n",
150-
" for i in range(10):\n",
151-
" yield i\n",
141+
"def factorial(n):\n",
142+
" product = 1\n",
143+
" for i in range(1, n+1):\n",
144+
" product *= i\n",
145+
" return product\n",
152146
"\n",
153-
"for num in gen():\n",
154-
" print(num)\n"
147+
"print(factorial(5))"
155148
]
156-
},
157-
{
158-
"cell_type": "code",
159-
"execution_count": null,
160-
"metadata": {},
161-
"outputs": [],
162-
"source": []
163149
}
164150
],
165151
"metadata": {

0 commit comments

Comments
 (0)