-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlessons.py
More file actions
1549 lines (1269 loc) · 56 KB
/
Copy pathlessons.py
File metadata and controls
1549 lines (1269 loc) · 56 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""The 'Learn' curriculum: 22 lessons, each = theory + runnable example + exercise."""
from __future__ import annotations
import copy
from dataclasses import dataclass, field
import i18n
import lessons_de
from tasks import Task, case
@dataclass
class Lesson:
id: str
title: str
section: str
theory: str
example: str
task: Task
takeaway: str = ""
def _t(lid, title, func, statement, starter, cases, hints, solution, topic,
checker_src: str = "") -> Task:
return Task(id=f"lesson_{lid}", title=title, func=func, statement=statement,
starter=starter, cases=cases, hints=hints, solution=solution,
difficulty="Easy", topic=topic, source="lesson",
checker_src=checker_src)
STACK_CHECKER = '''
def check(args, got):
items = list(args[0])
if got is None:
return False
for name in ("push", "pop", "peek", "size"):
if not callable(getattr(got, name, None)):
return False
if got.size() != len(items):
return False
if items:
if got.peek() != items[-1]:
return False
if got.size() != len(items): # peek must not remove anything
return False
if got.pop() != items[-1]:
return False
if got.size() != len(items) - 1:
return False
else:
if got.peek() is not None or got.pop() is not None:
return False
got.push("sentinel")
return got.peek() == "sentinel" and got.size() == max(0, len(items) - 1) + 1
'''
LESSONS: list[Lesson] = [
# ============================================================ 1. FOUNDATIONS
Lesson(
id="vars", section="Foundations", title="Variables & types",
theory="""Python has no type declarations. A variable is just a name pointing at an object,
and the object knows its own type.
age = 30 # int
price = 4.99 # float
name = "Ada" # str
is_ready = True # bool (capital T / F!)
nothing = None # the "no value" object
Useful things you will use constantly:
* type(x) -> the type of x
* int("42") -> convert text to a number
* str(42) -> convert a number to text
* f-strings -> f"{name} is {age}" builds text with values inside
Integer division and remainder come up in every interview:
7 / 2 -> 3.5 (true division, always a float)
7 // 2 -> 3 (floor division)
7 % 2 -> 1 (remainder — the key to even/odd and cyclic problems)
2 ** 10 -> 1024 (power)
Names are snake_case, constants are SHOUTING_CASE. Python cares about that
socially, not technically.""",
example='''name = "Ada"
age = 36
height = 1.68
likes_python = True
print(f"{name} is {age} years old and {height} m tall.")
print("type of age:", type(age))
print("age as text:", str(age) + " years")
print("7 / 2 =", 7 / 2)
print("7 // 2 =", 7 // 2)
print("7 % 2 =", 7 % 2)
print("2 ** 10 =", 2 ** 10)
# multiple assignment / swapping without a temp variable
a, b = 1, 2
a, b = b, a
print("swapped:", a, b)
''',
takeaway="A variable is a label on an object. % and // are your interview workhorses.",
task=_t("vars", "Seconds to clock", "to_clock",
"""Write to_clock(seconds) that turns a number of seconds into a "H:MM:SS" string.
Rules:
* hours have no leading zero, minutes and seconds always have two digits
* to_clock(3661) -> "1:01:01"
* to_clock(59) -> "0:00:59"
Hint: // gives you whole units, % gives you the leftovers.""",
'def to_clock(seconds):\n # your code here\n pass\n',
[case(3661, "1:01:01"), case(59, "0:00:59"), case(0, "0:00:00"),
case(86399, "23:59:59", hidden=True), case(600, "0:10:00", hidden=True),
case(45296, "12:34:56", hidden=True)],
["hours = seconds // 3600", "minutes = (seconds % 3600) // 60",
'f-strings can pad: f"{m:02d}" gives "07" for 7'],
'def to_clock(seconds):\n hours = seconds // 3600\n minutes = (seconds % 3600) // 60\n'
' secs = seconds % 60\n return f"{hours}:{minutes:02d}:{secs:02d}"\n',
"Basics"),
),
Lesson(
id="strings", section="Foundations", title="Strings",
theory="""Strings are immutable sequences of characters. Every "modifying" method returns a
NEW string.
s = " Hello, World "
s.strip() -> "Hello, World"
s.lower() -> " hello, world "
s.replace("l","L") -> new string
s.split(",") -> [' Hello', ' World ']
",".join(parts) -> glue a list back together
s.startswith("H"), s.endswith("d"), "World" in s
Indexing and slicing (this is the single most useful Python skill):
s[0] first character
s[-1] last character
s[2:5] characters 2,3,4 (start included, stop excluded)
s[:3] first three
s[3:] everything from 3 on
s[::-1] the whole thing reversed
Building a string in a loop with += is O(n^2). Collect pieces in a list and
"".join(them) instead — interviewers notice.""",
example='''s = " Hello, Python World "
print(repr(s.strip()))
print(s.strip().lower())
print(s.strip().split())
print("-".join(["a", "b", "c"]))
word = "interview"
print("first:", word[0], "| last:", word[-1])
print("slice 2:5 ->", word[2:5])
print("reversed ->", word[::-1])
print("every 2nd ->", word[::2])
print("is 'view' inside?", "view" in word)
print("count of 'e':", word.count("e"))
# efficient string building
parts = []
for i in range(5):
parts.append(str(i * i))
print(", ".join(parts))
''',
takeaway="Strings are immutable. Slice with [start:stop:step]. Join, don't +=.",
task=_t("strings", "Normalise a name", "normalise",
"""Write normalise(text) that cleans up a messy full name:
* strip whitespace at both ends
* collapse multiple inner spaces into one
* capitalise every word (first letter upper, rest lower)
normalise(" aDA LOVElace ") -> "Ada Lovelace"
Hint: text.split() with no argument already splits on any run of whitespace.""",
'def normalise(text):\n # your code here\n pass\n',
[case(" aDA LOVElace ", "Ada Lovelace"),
case("guido van ROSSUM", "Guido Van Rossum"),
case(" ", ""),
case("gRaCe hopper", "Grace Hopper", hidden=True),
case("a", "A", hidden=True),
case(" linus TORVALDS ", "Linus Torvalds", hidden=True)],
["words = text.split() removes all the extra whitespace for you",
"word.capitalize() upper-cases the first letter and lowers the rest",
'return " ".join(...)'],
'def normalise(text):\n words = text.split()\n'
' return " ".join(word.capitalize() for word in words)\n',
"Strings"),
),
Lesson(
id="lists", section="Foundations", title="Lists",
theory="""A list is an ordered, mutable sequence. It is the default container in Python.
nums = [3, 1, 4, 1, 5]
nums.append(9) add to the end O(1)
nums.pop() remove & return the last O(1)
nums.pop(0) remove & return the first O(n) <- careful!
nums.insert(0, 7) insert at front O(n)
nums.remove(1) remove the FIRST value 1 O(n)
len(nums), sum(nums), min(nums), max(nums)
nums.sort() sorts in place, returns None
sorted(nums) returns a NEW sorted list
nums[::-1] reversed copy
The classic beginner trap:
a = [1, 2, 3]
b = a # b is the SAME list, not a copy
b.append(4) # a is now [1, 2, 3, 4] too!
c = a[:] # this IS a copy (or a.copy() / list(a))
Sorting with a key is interview bread and butter:
people.sort(key=lambda p: p[1]) by second element
words.sort(key=len, reverse=True) longest first
items.sort(key=lambda x: (-x.score, x.name)) score desc, then name asc""",
example='''nums = [3, 1, 4, 1, 5, 9, 2, 6]
print("length:", len(nums), "| sum:", sum(nums), "| max:", max(nums))
nums.append(5)
print("after append:", nums)
print("popped:", nums.pop(), "->", nums)
print("sorted copy:", sorted(nums))
print("original still:", nums)
nums.sort()
print("sorted in place:", nums)
words = ["pear", "fig", "banana", "kiwi"]
words.sort(key=len)
print("by length:", words)
words.sort(key=lambda w: (-len(w), w))
print("long->short, then a-z:", words)
# aliasing vs copying
a = [1, 2, 3]
alias, copy = a, a[:]
alias.append(99)
print("a:", a, "| copy:", copy)
''',
takeaway="Lists are mutable and shared by reference. sort() mutates, sorted() copies.",
task=_t("lists", "Second largest", "second_largest",
"""Write second_largest(nums) that returns the second largest DISTINCT value in a list.
second_largest([3, 1, 4, 4, 5]) -> 4
second_largest([7, 7, 7]) -> None (there is no second distinct value)
Return None if the list has fewer than two distinct values.""",
'def second_largest(nums):\n # your code here\n pass\n',
[case([3, 1, 4, 4, 5], 4), case([7, 7, 7], None), case([2, 1], 1),
case([10], None, hidden=True), case([], None, hidden=True),
case([-5, -2, -9, -2], -5, hidden=True),
case([1, 2, 3, 4, 5, 6], 5, hidden=True)],
["set(nums) throws away the duplicates",
"sorted(...) then take index -2",
"Guard the short case first: if len(distinct) < 2: return None"],
'def second_largest(nums):\n distinct = sorted(set(nums))\n'
' if len(distinct) < 2:\n return None\n return distinct[-2]\n',
"Lists"),
),
Lesson(
id="control", section="Foundations", title="if / else & loops",
theory="""Indentation IS the block. Four spaces, always.
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
Loop over the ITEMS, not the indices, whenever you can:
for word in words: # good
for i in range(len(words)): # only when you truly need i
for i, word in enumerate(words): # index AND item
for name, score in zip(names, scores): # two lists in lockstep
while loops run until a condition flips:
while low <= high: # binary search shape
...
Loop control:
break leave the loop right now
continue skip to the next iteration
for...else the else runs only if the loop was NOT broken out of
range(start, stop, step) — stop is exclusive:
range(5) 0 1 2 3 4
range(2, 8, 2) 2 4 6
range(5, 0, -1) 5 4 3 2 1""",
example='''scores = [92, 78, 85, 61, 99]
names = ["Ada", "Linus", "Grace", "Guido", "Hedy"]
for name, score in zip(names, scores):
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
print(f"{name:<6} {score:>3} {grade}")
print("---")
for i, name in enumerate(names, start=1):
print(i, name)
print("--- first score above 95")
for score in scores:
if score > 95:
print("found", score)
break
else:
print("none found")
n, steps = 27, 0
while n != 1: # Collatz
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps += 1
print("collatz steps for 27:", steps)
''',
takeaway="Iterate over items; reach for enumerate/zip before range(len(x)).",
task=_t("control", "FizzBuzz, returned not printed", "fizzbuzz",
"""The classic — but return a LIST instead of printing.
fizzbuzz(n) returns a list for the numbers 1..n where:
* multiples of 3 and 5 -> "FizzBuzz"
* multiples of 3 -> "Fizz"
* multiples of 5 -> "Buzz"
* everything else -> the number itself, as an int
fizzbuzz(5) -> [1, 2, "Fizz", 4, "Buzz"]
Check the 15-case FIRST, or it can never happen.""",
'def fizzbuzz(n):\n # your code here\n pass\n',
[case(5, [1, 2, "Fizz", 4, "Buzz"]),
case(3, [1, 2, "Fizz"]),
case(15, [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz",
11, "Fizz", 13, 14, "FizzBuzz"]),
case(0, [], hidden=True),
case(1, [1], hidden=True),
case(16, [1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz",
11, "Fizz", 13, 14, "FizzBuzz", 16], hidden=True)],
["Build a result list and append to it",
"range(1, n + 1) gives you 1..n inclusive",
"if i % 15 == 0 handles both at once"],
'def fizzbuzz(n):\n out = []\n for i in range(1, n + 1):\n'
' if i % 15 == 0:\n out.append("FizzBuzz")\n'
' elif i % 3 == 0:\n out.append("Fizz")\n'
' elif i % 5 == 0:\n out.append("Buzz")\n'
' else:\n out.append(i)\n return out\n',
"Basics"),
),
# ============================================================ 2. STRUCTURES
Lesson(
id="dicts", section="Data structures", title="Dictionaries",
theory="""A dict maps keys to values with O(1) average lookup. It is THE tool for turning
an O(n^2) loop into an O(n) one, which is most of what coding interviews reward.
ages = {"Ada": 36, "Linus": 54}
ages["Grace"] = 85 insert / overwrite
ages["Ada"] KeyError if missing
ages.get("Nobody") -> None (no crash)
ages.get("Nobody", 0) -> 0 (default)
"Ada" in ages membership test, O(1)
del ages["Ada"]
ages.keys() / .values() / .items()
Counting idioms, best to worst-known:
counts[c] = counts.get(c, 0) + 1 works everywhere
from collections import Counter
counts = Counter(text) batteries included
counts.most_common(3)
Grouping:
from collections import defaultdict
groups = defaultdict(list)
for word in words:
groups[len(word)].append(word)
Since Python 3.7 dicts keep insertion order.""",
example='''from collections import Counter, defaultdict
text = "mississippi"
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
print("manual:", counts)
print("Counter:", Counter(text))
print("top 2:", Counter(text).most_common(2))
words = ["fig", "pear", "kiwi", "plum", "apple"]
by_length = defaultdict(list)
for word in words:
by_length[len(word)].append(word)
print("grouped:", dict(by_length))
stock = {"apple": 3, "pear": 0, "fig": 7}
for name, qty in stock.items():
print(f"{name:<6} {qty}")
print("in stock:", [n for n, q in stock.items() if q > 0])
print("missing key safely:", stock.get("banana", 0))
''',
takeaway="Dict lookup is O(1). Counter and defaultdict save you real time.",
task=_t("dicts", "First non-repeating character", "first_unique",
"""Write first_unique(text) that returns the first character appearing exactly once.
Return None if every character repeats.
first_unique("swiss") -> "w"
first_unique("aabbcc") -> None
Do it in two passes: count first, then scan in order. That is O(n) — an O(n^2)
nested loop is the answer they do NOT want.""",
'def first_unique(text):\n # your code here\n pass\n',
[case("swiss", "w"), case("aabbcc", None), case("aabbc", "c"),
case("", None, hidden=True), case("x", "x", hidden=True),
case("leetcode", "l", hidden=True),
case("loveleetcode", "v", hidden=True)],
["First loop: build counts[ch] = counts.get(ch, 0) + 1",
"Second loop: over text again, return the first ch with counts[ch] == 1",
"Scan the TEXT in the second pass, not the dict — order matters"],
'def first_unique(text):\n counts = {}\n for ch in text:\n'
' counts[ch] = counts.get(ch, 0) + 1\n for ch in text:\n'
' if counts[ch] == 1:\n return ch\n return None\n',
"Hash map"),
),
Lesson(
id="sets", section="Data structures", title="Sets & tuples",
theory="""A set is an unordered collection of unique items with O(1) membership.
seen = set()
seen.add(3)
3 in seen O(1) <- vs O(n) for a list!
seen.discard(3) no error if absent
a | b union a & b intersection
a - b difference a ^ b symmetric difference
Turning `if x in big_list` into `if x in big_set` is the single most common
"make it faster" fix in coding tests.
A tuple is an immutable list: (3, 4). Because it is immutable it is hashable,
so tuples can be dict keys and set members — lists cannot.
point = (3, 4)
x, y = point unpacking
grid[(row, col)] = value tuple as a dict key
seen.add((row, col)) visited-cells pattern
Careful: {} is an empty DICT. An empty set is set().""",
example='''nums = [3, 1, 4, 1, 5, 9, 2, 6, 5]
print("unique:", set(nums))
print("has 4?", 4 in set(nums))
print("dupes removed, order kept:", list(dict.fromkeys(nums)))
a, b = {1, 2, 3, 4}, {3, 4, 5}
print("union:", a | b, "| intersection:", a & b)
print("only in a:", a - b, "| in exactly one:", a ^ b)
# the "have I seen this before" pattern
seen, duplicates = set(), []
for n in nums:
if n in seen:
duplicates.append(n)
seen.add(n)
print("duplicates:", duplicates)
# tuples: immutable, hashable, unpackable
point = (3, 4)
x, y = point
print("x =", x, "y =", y)
visited = {(0, 0), (1, 2)}
print("visited (1,2)?", (1, 2) in visited)
''',
takeaway="`in` on a set is O(1), on a list O(n). Tuples are hashable, lists are not.",
task=_t("sets", "Do two lists share anything?", "common_items",
"""Write common_items(a, b) that returns the values present in BOTH lists,
sorted ascending, with no duplicates.
common_items([1, 2, 2, 3], [3, 4, 2]) -> [2, 3]
common_items([1], [2]) -> []
Aim for O(n + m), not a nested loop.""",
'def common_items(a, b):\n # your code here\n pass\n',
[case(([1, 2, 2, 3], [3, 4, 2]), [2, 3]),
case(([1], [2]), []),
case(([5, 5], [5]), [5]),
case(([], [1, 2]), [], hidden=True),
case(([9, 8, 7], [7, 8, 9]), [7, 8, 9], hidden=True),
case(([-1, 0], [0, -1, 3]), [-1, 0], hidden=True)],
["set(a) & set(b) gives the shared values",
"sorted(...) turns the set into an ordered list"],
'def common_items(a, b):\n return sorted(set(a) & set(b))\n',
"Sets"),
),
Lesson(
id="comprehensions", section="Data structures", title="Comprehensions",
theory="""A comprehension is a loop that builds a container, written as one expression.
squares = [n * n for n in range(10)]
evens = [n for n in nums if n % 2 == 0]
labels = [f"#{i}" for i in ids]
lookup = {word: len(word) for word in words} dict comprehension
unique = {w.lower() for w in words} set comprehension
total = sum(n * n for n in nums) generator, no list built
Shape to remember:
[ WHAT_TO_KEEP for ITEM in ITERABLE if CONDITION ]
Nested (read it top-to-bottom, left-to-right, exactly like nested for loops):
flat = [x for row in matrix for x in row]
Conditional VALUE (this if/else goes in front, it is not a filter):
parity = ["even" if n % 2 == 0 else "odd" for n in nums]
Rule of thumb: if it does not fit comfortably on one line, use a real loop.""",
example='''nums = [1, 2, 3, 4, 5, 6, 7, 8]
print([n * n for n in nums])
print([n for n in nums if n % 2 == 0])
print(["even" if n % 2 == 0 else "odd" for n in nums])
words = ["fig", "banana", "kiwi"]
print({w: len(w) for w in words})
print({w[0] for w in words})
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("flattened:", [x for row in matrix for x in row])
print("diagonal:", [matrix[i][i] for i in range(len(matrix))])
print("transposed:", [list(col) for col in zip(*matrix)])
# generator expression: no intermediate list in memory
print("sum of squares:", sum(n * n for n in range(1000)))
print("any negative?", any(n < 0 for n in nums))
print("all positive?", all(n > 0 for n in nums))
''',
takeaway="[expr for item in it if cond]. Use a generator inside sum/any/all.",
task=_t("comprehensions", "Matrix column sums", "col_sums",
"""Write col_sums(matrix) that returns the sum of each COLUMN of a rectangular
list-of-lists.
col_sums([[1, 2], [3, 4], [5, 6]]) -> [9, 12]
col_sums([]) -> []
zip(*matrix) transposes a matrix — that plus a comprehension is a one-liner.""",
'def col_sums(matrix):\n # your code here\n pass\n',
[case([[1, 2], [3, 4], [5, 6]], [9, 12]),
case([[1, 2, 3]], [1, 2, 3]),
case([], []),
case([[0, 0], [0, 0]], [0, 0], hidden=True),
case([[-1, 5], [1, -5]], [0, 0], hidden=True),
case([[1], [2], [3], [4]], [10], hidden=True)],
["zip(*matrix) yields one tuple per column",
"[sum(col) for col in zip(*matrix)]",
"zip(*[]) is empty, so the empty case already works"],
'def col_sums(matrix):\n return [sum(col) for col in zip(*matrix)]\n',
"Lists"),
),
# ============================================================ 3. FUNCTIONS
Lesson(
id="functions", section="Functions & structure", title="Functions",
theory=""" def greet(name, greeting="Hello", *, loud=False):
text = f"{greeting}, {name}!"
return text.upper() if loud else text
* `greeting="Hello"` is a default — optional at the call site
* everything after `*` must be passed by keyword: greet("Ada", loud=True)
* a function with no `return` returns None
* `return a, b` returns a tuple; the caller can unpack it
THE classic Python trap — never use a mutable default:
def bad(item, bucket=[]): # the SAME list is reused across all calls!
bucket.append(item)
return bucket
def good(item, bucket=None):
if bucket is None:
bucket = []
bucket.append(item)
return bucket
Scope: a name assigned inside a function is local to it. Reading an outer
variable is fine; rebinding it needs `global` (which you almost never want —
pass it in and return it out instead).
Type hints are optional and never enforced, but they document intent:
def total(prices: list[float]) -> float: ...""",
example='''def greet(name, greeting="Hello", *, loud=False):
text = f"{greeting}, {name}!"
return text.upper() if loud else text
print(greet("Ada"))
print(greet("Linus", "Hi"))
print(greet("Grace", loud=True))
def min_max(nums):
return min(nums), max(nums)
low, high = min_max([4, 9, 1, 7])
print("low:", low, "high:", high)
def bad(item, bucket=[]):
bucket.append(item)
return bucket
print("bad call 1:", bad(1))
print("bad call 2:", bad(2), " <- the list survived!")
def good(item, bucket=None):
bucket = [] if bucket is None else bucket
bucket.append(item)
return bucket
print("good call 1:", good(1))
print("good call 2:", good(2))
def apply_twice(fn, value):
return fn(fn(value))
print("apply_twice:", apply_twice(lambda x: x * 3, 2))
''',
takeaway="Default arguments are evaluated once. Never default to [] or {}.",
task=_t("functions", "Flexible average", "average",
"""Write average(nums, ndigits=2) that returns the mean of a list of numbers,
rounded to `ndigits` decimal places. Return 0.0 for an empty list.
average([1, 2, 3, 4]) -> 2.5
average([1, 2], ndigits=0) -> 2.0 (round() returns a float here since we ask for a float)
Use round(value, ndigits). Guard the empty list before dividing!""",
'def average(nums, ndigits=2):\n # your code here\n pass\n',
[case([1, 2, 3, 4], 2.5), case([], 0.0), case(([1, 2, 4], 1), 2.3),
case([10], 10.0, hidden=True),
case(([1, 1, 1, 2], 3), 1.25, hidden=True),
case(([-2, 2], 2), 0.0, hidden=True)],
["if not nums: return 0.0",
"mean = sum(nums) / len(nums)",
"return round(mean, ndigits)"],
'def average(nums, ndigits=2):\n if not nums:\n return 0.0\n'
' return round(sum(nums) / len(nums), ndigits)\n',
"Functions"),
),
Lesson(
id="errors", section="Functions & structure", title="Errors & exceptions",
theory=""" try:
value = int(text)
except ValueError:
value = 0
else:
print("worked:", value) # only if no exception
finally:
print("always runs") # cleanup
Catch the SPECIFIC exception. A bare `except:` swallows typos, Ctrl-C and real
bugs alike.
Common ones you will meet:
ValueError int("abc")
TypeError "a" + 1
KeyError d["missing"]
IndexError lst[99]
ZeroDivisionError 1 / 0
AttributeError None.strip()
Raise your own when an argument makes no sense:
if n < 0:
raise ValueError(f"n must be >= 0, got {n}")
Python style is EAFP — "easier to ask forgiveness than permission". Trying and
catching is idiomatic; checking every precondition first is not.
try: # EAFP, pythonic
return d[key]
except KeyError:
return default""",
example='''def safe_int(text, default=0):
try:
return int(text)
except (ValueError, TypeError):
return default
print(safe_int("42"), safe_int("nope"), safe_int(None, -1))
def divide(a, b):
try:
result = a / b
except ZeroDivisionError:
print(" -> cannot divide by zero")
return None
else:
return result
finally:
print(" (divide finished)")
print("10/2 =", divide(10, 2))
print("10/0 =", divide(10, 0))
def sqrt_of(n):
if n < 0:
raise ValueError(f"n must be >= 0, got {n}")
return n ** 0.5
try:
sqrt_of(-4)
except ValueError as exc:
print("caught:", exc)
''',
takeaway="Catch specific exceptions. Raise ValueError for bad input.",
task=_t("errors", "Parse a list of numbers", "parse_numbers",
"""Write parse_numbers(items) that converts a list of strings to ints and returns
(numbers, bad_count):
* numbers — the list of successfully converted ints, in order
* bad_count — how many items could not be converted
parse_numbers(["1", "x", "3"]) -> ([1, 3], 1)
Return a real tuple. Do not let a bad item crash the function.""",
'def parse_numbers(items):\n # your code here\n pass\n',
[case(["1", "x", "3"], ([1, 3], 1)),
case(["10", "20"], ([10, 20], 0)),
case([], ([], 0)),
case(["a", "b"], ([], 2), hidden=True),
case(["-5", "3.5", "7"], ([-5, 7], 1), hidden=True),
case([" 8 ", "nope"], ([8], 1), hidden=True)],
["Loop, and wrap int(item) in try/except ValueError",
'int("3.5") raises ValueError — that is intended here',
"return numbers, bad builds the tuple automatically"],
'def parse_numbers(items):\n numbers, bad = [], 0\n for item in items:\n'
' try:\n numbers.append(int(item))\n'
' except (ValueError, TypeError):\n bad += 1\n'
' return numbers, bad\n',
"Errors"),
),
Lesson(
id="oop", section="Functions & structure", title="Classes & objects",
theory="""A class bundles data with the functions that work on it.
class Account:
def __init__(self, owner, balance=0): # runs on Account("Ada")
self.owner = owner # instance attributes
self.balance = balance
def deposit(self, amount): # self = this object
if amount <= 0:
raise ValueError("amount must be positive")
self.balance += amount
return self.balance
def __repr__(self): # how it prints
return f"Account({self.owner!r}, {self.balance})"
`self` is the first parameter of every instance method and Python passes it for
you: acc.deposit(50) calls deposit(acc, 50).
Dunder methods hook into the language:
__init__ construction __repr__ developer-facing text
__str__ user-facing text __len__ len(obj)
__eq__ == __lt__ < , which makes sort() work
Inheritance:
class Savings(Account):
def __init__(self, owner, balance=0, rate=0.02):
super().__init__(owner, balance)
self.rate = rate
For plain data bundles, reach for a dataclass — it writes __init__ and
__repr__ for you:
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int""",
example='''from dataclasses import dataclass
class Account:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
if amount <= 0:
raise ValueError("amount must be positive")
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
raise ValueError("insufficient funds")
self.balance -= amount
return self.balance
def __repr__(self):
return f"Account({self.owner!r}, {self.balance})"
acc = Account("Ada", 100)
acc.deposit(50)
print(acc, "| balance:", acc.balance)
try:
acc.withdraw(1000)
except ValueError as exc:
print("caught:", exc)
class Savings(Account):
def __init__(self, owner, balance=0, rate=0.02):
super().__init__(owner, balance)
self.rate = rate
def add_interest(self):
return self.deposit(self.balance * self.rate)
s = Savings("Grace", 1000)
s.add_interest()
print(s, "| rate:", s.rate)
@dataclass
class Point:
x: int
y: int
def dist(self):
return (self.x ** 2 + self.y ** 2) ** 0.5
p = Point(3, 4)
print(p, "| distance:", p.dist())
''',
takeaway="__init__ builds it, self is the object, super() reaches the parent.",
task=_t("oop", "A tiny Stack class", "make_stack",
"""Build a Stack class and a factory function make_stack(items) that returns a
Stack already filled with `items` (pushed left to right).
Your Stack needs:
* push(x) add on top
* pop() remove and return the top, or None if empty
* peek() return the top without removing it, or None if empty
* size() number of items
make_stack([1, 2, 3]).pop() -> 3
The tests call make_stack(...) and then poke at the object it returns.""",
'class Stack:\n def __init__(self):\n self.items = []\n\n'
' # add push / pop / peek / size here\n\n\n'
'def make_stack(items):\n # your code here\n pass\n',
[case([1, 2, 3], None, label="push 1,2,3 then peek/pop/size"),
case([], None, label="an empty stack must not crash"),
case([5], None, label="a single item"),
case([1, 2, 3, 4], None, hidden=True),
case(["a", "b"], None, hidden=True)],
["push -> self.items.append(x)",
"pop -> if not self.items: return None, else self.items.pop()",
"make_stack: build a Stack, loop over items, push each, return it"],
'class Stack:\n def __init__(self):\n self.items = []\n\n'
' def push(self, x):\n self.items.append(x)\n\n'
' def pop(self):\n return self.items.pop() if self.items else None\n\n'
' def peek(self):\n return self.items[-1] if self.items else None\n\n'
' def size(self):\n return len(self.items)\n\n\n'
'def make_stack(items):\n stack = Stack()\n for item in items:\n'
' stack.push(item)\n return stack\n',
"OOP", checker_src=STACK_CHECKER),
),
Lesson(
id="modules", section="Functions & structure", title="Modules & the standard library",
theory=""" import math math.sqrt(16)
from math import sqrt, pi sqrt(16)
import statistics as stats stats.mean(nums)
The batteries you actually reach for in a coding test:
collections Counter, defaultdict, deque, namedtuple
itertools accumulate, combinations, permutations, groupby, product
math gcd, sqrt, ceil, floor, inf, comb, isclose
heapq heappush/heappop — a priority queue (top-K problems)
bisect binary search into a sorted list
functools lru_cache (memoisation!), reduce
re regular expressions
deque is the one people forget: popping from the FRONT of a list is O(n), from
a deque it is O(1). That is the difference between passing and timing out on a
BFS / sliding-window problem.
from collections import deque
q = deque([1, 2, 3])
q.append(4); q.appendleft(0)
q.popleft() # O(1)
Every file you write is itself a module. `if __name__ == "__main__":` guards
code that should only run when the file is executed directly.""",
example='''import math
from collections import deque, Counter
from itertools import accumulate, combinations
from functools import lru_cache
import bisect, heapq
print("gcd(84, 36):", math.gcd(84, 36))
print("ceil(2.1):", math.ceil(2.1), "| floor(2.9):", math.floor(2.9))
print("comb(5, 2):", math.comb(5, 2))
q = deque([1, 2, 3])
q.appendleft(0)
print("deque:", q, "| popleft ->", q.popleft(), q)
print("running totals:", list(accumulate([1, 2, 3, 4])))
print("pairs:", list(combinations("abc", 2)))
print("counter:", Counter("banana").most_common())
nums = [1, 3, 5, 7, 9]
print("insert 6 at index:", bisect.bisect_left(nums, 6))
print("3 largest:", heapq.nlargest(3, [5, 1, 9, 3, 7]))
@lru_cache(maxsize=None)
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print("fib(60) instantly:", fib(60))
if __name__ == "__main__":
print("this file was run directly")
''',
takeaway="deque for O(1) front ops, lru_cache for free memoisation, Counter for tallies.",
task=_t("modules", "Top K frequent words", "top_k",
"""Write top_k(words, k) returning the k most frequent words, most frequent first.
Ties are broken alphabetically.
top_k(["a", "b", "a", "c", "b", "a"], 2) -> ["a", "b"]
collections.Counter does the counting; the tie-break needs a sort key of
(-count, word).""",
'from collections import Counter\n\n\n'
'def top_k(words, k):\n # your code here\n pass\n',
[case((["a", "b", "a", "c", "b", "a"], 2), ["a", "b"]),
case((["x"], 1), ["x"]),
case((["b", "a"], 2), ["a", "b"]),
case(([], 3), [], hidden=True),
case((["p", "q", "q", "p", "r"], 3), ["p", "q", "r"], hidden=True),
case((["z", "z", "y"], 1), ["z"], hidden=True)],
["counts = Counter(words)",
"ordered = sorted(counts, key=lambda w: (-counts[w], w))",
"return ordered[:k]"],
'from collections import Counter\n\n\n'
'def top_k(words, k):\n counts = Counter(words)\n'
' ordered = sorted(counts, key=lambda w: (-counts[w], w))\n'
' return ordered[:k]\n',
"Sorting"),
),
# ============================================= 4. INTERVIEW-SHAPED TECHNIQUES
Lesson(
id="complexity", section="Interview technique", title="Big-O in practice",
theory="""Codility and friends do not just check that your answer is right — they check
that it is fast enough. Learn to read your own code's cost.
O(1) a fixed number of steps d[key], lst[i], arithmetic
O(log n) halving each step binary search
O(n) one pass sum(lst), a single for loop
O(n log n) sort sorted(lst), lst.sort()
O(n^2) loop inside a loop two nested for loops over n
O(2^n) naive recursion over subsets the "too slow" cliff
Hidden costs people miss:
x in list O(n) but x in set / dict is O(1)
list.pop(0) O(n) but deque.popleft() is O(1)
list.insert(0,x) O(n)
s += "x" in loop O(n^2) build a list and "".join it
sorting O(n log n) — often the intended answer, not a failure
Rule of thumb for a 1-second limit: about 10^7-10^8 simple operations. So for
n = 100000, an O(n^2) solution (10^10 steps) will time out; O(n log n) is fine.
The interview move: state the complexity out loud, then say what would improve
it. "This is O(n^2); a hash set makes it O(n)." That sentence is worth points.""",
example='''import time
n = 20000
data = list(range(n))