/
index.html
550 lines (252 loc) · 13.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<meta name="google-site-verification" content="Sbao2yVur4CMYEsgpErDP5S6uuqaNxw3VOmmtylKbeA" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="I'm Mark, a student at the University of California, Berkeley pursuing a degree in Electrical Engineering and Computer Science. I am currently a Software Engineer at Yelp and a TA for CS61A at UC Berkeley.">
<meta name="author" content="Mark Miyashita">
<link href="http://feeds.feedburner.com/markmiyashita" rel="alternate" title="Mark Miyashita" type="application/atom+xml">
<title>
CS61A - Zip Generator - Mark Miyashita
</title>
<link href='http://fonts.googleapis.com/css?family=Lato:100,300,400,700,900' rel='stylesheet' type='text/css'>
<link href="/assets/css/bootstrap.css" rel="stylesheet">
<link href="/assets/css/main.css" rel="stylesheet">
<link href="/assets/css/bootstrap-overrides.css" rel="stylesheet">
<link href="/assets/css/solarized_dark.css" rel="stylesheet">
<link href="/assets/css/default.css" rel="stylesheet">
<link href="/assets/css/custom.css" rel="stylesheet">
<script src="/assets/js/jquery-1.8.3.min.js"></script>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="assets/js/html5shiv.js"></script>
<script src="assets/js/respond.min.js"></script>
<![endif]-->
<!-- Start of Google Analytics Code -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-29669299-1']);
_gaq.push(['_setDomainName', 'markmiyashita.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- End of Google Analytics Code -->
<!-- Start of Woopra Code -->
<script>
(function(){
var t,i,e,n=window,o=document,a=arguments,s="script",r=["config","track","identify","visit","push","call"],c=function(){var t,i=this;for(i._e=[],t=0;r.length>t;t++)(function(t){i[t]=function(){return i._e.push([t].concat(Array.prototype.slice.call(arguments,0))),i}})(r[t])};for(n._w=n._w||{},t=0;a.length>t;t++)n._w[a[t]]=n[a[t]]=n[a[t]]||new c;i=o.createElement(s),i.async=1,i.src="//static.woopra.com/js/w.js",e=o.getElementsByTagName(s)[0],e.parentNode.insertBefore(i,e)
})("woopra");
woopra.config({
domain: 'markmiyashita.com',
idle_timeout: 600000
});
woopra.track('pv', {
url: window.location.pathname+window.location.search,
title: document.title
});
</script>
<!-- End of Woopra Code -->
</head><body><a class="sr-only" href="#content">Skip Navigation</a><header class="navbar navbar-inverse navbar-fixed-top bs-docs-nav" role="banner">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
<ul class="nav navbar-nav">
<li>
<a href="/">About</a>
</li>
<li>
<a href="/resume">Resume</a>
</li>
<li>
<a href="/projects">Projects</a>
</li>
<li>
<a href="/blog">Blog</a>
</li>
<li class="active">
<a href="/cs61a/sp14">CS61A</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">More CS Classes <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="/cs61b">CS61B - Data Structures</a></li>
<li><a href="/cs61c">CS61C - Computer Architecture</a></li>
<li><a href="/cs70">CS70 - Discrete Mathematics</a></li>
</ul>
</li>
<li>
<a href="/interviews/problems">Interview Practice</a>
</li>
<li >
<a href="/blueprint">Blueprint</a>
</li>
<li>
<a href="/hkn">HKN</a>
</li>
<li>
<a href="/web_dev">Web Development</a>
</li>
<li>
<a href="/contact">Contact</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="bs-header" id="content">
<div class="container">
<img class="profile-pic" src="/assets/images/mark_miyashita.jpg"></img>
<h1>Mark Miyashita</h1>
<h4>negativetwelve</h4>
<p>
<a href="/resume/#education">Student</a>, <a href="/resume/#work">Software Engineer</a>, <a href="/cs61a">Teacher</a>, <a href="/blog">Aspiring Entrepreneur</a>
</p>
</div>
</div>
<div class="bs-old-docs">
<div class="container">
Looking for more CS classes? Check out:
<strong><a href="/cs61a">CS61A</a></strong>
-
<a href="/cs61b">CS61B</a>
-
<a href="/cs61c">CS61C</a>
-
<a href="/cs70">CS70</a>
</div>
</div>
<div class="container bs-docs-container"><div class="row"><div class="col-md-3">
<div class="bs-sidebar hidden-print" role="complementary" data-spy="affix" data-offset-top="335">
<ul class="nav bs-sidenav">
<li><a href="/cs61a/sp14/practice">All Problems and Notes (62)</a></li>
<li><a href="/cs61a/sp14/basics">Basics (3)</a></li>
<li><a href="/cs61a/sp14/higher_order_functions">Higher Order Functions (3)</a></li>
<li><a href="/cs61a/sp14/environment_diagrams">Environment Diagrams (4)</a></li>
<li><a href="/cs61a/sp14/lambdas">Lambdas (1)</a></li>
<li><a href="/cs61a/sp14/newtons_method">Newtons Method (1)</a></li>
<li><a href="/cs61a/sp14/recursion">Recursion (8)</a></li>
<li><a href="/cs61a/sp14/rlists">Rlists (11)</a></li>
<li><a href="/cs61a/sp14/iterables">Iterables (2)</a></li>
<li><a href="/cs61a/sp14/object_oriented_programming">Object Oriented Programming (2)</a></li>
<li><a href="/cs61a/sp14/mutable_data">Mutable Data (1)</a></li>
<li><a href="/cs61a/sp14/inheritance">Inheritance (1)</a></li>
<li><a href="/cs61a/sp14/generic_functions">Generic Functions (1)</a></li>
<li><a href="/cs61a/sp14/orders_of_growth">Orders Of Growth (1)</a></li>
<li><a href="/cs61a/sp14/tree_recursion">Tree Recursion (7)</a></li>
<li><a href="/cs61a/sp14/memoization">Memoization (1)</a></li>
<li><a href="/cs61a/sp14/scheme">Scheme (6)</a></li>
<li><a href="/cs61a/sp14/tail_recursion">Tail Recursion (1)</a></li>
<li><a href="/cs61a/sp14/generators">Generators (3)</a></li>
<li><a href="/cs61a/sp14/streams">Streams (1)</a></li>
<li><a href="/cs61a/sp14/iterators">Iterators (2)</a></li>
<li><a href="/cs61a/sp14/logic">Logic (2)</a></li>
</ul>
</div>
</div>
<div class="col-md-9" role="main">
<div class="page-header">
<h3>Zip Generator</h3>
</div>
<p>Write a generator that takes in any number of iterables and zips them together. It should output a series of lists, each containing the nth items of each iterable. It should stop when the smallest iterable runs out of elements. For example:</p>
<pre><code>def zip( ):
"""
>>> z = zip([1, 2, 3], [4, 5, 6], [7, 8])
>>> for i in z:
... print(i)
...
[1, 4, 7]
[2, 5, 8]
"""
"***YOUR CODE HERE***"
</code></pre>
<p><a href="#solution" class="solution_toggle">Toggle Solution</a></p>
<div class="solution">
<pre><code>def zip(*iterables):
"""
>>> z = zip([1, 2, 3], [4, 5, 6], [7, 8])
>>> for i in z:
... print(i)
...
[1, 4, 7]
[2, 5, 8]
"""
iterators = [iter(iterable) for iterable in iterables]
while True:
yield [next(iterator) for iterator in iterators]
</code></pre>
<p>There were several concepts that were tested in this problem. The first was the * syntax most commonly associated with <code>*args</code>. Remember that <code>args</code> is just the name of the list of arguments, but the variable can be named anything. For this problem, we’re going to call it <code>iterables</code> because it’s going to be a list of iterables. Since they are iterables, we need to make them all into iterators so the best way to do this is call <code>iter</code> on each of the iterables to make iterators. This can also be done using <code>map</code>. Next, we need a loop that runs to the end of the shortest iterator, however, we note that when we reach the end of the shortest iterator, we need to raise a <code>StopIteration</code>. We then note that when calling <code>next</code> on an iterator that is out of elements, it raises a <code>StopIteration</code>! So, our job is mainly complete and we can just yield the next items of each iterator. When one iterator runs out of elements, it will raise a <code>StopIteration</code> which in turn will make the <code>zip</code> function return a <code>StopIteration</code> as well.</p>
</div>
<p class="disclaimer">
I don't claim to be perfect so if you find an error on this page, please <a href="/contact">send me an email</a> preferably with a link to this page so that I know what I need to fix!
</p>
<article class="post">
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'markmiyashita'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</article>
</div></div></div><footer class="bs-footer" role="contentinfo"><p class="copyright">© 2014 Mark Miyashita</p><ul class="footer-links"><li><a href="http://github.com/negativetwelve">Github</a></li><li class="muted">·</li><li><a href="https://hkn.eecs.berkeley.edu/coursesurveys/instructor/7762">TA Ratings</a></li><li class="muted">·</li><li><a href="https://www.facebook.com/markmiyashita12">Facebook</a></li><li class="muted">·</li><li><a href="http://www.linkedin.com/pub/mark-miyashita/38/63/49/">LinkedIn</a></li><li class="muted">·</li><li><a href="https://plus.google.com/101180624276428786239?rel=author">Google+</a></li></ul></footer><script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/highlight.pack.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".solution_toggle").click(function(event) {
event.preventDefault();
$(".solution").fadeToggle('medium');
});
});
hljs.initHighlightingOnLoad();
$(document).keypress(function(e) {
base = document.location.origin;
switch (e.charCode) {
case 97: // 'a' for about
document.location.href = base;
break;
case 98: // 'b' for blog
document.location.href = base + '/blog';
break;
case 104: // 'h' for hkn
document.location.href = base + "/hkn";
break;
case 105: // 'i' for interview practice
document.location.href = base + "/interviews/problems";
break;
case 109: // 'm' for more
$('#sub-links').slideDown('medium');
break;
case 112: // 'p' for projects
document.location.href = base + '/projects';
break;
case 114: // 'r' for resume
document.location.href = base + '/resume';
break;
case 119: // 'w' for web development
document.location.href = base + '/web_dev';
break;
case 99: // 'c' for cs61a
window.location.href = base + "/cs61a";
break;
default:
console.log(e.charCode);
}
});
</script>
</body></html>