ЁЯПл The SchoolтА║ЁЯзо DSAтА║ЁЯЧДя╕П рдзрдбрд╛ 02 тАФ Arrays рдЖрдгрд┐ strings: рд▓реЙрдХрд░реНрд╕рдЪреА рд░рд╛рдВрдЧ
ЁЯЦ╝я╕П See the drawing + lab ЁЯПа Course home ЁЯМ┐ Branch on GitHub тЬПя╕П View source
ЁЯЦ╝я╕П рдЖрдХреГрддреА рдЖрдгрд┐ labThe drawing + lab рдкреВрд░реНрдг рдкрд╛рдирд╛рд╡рд░ рдЙрдШрдбрд╛ тЖЧOpen full page тЖЧ

ЁЯЧДя╕П рдзрдбрд╛ 02 тАФ Arrays рдЖрдгрд┐ strings: рд▓реЙрдХрд░реНрд╕рдЪреА рд░рд╛рдВрдЧ

ЁЯУН рддреБрдореНрд╣реА рдЗрдереЗ рдЖрд╣рд╛рдд: 12 рдкреИрдХреА рдзрдбрд╛ 02 ┬╖ рдкреБрдвреЗ: lesson-03-linked-lists


ЁЯУж рдпрд╛ рдмреНрд░рдБрдЪрдордзреНрдпреЗ рдХрд╛рдп рдЖрд╣реЗ

рдзрдбреЗ 01тАУ02. Python рдЪреНрдпрд╛ list рдорд╛рдЧрдЪрд╛ structure, рддреЛ рдХрд╢рд╛рдд рдЬрд▓рдж рдЖрдгрд┐ рдХрд╢рд╛рдд рд╣рд│реВ рдЖрд╣реЗ, рдЖрдгрд┐ рдореБрд▓рд╛рдЦрддреАрддрд▓реНрдпрд╛ рдПрдХ рддреГрддреАрдпрд╛рдВрд╢ рдкреНрд░рд╢реНрдирд╛рдВрдЪреА рдЙрддреНрддрд░реЗ рджреЗрдгрд╛рд░реЗ рджреЛрди array patterns.

ЁЯзТ 5 рд╡рд░реНрд╖рд╛рдВрдЪреНрдпрд╛ рдореБрд▓рд╛рд▓рд╛ рд╕рдордЬрд╛рд╡рд▓реНрдпрд╛рд╕рд╛рд░рдЦреЗ

рдХреНрд░рдорд╛рдВрдХ рдЕрд╕рд▓реЗрд▓реНрдпрд╛ lockers рдЪреА рд░рд╛рдВрдЧ, рд╕рдЧрд│реЗ рд╕рдорд╛рди рдЖрдХрд╛рд░рд╛рдЪреЗ, рд╢реЗрдЬрд╛рд░реА-рд╢реЗрдЬрд╛рд░реА. locker 17 рд╣рд╡рд╛? рдереЗрдЯ рддрд┐рдереЗ рдЬрд╛ тАФ рдПрдХ step, рд░рд╛рдВрдЧреЗрдд 20 lockers рдЕрд╕реЛрдд рдХреА 2 рдХреЛрдЯреА, рдХрд╛рд░рдг рддреЛ рдХреБрдареЗ рдЖрд╣реЗ рд╣реЗ рддреБрдореНрд╣реА рдореЛрдЬреВ рд╢рдХрддрд╛. рдкрдг рд╕реБрд░реБрд╡рд╛рддреАрд▓рд╛ рдПрдХ рдирд╡рд╛ locker рдШреБрд╕рд╡рд▓рд╛ рддрд░ рддреНрдпрд╛рдЪреНрдпрд╛рдирдВрддрд░рдЪреЗ рд╕рдЧрд│реЗ рдПрдХ рдЬрд╛рдЧрд╛ рд╕рд░рдХрддрд╛рдд: рд░рд╛рдВрдЧ рдЬрд┐рддрдХреА рд▓рд╛рдВрдм, рддрд┐рддрдХреА рд╕рд░рдХрд╡рд╛рд╕рд░рдХрд╡реА рдЬрд╛рд╕реНрдд. рд╢реЗрд╡рдЯреА рдЬреЛрдбрдгреЗ рд╕реНрд╡рд╕реНрдд рдЖрд╣реЗ тАФ рд░рд╛рдВрдЧ рднрд░реЗрдкрд░реНрдпрдВрдд; рдордЧ рдХрд╛рд│рдЬреАрд╡рд╛рд╣реВ рдореЛрдареА рд░рд╛рдВрдЧ рдмрд╛рдВрдзрддреЛ рдЖрдгрд┐ рд╕рдЧрд│реЗ рд╣рд▓рд╡рддреЛ (рдХреНрд╡рдЪрд┐рддрдЪ, рдореНрд╣рдгреВрди рд╕рд░рд╛рд╕рд░реА рдЕрдЬреВрдирд╣реА рд╕реНрд╡рд╕реНрдд).

