Skip to content

Commit f8b84c5

Browse files
Docs: Add Space column to all complexity tables
Standardize complexity tables to include Time, Space, and Notes columns: - bisect.md, collections.md (all sections) - list.md, dict.md, set.md, tuple.md, str.md - frozenset.md, range.md, bytes.md, bool.md Merged separate Time/Space sections into unified tables for consistency. Co-Authored-By: Amp <amp@ampcode.com>
1 parent 2cd671d commit f8b84c5

11 files changed

Lines changed: 316 additions & 391 deletions

File tree

docs/builtins/bool.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ The `bool` type is a subclass of `int` representing truth values: `True` and `Fa
1919

2020
## Logical Operations
2121

22-
| Operation | Time | Notes |
23-
|-----------|------|-------|
24-
| `x and y` | O(1) | Short-circuit: returns first falsy or last value |
25-
| `x or y` | O(1) | Short-circuit: returns first truthy or last value |
26-
| `not x` | O(1) | Logical negation |
22+
| Operation | Time | Space | Notes |
23+
|-----------|------|-------|-------|
24+
| `x and y` | O(1) | O(1) | Short-circuit: returns first falsy or last value |
25+
| `x or y` | O(1) | O(1) | Short-circuit: returns first truthy or last value |
26+
| `not x` | O(1) | O(1) | Logical negation |
2727

2828
### Short-circuit Evaluation
2929

docs/builtins/bytes.md

Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,82 @@
22

33
The `bytes` type is an immutable sequence of bytes, while `bytearray` is the mutable equivalent.
44

5-
## Time Complexity
5+
## Complexity Reference
66

77
### Bytes
88

9-
| Operation | Time | Notes |
10-
|-----------|------|-------|
11-
| `len()` | O(1) | Direct lookup |
12-
| `access[i]` | O(1) | Direct indexing |
13-
| `in` (membership) | O(n) | Linear search |
14-
| **Search** |||
15-
| `find(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | n = bytes length, m = pattern length |
16-
| `rfind(sub)` | O(n*m) worst case | Search from right |
17-
| `index(sub)` | O(n) | Like find() but raises ValueError |
18-
| `rindex(sub)` | O(n*m) worst case | Like rfind() but raises ValueError |
19-
| `count(sub)` | O(n) | Count non-overlapping occurrences |
20-
| `startswith(prefix)` | O(m) | m = prefix length |
21-
| `endswith(suffix)` | O(m) | m = suffix length |
22-
| **Replace/Translate** |||
23-
| `replace(old, new)` | O(n) | Creates new bytes object |
24-
| `translate(table)` | O(n) | Single pass with table lookup |
25-
| `maketrans(from, to)` | O(k) | k = mapping size; static method |
26-
| `expandtabs(tabsize)` | O(n) | Replace tabs with spaces |
27-
| `removeprefix(prefix)` | O(n) | Returns slice if prefix matches (Python 3.9+) |
28-
| `removesuffix(suffix)` | O(n) | Returns slice if suffix matches (Python 3.9+) |
29-
| **Split/Join** |||
30-
| `split(sep)` | O(n) | Single pass |
31-
| `rsplit(sep)` | O(n) | Split from right |
32-
| `splitlines()` | O(n) | Split on line boundaries |
33-
| `partition(sep)` | O(n) | Split into 3-tuple at first sep |
34-
| `rpartition(sep)` | O(n) | Split into 3-tuple at last sep |
35-
| `join(iterable)` | O(n) | n = total output length |
36-
| **Case Conversion** |||
37-
| `upper()` | O(n) | ASCII uppercase |
38-
| `lower()` | O(n) | ASCII lowercase |
39-
| `capitalize()` | O(n) | Uppercase first, lowercase rest |
40-
| `title()` | O(n) | Titlecase words |
41-
| `swapcase()` | O(n) | Swap upper/lower |
42-
| **Stripping** |||
43-
| `strip(chars)` | O(n) | Remove from both ends |
44-
| `lstrip(chars)` | O(n) | Remove from left |
45-
| `rstrip(chars)` | O(n) | Remove from right |
46-
| **Padding/Alignment** |||
47-
| `center(width)` | O(n) | Pad both sides |
48-
| `ljust(width)` | O(n) | Pad right side |
49-
| `rjust(width)` | O(n) | Pad left side |
50-
| `zfill(width)` | O(n) | Pad with zeros |
51-
| **Predicates** |||
52-
| `isalnum()` | O(n) | Check alphanumeric |
53-
| `isalpha()` | O(n) | Check alphabetic |
54-
| `isascii()` | O(n) | Check ASCII (Python 3.7+) |
55-
| `isdigit()` | O(n) | Check digit chars |
56-
| `islower()` | O(n) | Check lowercase |
57-
| `isspace()` | O(n) | Check whitespace |
58-
| `istitle()` | O(n) | Check titlecase |
59-
| `isupper()` | O(n) | Check uppercase |
60-
| **Encoding** |||
61-
| `decode(encoding)` | O(n) | Convert to string |
62-
| `hex()` | O(n) | Convert to hex string |
63-
| `fromhex(string)` | O(n) | Create bytes from hex; class method |
9+
| Operation | Time | Space | Notes |
10+
|-----------|------|-------|-------|
11+
| `len()` | O(1) | O(1) | Direct lookup |
12+
| `access[i]` | O(1) | O(1) | Direct indexing |
13+
| `in` (membership) | O(n) | O(1) | Linear search |
14+
| **Search** ||||
15+
| `find(sub)` | O(n + m) avg | O(1) | n = bytes length, m = pattern length |
16+
| `rfind(sub)` | O(n*m) worst | O(1) | Search from right |
17+
| `index(sub)` | O(n) | O(1) | Like find() but raises ValueError |
18+
| `rindex(sub)` | O(n*m) worst | O(1) | Like rfind() but raises ValueError |
19+
| `count(sub)` | O(n) | O(1) | Count non-overlapping occurrences |
20+
| `startswith(prefix)` | O(m) | O(1) | m = prefix length |
21+
| `endswith(suffix)` | O(m) | O(1) | m = suffix length |
22+
| **Replace/Translate** ||||
23+
| `replace(old, new)` | O(n) | O(n) | Creates new bytes object |
24+
| `translate(table)` | O(n) | O(n) | Single pass with table lookup |
25+
| `maketrans(from, to)` | O(k) | O(k) | k = mapping size; static method |
26+
| `expandtabs(tabsize)` | O(n) | O(n) | Replace tabs with spaces |
27+
| `removeprefix(prefix)` | O(n) | O(n) | Returns slice if prefix matches (Python 3.9+) |
28+
| `removesuffix(suffix)` | O(n) | O(n) | Returns slice if suffix matches (Python 3.9+) |
29+
| **Split/Join** ||||
30+
| `split(sep)` | O(n) | O(n) | Single pass |
31+
| `rsplit(sep)` | O(n) | O(n) | Split from right |
32+
| `splitlines()` | O(n) | O(n) | Split on line boundaries |
33+
| `partition(sep)` | O(n) | O(n) | Split into 3-tuple at first sep |
34+
| `rpartition(sep)` | O(n) | O(n) | Split into 3-tuple at last sep |
35+
| `join(iterable)` | O(n) | O(n) | n = total output length |
36+
| **Case Conversion** ||||
37+
| `upper()` | O(n) | O(n) | ASCII uppercase |
38+
| `lower()` | O(n) | O(n) | ASCII lowercase |
39+
| `capitalize()` | O(n) | O(n) | Uppercase first, lowercase rest |
40+
| `title()` | O(n) | O(n) | Titlecase words |
41+
| `swapcase()` | O(n) | O(n) | Swap upper/lower |
42+
| **Stripping** ||||
43+
| `strip(chars)` | O(n) | O(n) | Remove from both ends |
44+
| `lstrip(chars)` | O(n) | O(n) | Remove from left |
45+
| `rstrip(chars)` | O(n) | O(n) | Remove from right |
46+
| **Padding/Alignment** ||||
47+
| `center(width)` | O(n) | O(n) | Pad both sides |
48+
| `ljust(width)` | O(n) | O(n) | Pad right side |
49+
| `rjust(width)` | O(n) | O(n) | Pad left side |
50+
| `zfill(width)` | O(n) | O(n) | Pad with zeros |
51+
| **Predicates** ||||
52+
| `isalnum()` | O(n) | O(1) | Check alphanumeric |
53+
| `isalpha()` | O(n) | O(1) | Check alphabetic |
54+
| `isascii()` | O(n) | O(1) | Check ASCII (Python 3.7+) |
55+
| `isdigit()` | O(n) | O(1) | Check digit chars |
56+
| `islower()` | O(n) | O(1) | Check lowercase |
57+
| `isspace()` | O(n) | O(1) | Check whitespace |
58+
| `istitle()` | O(n) | O(1) | Check titlecase |
59+
| `isupper()` | O(n) | O(1) | Check uppercase |
60+
| **Encoding** ||||
61+
| `decode(encoding)` | O(n) | O(n) | Convert to string |
62+
| `hex()` | O(n) | O(n) | Convert to hex string |
63+
| `fromhex(string)` | O(n) | O(n) | Create bytes from hex; class method |
6464

6565
*Note: bytes is immutable, so operations that appear to modify return new objects (O(n) space).*
6666

6767
### Bytearray
6868

69-
| Operation | Time | Notes |
70-
|-----------|------|-------|
71-
| `append(x)` | O(1)* | Amortized, may resize |
72-
| `insert(i, x)` | O(n) | Shift elements |
73-
| `extend(iterable)` | O(k) | k = length |
74-
| `pop()` | O(1) | Remove last |
75-
| `pop(0)` | O(n) | Shift remaining |
76-
| `remove(x)` | O(n) | Search and remove |
77-
| `clear()` | O(n) | Deallocate |
78-
| `copy()` | O(n) | Shallow copy |
79-
| `reverse()` | O(n) | Reverse in-place |
80-
| `resize(n)` | O(n) | Resize to n bytes; may truncate or zero-fill |
81-
82-
## Space Complexity
83-
84-
| Operation | Space |
85-
|-----------|-------|
86-
| `bytes()` copy | O(n) |
87-
| `bytearray()` operations | O(1) amortized |
88-
| `decode()` | O(n) for string |
89-
| `join()` | O(n) result |
69+
| Operation | Time | Space | Notes |
70+
|-----------|------|-------|-------|
71+
| `append(x)` | O(1)* | O(1) | Amortized, may resize |
72+
| `insert(i, x)` | O(n) | O(1) | Shift elements |
73+
| `extend(iterable)` | O(k) | O(k) | k = length |
74+
| `pop()` | O(1) | O(1) | Remove last |
75+
| `pop(0)` | O(n) | O(1) | Shift remaining |
76+
| `remove(x)` | O(n) | O(1) | Search and remove |
77+
| `clear()` | O(n) | O(1) | Deallocate |
78+
| `copy()` | O(n) | O(n) | Shallow copy |
79+
| `reverse()` | O(n) | O(1) | Reverse in-place |
80+
| `resize(n)` | O(n) | O(1) | Resize to n bytes; may truncate or zero-fill |
9081

9182
## Implementation Details
9283

docs/builtins/dict.md

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,29 @@
22

33
The `dict` type is a mutable mapping that stores key-value pairs. It's implemented as a hash table in CPython.
44

5-
## Time Complexity
6-
7-
| Operation | Time | Notes |
8-
|-----------|------|-------|
9-
| `len()` | O(1) | Direct count |
10-
| `access[key]` | O(1) avg, O(n) worst | Hash lookup; worst case with collisions |
11-
| `set[key] = value` | O(1) amortized | Hash insertion; may trigger resize |
12-
| `del[key]` | O(1) avg, O(n) worst | Hash deletion |
13-
| `key in dict` | O(1) avg, O(n) worst | Hash lookup |
14-
| `get(key)` | O(1) avg, O(n) worst | Hash lookup |
15-
| `pop(key)` | O(1) avg, O(n) worst | Hash deletion |
16-
| `clear()` | O(n) | Must deallocate all entries |
17-
| `keys()` | O(1) | View object (O(n) to iterate) |
18-
| `values()` | O(1) | View object (O(n) to iterate) |
19-
| `items()` | O(1) | View object (O(n) to iterate) |
20-
| `copy()` | O(n) | Shallow copy of all pairs |
21-
| `update(other)` | O(k) | k = len(other), amortized |
22-
| `setdefault(key, val)` | O(1) avg | Hash lookup + insert |
23-
| `fromkeys(keys)` | O(k) | k = len(keys) |
24-
| `popitem()` | O(1) | Remove last inserted pair (LIFO since 3.7) |
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `len()` | O(1) | O(1) | Direct count |
10+
| `access[key]` | O(1) avg, O(n) worst | O(1) | Hash lookup; worst case with collisions |
11+
| `set[key] = value` | O(1) amortized | O(1) | Hash insertion; may trigger resize |
12+
| `del[key]` | O(1) avg, O(n) worst | O(1) | Hash deletion |
13+
| `key in dict` | O(1) avg, O(n) worst | O(1) | Hash lookup |
14+
| `get(key)` | O(1) avg, O(n) worst | O(1) | Hash lookup |
15+
| `pop(key)` | O(1) avg, O(n) worst | O(1) | Hash deletion |
16+
| `clear()` | O(n) | O(1) | Must deallocate all entries |
17+
| `keys()` | O(1) | O(1) | View object (O(n) to iterate) |
18+
| `values()` | O(1) | O(1) | View object (O(n) to iterate) |
19+
| `items()` | O(1) | O(1) | View object (O(n) to iterate) |
20+
| `copy()` | O(n) | O(n) | Shallow copy of all pairs |
21+
| `update(other)` | O(k) | O(1) | k = len(other), amortized; modifies in place |
22+
| `setdefault(key, val)` | O(1) avg | O(1) | Hash lookup + insert |
23+
| `fromkeys(keys)` | O(k) | O(k) | k = len(keys) |
24+
| `popitem()` | O(1) | O(1) | Remove last inserted pair (LIFO since 3.7) |
2525

2626
*Note: O(1) average case assumes good hash distribution. Worst case O(n) occurs with pathological hash collisions, which is rare with Python's randomized hashing.*
2727

28-
## Space Complexity
29-
30-
| Operation | Space |
31-
|-----------|-------|
32-
| `copy()` | O(n) |
33-
| `update()` | O(1) (modifies in place) |
34-
| General storage | O(n) for n key-value pairs |
35-
3628
## Implementation Details
3729

3830
### Hash Table Structure

docs/builtins/frozenset.md

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22

33
The `frozenset` type is an immutable set that can be hashed and used as a dictionary key or set member.
44

5-
## Time Complexity
6-
7-
| Operation | Time | Notes |
8-
|-----------|------|-------|
9-
| `frozenset(iterable)` | O(n) | Create from iterable |
10-
| `len()` | O(1) | Direct count |
11-
| `in` (membership) | O(1) avg, O(n) worst | Hash lookup; worst case with hash collisions |
12-
| `union(\|)` | O(n+m) | Combine sets |
13-
| `intersection(&)` | O(min(n,m)) | Common elements |
14-
| `difference(-)` | O(n) | Elements in first |
15-
| `symmetric_diff(^)` | O(n+m) | Exclusive elements |
16-
| `issubset()` | O(n) | Check containment |
17-
| `issuperset()` | O(m) | Check containment |
18-
| `isdisjoint()` | O(min(n,m)) | Check overlap |
19-
| `copy()` | O(1) | Shallow copy |
20-
| `hash()` | O(n) first call, O(1) cached | Hash value computed once, then cached |
21-
| `frozenset(set)` | O(n) | Convert from set |
22-
23-
## Space Complexity
24-
25-
| Operation | Space |
26-
|-----------|-------|
27-
| Creation | O(n) for n elements |
28-
| Copy | O(1) (same object) |
29-
| Union | O(n+m) for result |
30-
| Intersection | O(min(n,m)) for result |
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `frozenset(iterable)` | O(n) | O(n) | Create from iterable |
10+
| `len()` | O(1) | O(1) | Direct count |
11+
| `in` (membership) | O(1) avg, O(n) worst | O(1) | Hash lookup; worst case with hash collisions |
12+
| `union(\|)` | O(n+m) | O(n+m) | Combine sets |
13+
| `intersection(&)` | O(min(n,m)) | O(min(n,m)) | Common elements |
14+
| `difference(-)` | O(n) | O(n) | Elements in first |
15+
| `symmetric_difference(^)` | O(n+m) | O(n+m) | Exclusive elements |
16+
| `issubset()` | O(n) | O(1) | Check containment |
17+
| `issuperset()` | O(m) | O(1) | Check containment |
18+
| `isdisjoint()` | O(min(n,m)) | O(1) | Check overlap |
19+
| `copy()` | O(1) | O(1) | Returns same object (immutable) |
20+
| `hash()` | O(n) first call, O(1) cached | O(1) | Hash value computed once, then cached |
21+
| `frozenset(set)` | O(n) | O(n) | Convert from set |
3122

3223
## Implementation Details
3324

docs/builtins/list.md

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,29 @@
22

33
The `list` type is a mutable, ordered sequence. It's implemented as a dynamic array in CPython.
44

5-
## Time Complexity
6-
7-
| Operation | Time | Notes |
8-
|-----------|------|-------|
9-
| `len()` | O(1) | Direct lookup |
10-
| `access[i]` | O(1) | Direct indexing |
11-
| `append(x)` | O(1) amortized | May resize; worst case O(n) when reallocation needed |
12-
| `insert(0, x)` | O(n) | Must shift all elements |
13-
| `insert(i, x)` | O(n-i) | Shift elements from index i |
14-
| `remove(x)` | O(n) | Must search and shift |
15-
| `pop()` | O(1) | Remove last element |
16-
| `pop(0)` | O(n) | Shift remaining elements |
17-
| `pop(i)` | O(n-i) | Shift elements after i |
18-
| `clear()` | O(n) | Deallocate memory |
19-
| `index(x)` | O(n) | Linear search |
20-
| `count(x)` | O(n) | Linear scan |
21-
| `sort()` | O(n log n) avg/worst, O(n) best | Timsort/Powersort; adaptive for partially sorted data |
22-
| `reverse()` | O(n) | In-place reversal |
23-
| `copy()` | O(n) | Shallow copy |
24-
| `extend(iterable)` | O(k) | k = length of iterable |
25-
| `in` (membership) | O(n) | Linear search |
26-
| `x + y` (concatenation) | O(m+n) | m, n are lengths |
27-
| `[::2]` (slicing) | O(k) | k = slice length |
28-
29-
## Space Complexity
30-
31-
| Operation | Space |
32-
|-----------|-------|
33-
| `append()` | O(1) amortized; O(n) worst case on resize |
34-
| `extend()` | O(k); may trigger O(n) resize |
35-
| `sort()` | O(n) auxiliary |
36-
| `copy()` | O(n) |
37-
| `slice` | O(k) for new list |
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `len()` | O(1) | O(1) | Direct lookup |
10+
| `access[i]` | O(1) | O(1) | Direct indexing |
11+
| `append(x)` | O(1) amortized | O(1) amortized | May resize; worst case O(n) when reallocation needed |
12+
| `insert(0, x)` | O(n) | O(1) | Must shift all elements |
13+
| `insert(i, x)` | O(n-i) | O(1) | Shift elements from index i |
14+
| `remove(x)` | O(n) | O(1) | Must search and shift |
15+
| `pop()` | O(1) | O(1) | Remove last element |
16+
| `pop(0)` | O(n) | O(1) | Shift remaining elements |
17+
| `pop(i)` | O(n-i) | O(1) | Shift elements after i |
18+
| `clear()` | O(n) | O(1) | Deallocate memory |
19+
| `index(x)` | O(n) | O(1) | Linear search |
20+
| `count(x)` | O(n) | O(1) | Linear scan |
21+
| `sort()` | O(n log n) avg/worst, O(n) best | O(n) | Timsort/Powersort; adaptive for partially sorted data |
22+
| `reverse()` | O(n) | O(1) | In-place reversal |
23+
| `copy()` | O(n) | O(n) | Shallow copy |
24+
| `extend(iterable)` | O(k) | O(k) | k = length of iterable; may trigger O(n) resize |
25+
| `in` (membership) | O(n) | O(1) | Linear search |
26+
| `x + y` (concatenation) | O(m+n) | O(m+n) | m, n are lengths |
27+
| `[::2]` (slicing) | O(k) | O(k) | k = slice length |
3828

3929
## Implementation Details
4030

docs/builtins/range.md

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@
22

33
The `range` type is an immutable sequence of numbers used for iteration. It generates values lazily without storing all numbers in memory.
44

5-
## Time Complexity
6-
7-
| Operation | Time | Notes |
8-
|-----------|------|-------|
9-
| `range(stop)` | O(1) | Create range object |
10-
| `range(start, stop)` | O(1) | Create range object |
11-
| `range(start, stop, step)` | O(1) | Create range object |
12-
| `len()` | O(1) | Calculated, not stored |
13-
| `access[i]` | O(1) | Direct calculation |
14-
| `in` (membership) | O(1) | Math check, not scan |
15-
| `index(value)` | O(1) | Solve equation |
16-
| `count(value)` | O(1) | Single check |
17-
| `iteration` | O(n) | n = number of items |
18-
| `reversed()` | O(1) | Iterator, not materialized |
19-
| `list(range(...))` | O(n) | Convert to list |
20-
21-
## Space Complexity
22-
23-
| Operation | Space | Notes |
24-
|-----------|-------|-------|
25-
| Range object | O(1) | Fixed overhead (stores start, stop, step) |
26-
| Iteration | O(1) | No buffering, yields values on demand |
27-
| `list()` conversion | O(n) | Creates list |
28-
| `reversed()` iterator | O(1) | No materialization |
5+
## Complexity Reference
6+
7+
| Operation | Time | Space | Notes |
8+
|-----------|------|-------|-------|
9+
| `range(stop)` | O(1) | O(1) | Create range object |
10+
| `range(start, stop)` | O(1) | O(1) | Create range object |
11+
| `range(start, stop, step)` | O(1) | O(1) | Create range object |
12+
| `len()` | O(1) | O(1) | Calculated, not stored |
13+
| `access[i]` | O(1) | O(1) | Direct calculation |
14+
| `in` (membership) | O(1) | O(1) | Math check, not scan |
15+
| `index(value)` | O(1) | O(1) | Solve equation |
16+
| `count(value)` | O(1) | O(1) | Single check |
17+
| `iteration` | O(n) | O(1) | n = number of items; yields on demand |
18+
| `reversed()` | O(1) | O(1) | Iterator, not materialized |
19+
| `list(range(...))` | O(n) | O(n) | Convert to list |
2920

3021
## Implementation Details
3122

0 commit comments

Comments
 (0)