|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "1.1 Write a Python Program to implement your own myreduce() function which works exactly\n", |
| 8 | + "like Python's built-in function reduce()" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [ |
| 16 | + { |
| 17 | + "name": "stdout", |
| 18 | + "output_type": "stream", |
| 19 | + "text": [ |
| 20 | + "15\n" |
| 21 | + ] |
| 22 | + } |
| 23 | + ], |
| 24 | + "source": [ |
| 25 | + "def do_sum(x, y): \n", |
| 26 | + " return x + y\n", |
| 27 | + "\n", |
| 28 | + "def myreduce(func, seq):\n", |
| 29 | + " first = seq[0]\n", |
| 30 | + " for i in seq[1:]:\n", |
| 31 | + " first = func(first, i)\n", |
| 32 | + " return first\n", |
| 33 | + "\n", |
| 34 | + "mylist=[1,2,3,4,5]\n", |
| 35 | + "print(myreduce(do_sum, mylist))" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "1.2 Write a Python program to implement your own myfilter() function which works exactly\n", |
| 43 | + "like Python's built-in function filter()" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 2, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "name": "stdout", |
| 53 | + "output_type": "stream", |
| 54 | + "text": [ |
| 55 | + "[2, 4, 6, 8]\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "mylist=[1,2,3,4,5,6,7,8]\n", |
| 61 | + "evenlist=[]\n", |
| 62 | + "\n", |
| 63 | + "def is_even(x):\n", |
| 64 | + " if x % 2 == 0:\n", |
| 65 | + " return True\n", |
| 66 | + " else:\n", |
| 67 | + " return False\n", |
| 68 | + " \n", |
| 69 | + "\n", |
| 70 | + "def myfilter(fun,mylist):\n", |
| 71 | + " for i in mylist:\n", |
| 72 | + " a=fun(i)\n", |
| 73 | + " if a==True:\n", |
| 74 | + " evenlist.append(i)\n", |
| 75 | + " return evenlist\n", |
| 76 | + "\n", |
| 77 | + "\n", |
| 78 | + "print(myfilter(is_even, mylist))\n", |
| 79 | + "\n" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "raw", |
| 84 | + "metadata": {}, |
| 85 | + "source": [ |
| 86 | + "2. Implement List comprehensions to produce the following lists.\n", |
| 87 | + "Write List comprehensions to produce the following Lists\n", |
| 88 | + "['x', 'xx', 'xxx', 'xxxx', 'y', 'yy', 'yyy', 'yyyy', 'z', 'zz', 'zzz', 'zzzz']\n", |
| 89 | + "['x', 'y', 'z', 'xx', 'yy', 'zz', 'xxx', 'yyy', 'zzz', 'xxxx', 'yyyy', 'zzzz']\n", |
| 90 | + "[[2], [3], [4], [3], [4], [5], [4], [5], [6]] \n", |
| 91 | + "[[2, 3, 4, 5], [3, 4, 5, 6],[4, 5, 6, 7], [5, 6, 7, 8]]\n", |
| 92 | + "[(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3)]" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 3, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [ |
| 100 | + { |
| 101 | + "data": { |
| 102 | + "text/plain": [ |
| 103 | + "['x', 'xx', 'xxx', 'xxxx', 'y', 'yy', 'yyy', 'yyyy', 'z', 'zz', 'zzz', 'zzzz']" |
| 104 | + ] |
| 105 | + }, |
| 106 | + "execution_count": 3, |
| 107 | + "metadata": {}, |
| 108 | + "output_type": "execute_result" |
| 109 | + } |
| 110 | + ], |
| 111 | + "source": [ |
| 112 | + "[i*j for i in ('x','y','z') for j in range(1,5)]" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 4, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "text/plain": [ |
| 123 | + "['x', 'y', 'z', 'xx', 'yy', 'zz', 'xxx', 'yyy', 'zzz', 'xxxx', 'yyyy', 'zzzz']" |
| 124 | + ] |
| 125 | + }, |
| 126 | + "execution_count": 4, |
| 127 | + "metadata": {}, |
| 128 | + "output_type": "execute_result" |
| 129 | + } |
| 130 | + ], |
| 131 | + "source": [ |
| 132 | + "[ i*j for i in range(1,5) for j in ('x','y','z') ] " |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": 5, |
| 138 | + "metadata": {}, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "data": { |
| 142 | + "text/plain": [ |
| 143 | + "[[2], [3], [4], [3], [4], [5], [4], [5], [6]]" |
| 144 | + ] |
| 145 | + }, |
| 146 | + "execution_count": 5, |
| 147 | + "metadata": {}, |
| 148 | + "output_type": "execute_result" |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "[[j] for i in range(0,3) for j in range(2+i,5+i)] " |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": 6, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [ |
| 160 | + { |
| 161 | + "data": { |
| 162 | + "text/plain": [ |
| 163 | + "[[2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7], [5, 6, 7, 8]]" |
| 164 | + ] |
| 165 | + }, |
| 166 | + "execution_count": 6, |
| 167 | + "metadata": {}, |
| 168 | + "output_type": "execute_result" |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "[[i for i in range(2+j,6+j)] for j in range(0,4)]" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 7, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "data": { |
| 182 | + "text/plain": [ |
| 183 | + "[(1, 1), (2, 1), (3, 1), (1, 2), (2, 2), (3, 2), (1, 3), (2, 3), (3, 3)]" |
| 184 | + ] |
| 185 | + }, |
| 186 | + "execution_count": 7, |
| 187 | + "metadata": {}, |
| 188 | + "output_type": "execute_result" |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "[(j,i) for i in range(1,4) for j in range(1,4)]" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "metadata": { |
| 197 | + "kernelspec": { |
| 198 | + "display_name": "Python 3", |
| 199 | + "language": "python", |
| 200 | + "name": "python3" |
| 201 | + }, |
| 202 | + "language_info": { |
| 203 | + "codemirror_mode": { |
| 204 | + "name": "ipython", |
| 205 | + "version": 3 |
| 206 | + }, |
| 207 | + "file_extension": ".py", |
| 208 | + "mimetype": "text/x-python", |
| 209 | + "name": "python", |
| 210 | + "nbconvert_exporter": "python", |
| 211 | + "pygments_lexer": "ipython3", |
| 212 | + "version": "3.7.4" |
| 213 | + } |
| 214 | + }, |
| 215 | + "nbformat": 4, |
| 216 | + "nbformat_minor": 2 |
| 217 | +} |
0 commit comments