ЁЯЧ║я╕П рдЖрдХреГрддреА

flowchart LR
  L["[0] Aishwarya | [1] Katrina | [2] Dipika | [3] Meera | тАж | [17] Zoya"]
  L -->|"lockers[17]"| O["one step тАФ O(1)"]
  L -->|"insert(0, x)"| S["everyone shifts тАФ O(n)"]

тЭУ рдХрд╛рдп

ЁЯдФ рдХрд╛

Arrays default рдЕрд╕рддрд╛рдд рдХрд╛рд░рдг programs рдмрд╣реБрддреЗрдХ рд╡реЗрд│рд╛ рд╡рд╛рдЪрдирдЪ рдХрд░рддрд╛рдд, рдЖрдгрд┐ рд╕рд▓рдЧ memory CPUs рдирд╛ рдЖрд╡рдбрддреЗ (caches). рдХреЛрдгрддреА operations O(n) рдЖрд╣реЗрдд рд╣реЗ рдиреЗрдордХреЗ рдорд╛рд╣реАрдд рдЕрд╕рд▓реЗ рдХреА list рдЪреБрдХреАрдЪреЗ рд╕рд╛рдзрди рдХрдзреА рдЖрд╣реЗ рд╣реЗ рдХрд│рддреЗ тАФ рдЖрдгрд┐ nested loop рдРрд╡рдЬреА рд╣реЗ рджреЛрди patterns рдХрдзреА рд╡рд╛рдкрд░рд╛рдпрдЪреЗ рддреЗрд╣реА.

ЁЯФз рдХрд╕реЗ (рдпрд╛ repo рдордзреНрдпреЗ)

two_sum_sorted рджреЛрди indices рдЖрддрд▓реНрдпрд╛ рджрд┐рд╢реЗрдиреЗ рдЪрд╛рд▓рд╡рддреЛ; best_window рдЪрд╛рд▓реВ рдмреЗрд░реАрдЬ рдареЗрд╡рддреЛ рдЖрдгрд┐ рд╕рд░рдХрддреЛ. demo.py arrays рд╡реАрд╕ рд▓рд╛рдЦ items рд╡рд░ insert(0, тАж) рдЖрдгрд┐ append рдпрд╛рдВрдЪрд╛ рд╡реЗрд│ рдореЛрдЬрддреЛ.

ЁЯзк рдХрд░реВрди рдкрд╛рд╣рд╛

python3 dsa/demo.py arrays
python3 - <<'EOF'
import sys; sys.path.insert(0, "dsa"); from algorithms import two_sum_sorted, best_window
print(two_sum_sorted([1, 3, 4, 6, 8, 11], 14))        # (1, 5): 3 + 11 тАФ the two monitors met after one step each
print(best_window([2, 1, 5, 1, 3, 2], 3))            # (9, 2): 5 + 1 + 3
s = ""
for i in range(5): s += str(i)                       # each += copies the string: O(n┬▓) in a loop
print(s, "".join(str(i) for i in range(5)))          # join builds once: O(n)
EOF

тЬЕ рддрдкрд╛рд╕рд╛ тАФ рддреБрдореНрд╣рд╛рд▓рд╛ рдХрд╛рдп рджрд┐рд╕рд╛рдпрд▓рд╛ рд╣рд╡реЗ

lockers[17] = 17 рдПрдХрд╛ step рдордзреНрдпреЗ; рд╡реАрд╕ рд▓рд╛рдЦ items рд╡рд░ рд╕реБрд░реБрд╡рд╛рддреАрдЪрд╛ insert milliseconds рдШреЗрддреЛ рддрд░ append microseconds; window рдЪреЗ рдЙрддреНрддрд░ рджрд┐рд╡рд╕ 1 рд▓рд╛ 19; two-sum рдЕрд╢рд╛ indices рдЪреА рдЬреЛрдбреА рдкрд░рдд рдХрд░рддреЛ рдЬреНрдпрд╛рдВрдЪреНрдпрд╛ values рдЪреА рдмреЗрд░реАрдЬ target рдЗрддрдХреА рд╣реЛрддреЗ.

ЁЯПБ рддреБрдореНрд╣реА рдЖрддреНрддрд╛рдЪ рдХрд╛рдп рд╕рд┐рджреНрдз рдХреЗрд▓реЗ

рддреБрдореНрд╣реА рдкреНрд░рддреНрдпреЗрдХ list operation рдЪрд╛ рдЦрд░реНрдЪ рд╕рд╛рдВрдЧреВ рд╢рдХрддрд╛, рдЖрдгрд┐ data рдХреНрд░рдорд╛рдиреЗ рдХрд┐рдВрд╡рд╛ рд░рд╛рдВрдЧреЗрдд рдЕрд╕реЗрд▓ рддреЗрд╡реНрд╣рд╛ n┬▓ loop рдРрд╡рдЬреА two pointers рдХрд┐рдВрд╡рд╛ window рд╡рд╛рдкрд░реВ рд╢рдХрддрд╛.

