|
28 | 28 | "cell_type": "markdown",
|
29 | 29 | "metadata": {},
|
30 | 30 | "source": [
|
31 |
| - "## Solution\n", |
32 |
| - "\n", |
33 |
| - "There aren't really any \"correct\" answers here, just make sure you're prepared to answer the following questions about the company you're interviewing with. Be honest, friendly and ready to defend any statements you make with logical arguments to back them up. Note, you should ALWAYS have follow-up questions." |
| 31 | + "___" |
34 | 32 | ]
|
35 | 33 | },
|
36 | 34 | {
|
|
48 | 46 | {
|
49 | 47 | "cell_type": "markdown",
|
50 | 48 | "metadata": {},
|
51 |
| - "source": [ |
52 |
| - "## Solution\n", |
53 |
| - "\n", |
54 |
| - "There's many ways to answer this question, you might be required to solve it multiple ways and discuss some pros and cons of each way. Listed below are various solutions" |
55 |
| - ] |
56 |
| - }, |
57 |
| - { |
58 |
| - "cell_type": "code", |
59 |
| - "execution_count": 14, |
60 |
| - "metadata": { |
61 |
| - "collapsed": false |
62 |
| - }, |
63 |
| - "outputs": [ |
64 |
| - { |
65 |
| - "name": "stdout", |
66 |
| - "output_type": "stream", |
67 |
| - "text": [ |
68 |
| - "13\n", |
69 |
| - "13\n", |
70 |
| - "13\n", |
71 |
| - "13\n", |
72 |
| - "13\n" |
73 |
| - ] |
74 |
| - } |
75 |
| - ], |
76 |
| - "source": [ |
77 |
| - "## Example 1: Using looping technique\n", |
78 |
| - "def fib(n):\n", |
79 |
| - " \n", |
80 |
| - " a,b = 1,1\n", |
81 |
| - " for i in range(n-1):\n", |
82 |
| - " a,b = b,a+b\n", |
83 |
| - " return a\n", |
84 |
| - "\n", |
85 |
| - "print fib(7)\n", |
86 |
| - " \n", |
87 |
| - "# Using recursion \n", |
88 |
| - "def fibR(n):\n", |
89 |
| - " if n==1 or n==2:\n", |
90 |
| - " return 1\n", |
91 |
| - " return fib(n-1)+fib(n-2)\n", |
92 |
| - "\n", |
93 |
| - "print fibR(7)\n", |
94 |
| - " \n", |
95 |
| - "## Example 3: Using generators\n", |
96 |
| - "a,b = 0,1\n", |
97 |
| - "def fibI():\n", |
98 |
| - " global a,b\n", |
99 |
| - " while True:\n", |
100 |
| - " a,b = b, a+b\n", |
101 |
| - " yield a\n", |
102 |
| - "f=fibI()\n", |
103 |
| - "f.next()\n", |
104 |
| - "f.next()\n", |
105 |
| - "f.next()\n", |
106 |
| - "f.next()\n", |
107 |
| - "f.next()\n", |
108 |
| - "f.next()\n", |
109 |
| - "print f.next()\n", |
110 |
| - "\n", |
111 |
| - " \n", |
112 |
| - "## Example 4: Using memoization\n", |
113 |
| - "def memoize(fn, arg):\n", |
114 |
| - " memo = {}\n", |
115 |
| - " if arg not in memo:\n", |
116 |
| - " memo[arg] = fn(arg)\n", |
117 |
| - " return memo[arg]\n", |
118 |
| - " \n", |
119 |
| - "## fib() as written in example 1.\n", |
120 |
| - "fibm = memoize(fib,7)\n", |
121 |
| - "print fibm\n", |
122 |
| - " \n", |
123 |
| - "## Example 5: Using memoization as decorator\n", |
124 |
| - "class Memoize:\n", |
125 |
| - " def __init__(self, fn):\n", |
126 |
| - " self.fn = fn\n", |
127 |
| - " self.memo = {}\n", |
128 |
| - " def __call__(self, arg):\n", |
129 |
| - " if arg not in self.memo:\n", |
130 |
| - " self.memo[arg] = self.fn(arg)\n", |
131 |
| - " return self.memo[arg]\n", |
132 |
| - " \n", |
133 |
| - "@Memoize\n", |
134 |
| - "def fib(n):\n", |
135 |
| - " a,b = 1,1\n", |
136 |
| - " for i in range(n-1):\n", |
137 |
| - " a,b = b,a+b\n", |
138 |
| - " return a\n", |
139 |
| - "print fib(7)" |
140 |
| - ] |
141 |
| - }, |
142 |
| - { |
143 |
| - "cell_type": "markdown", |
144 |
| - "metadata": {}, |
145 |
| - "source": [ |
146 |
| - "Below is a table depicting averaged relative performance time in seconds over 10 runs to caluclate the 15000th fibonacci number.\n", |
147 |
| - "<table width=\"422\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n", |
148 |
| - "<col width=\"64\" />\n", |
149 |
| - "<col width=\"78\" />\n", |
150 |
| - "<col width=\"89\" />\n", |
151 |
| - "<col width=\"94\" />\n", |
152 |
| - "<col width=\"97\" />\n", |
153 |
| - "<tbody>\n", |
154 |
| - "<tr>\n", |
155 |
| - "<td colspan=\"5\" width=\"422\" height=\"20\"> <strong> Fib(n=15000)</strong></td>\n", |
156 |
| - "</tr>\n", |
157 |
| - "<tr>\n", |
158 |
| - "<td width=\"64\" height=\"40\"><strong>loops</strong></td>\n", |
159 |
| - "<td width=\"78\"><strong>recursion</strong></td>\n", |
160 |
| - "<td width=\"89\"><strong>generators</strong></td>\n", |
161 |
| - "<td width=\"94\"><strong>memoization</strong></td>\n", |
162 |
| - "<td width=\"97\"><strong>memoization as decorator</strong></td>\n", |
163 |
| - "</tr>\n", |
164 |
| - "<tr>\n", |
165 |
| - "<td width=\"64\" height=\"20\">45</td>\n", |
166 |
| - "<td width=\"78\">87</td>\n", |
167 |
| - "<td width=\"89\">58</td>\n", |
168 |
| - "<td width=\"94\">44</td>\n", |
169 |
| - "<td width=\"97\">43</td>\n", |
170 |
| - "</tr>\n", |
171 |
| - "<tr>\n", |
172 |
| - "<td width=\"64\" height=\"20\">47</td>\n", |
173 |
| - "<td width=\"78\">88</td>\n", |
174 |
| - "<td width=\"89\">58</td>\n", |
175 |
| - "<td width=\"94\">42</td>\n", |
176 |
| - "<td width=\"97\">42</td>\n", |
177 |
| - "</tr>\n", |
178 |
| - "<tr>\n", |
179 |
| - "<td width=\"64\" height=\"20\">51</td>\n", |
180 |
| - "<td width=\"78\">92</td>\n", |
181 |
| - "<td width=\"89\">60</td>\n", |
182 |
| - "<td width=\"94\">44</td>\n", |
183 |
| - "<td width=\"97\">43</td>\n", |
184 |
| - "</tr>\n", |
185 |
| - "<tr>\n", |
186 |
| - "<td width=\"64\" height=\"20\">43</td>\n", |
187 |
| - "<td width=\"78\">87</td>\n", |
188 |
| - "<td width=\"89\">58</td>\n", |
189 |
| - "<td width=\"94\">42</td>\n", |
190 |
| - "<td width=\"97\">43</td>\n", |
191 |
| - "</tr>\n", |
192 |
| - "<tr>\n", |
193 |
| - "<td width=\"64\" height=\"20\">48</td>\n", |
194 |
| - "<td width=\"78\">92</td>\n", |
195 |
| - "<td width=\"89\">61</td>\n", |
196 |
| - "<td width=\"94\">42</td>\n", |
197 |
| - "<td width=\"97\">44</td>\n", |
198 |
| - "</tr>\n", |
199 |
| - "<tr>\n", |
200 |
| - "<td width=\"64\" height=\"20\">45</td>\n", |
201 |
| - "<td width=\"78\">87</td>\n", |
202 |
| - "<td width=\"89\">59</td>\n", |
203 |
| - "<td width=\"94\">43</td>\n", |
204 |
| - "<td width=\"97\">44</td>\n", |
205 |
| - "</tr>\n", |
206 |
| - "<tr>\n", |
207 |
| - "<td width=\"64\" height=\"20\">44</td>\n", |
208 |
| - "<td width=\"78\">85</td>\n", |
209 |
| - "<td width=\"89\">57</td>\n", |
210 |
| - "<td width=\"94\">42</td>\n", |
211 |
| - "<td width=\"97\">44</td>\n", |
212 |
| - "</tr>\n", |
213 |
| - "<tr>\n", |
214 |
| - "<td width=\"64\" height=\"20\">44</td>\n", |
215 |
| - "<td width=\"78\">87</td>\n", |
216 |
| - "<td width=\"89\">62</td>\n", |
217 |
| - "<td width=\"94\">43</td>\n", |
218 |
| - "<td width=\"97\">43</td>\n", |
219 |
| - "</tr>\n", |
220 |
| - "<tr>\n", |
221 |
| - "<td width=\"64\" height=\"20\">48</td>\n", |
222 |
| - "<td width=\"78\">86</td>\n", |
223 |
| - "<td width=\"89\">59</td>\n", |
224 |
| - "<td width=\"94\">42</td>\n", |
225 |
| - "<td width=\"97\">43</td>\n", |
226 |
| - "</tr>\n", |
227 |
| - "<tr>\n", |
228 |
| - "<td width=\"64\" height=\"21\">45</td>\n", |
229 |
| - "<td width=\"78\">91</td>\n", |
230 |
| - "<td width=\"89\">61</td>\n", |
231 |
| - "<td width=\"94\">45</td>\n", |
232 |
| - "<td width=\"97\">45</td>\n", |
233 |
| - "</tr>\n", |
234 |
| - "<tr>\n", |
235 |
| - "<td width=\"64\" height=\"21\"><strong>46</strong></td>\n", |
236 |
| - "<td width=\"78\"><strong>88.2</strong></td>\n", |
237 |
| - "<td width=\"89\"><strong>59.3</strong></td>\n", |
238 |
| - "<td width=\"94\"><strong>42.9</strong></td>\n", |
239 |
| - "<td width=\"97\"><strong>43.4 (Avg)</strong></td>\n", |
240 |
| - "</tr>\n", |
241 |
| - "</tbody>\n", |
242 |
| - "</table>" |
243 |
| - ] |
| 49 | + "source": [] |
244 | 50 | },
|
245 | 51 | {
|
246 | 52 | "cell_type": "markdown",
|
|
0 commit comments