тЪая╕П рдиреЗрд╣рдореАрдЪреНрдпрд╛ рдЪреБрдХрд╛

ЁЯПн рдкреНрд░рддреНрдпрдХреНрд╖ рд╡рд╛рдкрд░рд╛рдд рд╣реЗ рдХрд╛ рдорд╣рддреНрддреНрд╡рд╛рдЪреЗ: buffers, batches, rows, pixels, time series тАФ рдЬрд╡рд│рдЬрд╡рд│ рд╕рдЧрд│реЗ array рдЕрд╕рддреЗ, рдЖрдгрд┐ рдЬрд╡рд│рдЬрд╡рд│ рдкреНрд░рддреНрдпреЗрдХ hot loop рдореНрд╣рдгрдЬреЗ рдЪрд╛рдВрдЧрд▓рд╛ рдХрд┐рдВрд╡рд╛ рд╡рд╛рдИрдЯ рдХреЗрд▓реЗрд▓рд╛ array pattern.

тПня╕П рдкреБрдвреЗ

рдзрдбрд╛ 03 тАФ linked lists: рдЦрдЬрд┐рдиреНрдпрд╛рдЪрд╛ рд╢реЛрдз, рдЬрд┐рдереЗ рдЪрд┐рдареНрдареА рдЬреЛрдбрдгреЗ рдлреБрдХрдЯ рдЖрд╣реЗ рдЖрдгрд┐ рд╢реЛрдзрдгреЗ рдирд╛рд╣реА.

ЁЯЧДя╕П Lesson 02 тАФ Arrays & strings: the row of lockers

ЁЯУН You are here: Lesson 02 of 12 ┬╖ Next: lesson-03-linked-lists


ЁЯУж What's in this branch

Lessons 01тАУ02. The structure behind Python's list, what it is fast and slow at, and the two array patterns that answer a third of interview questions.

ЁЯзТ Explain like I'm 5

A row of numbered lockers, all the same size, side by side. Want locker 17? Walk straight to it тАФ one step, whether the row has 20 lockers or 20 million, because you can compute where it is. But squeeze a new locker in at the front and everyone after it shifts one place: the longer the row, the longer the shuffle. Adding at the end is cheap тАФ until the row is full and the caretaker builds a bigger one and moves everything (rarely, so on average still cheap).

ЁЯЧ║я╕П Diagram

flowchart LR
  L["[0] Aishwarya | [1] Katrina | [2] Dipika | [3] Meera | тАж | [17] Zoya"]
  L -->|"lockers[17]"| O["one step тАФ O(1)"]
  L -->|"insert(0, x)"| S["everyone shifts тАФ O(n)"]

тЭУ What

ЁЯдФ Why

Arrays are the default because reading is what programs mostly do, and contiguous memory is what CPUs love (caches). Knowing exactly which operations are O(n) tells you when a list is the wrong tool тАФ and when to reach for the two patterns instead of a nested loop.

ЁЯФз How (in this repo)

two_sum_sorted walks two indices inward; best_window keeps a running sum and slides. demo.py arrays times insert(0, тАж) against append on two million items.

ЁЯзк Try it

python3 dsa/demo.py arrays
python3 - <<'EOF'
import sys; sys.path.insert(0, "dsa"); from algorithms import two_sum_sorted, best_window
print(two_sum_sorted([1, 3, 4, 6, 8, 11], 14))        # (1, 5): 3 + 11 тАФ the two monitors met after one step each
print(best_window([2, 1, 5, 1, 3, 2], 3))            # (9, 2): 5 + 1 + 3
s = ""
for i in range(5): s += str(i)                       # each += copies the string: O(n┬▓) in a loop
print(s, "".join(str(i) for i in range(5)))          # join builds once: O(n)
EOF

тЬЕ Verify тАФ what you should see

lockers[17] = 17 in one step; the front insert on two million takes milliseconds while append takes microseconds; the window answer is 19 at day 1; two-sum returns a pair of indices whose values add to the target.

ЁЯПБ What you just proved

You can name the cost of every list operation and replace an n┬▓ loop with two pointers or a window when the data is in order or in a line.

тЪая╕П Common mistakes

ЁЯПн Why this matters in production: buffers, batches, rows, pixels, time series тАФ nearly everything is an array, and nearly every hot loop is an array pattern done well or badly.

тПня╕П Next

Lesson 03 тАФ linked lists: the treasure hunt, where adding a note is free and finding one is not.

тЖР Previouswhy dsaNext тЖТlinked lists

This page is the lesson's README from the lesson-02-arrays branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.