Skip to content

Commit 14c767e

Browse files
heikkitoivonencodex
andcommitted
Docs: Fix operator/optparse/os complexities
Co-Authored-By: Codex <codex@openai.com>
1 parent 74d6e04 commit 14c767e

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

docs/stdlib/operator.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ The `operator` module provides function equivalents for Python's operators, usef
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| `add(a, b)` | O(1) | O(1) | Addition |
10-
| `mul(a, b)` | O(1) | O(1) | Multiplication |
9+
| `add(a, b)` | Varies | Varies | Same as `a + b` |
10+
| `mul(a, b)` | Varies | Varies | Same as `a * b` |
1111
| `itemgetter(key)` | O(1) | O(1) | Get item function |
1212
| `attrgetter(name)` | O(1) | O(1) | Get attribute function |
1313
| `methodcaller(name)` | O(1) | O(1) | Call method function |
@@ -16,12 +16,12 @@ The `operator` module provides function equivalents for Python's operators, usef
1616

1717
### Basic Operations
1818

19-
#### Time Complexity: O(1)
19+
#### Time Complexity: Varies by operand type
2020

2121
```python
2222
import operator
2323

24-
# Arithmetic: O(1)
24+
# Arithmetic: same as a + b, etc.
2525
result = operator.add(5, 3) # 8
2626
result = operator.sub(10, 4) # 6
2727
result = operator.mul(6, 7) # 42
@@ -35,22 +35,22 @@ numbers = [1, 2, 3, 4, 5]
3535
doubled = list(map(operator.mul, numbers, [2] * len(numbers))) # O(n)
3636
```
3737

38-
#### Space Complexity: O(1)
38+
#### Space Complexity: Varies by operand type
3939

4040
```python
4141
import operator
4242

43-
result = operator.add(a, b) # O(1) space
43+
result = operator.add(a, b) # Same as a + b
4444
```
4545

4646
## Comparison Operators
4747

48-
#### Time Complexity: O(1)
48+
#### Time Complexity: Varies by operand type
4949

5050
```python
5151
import operator
5252

53-
# Comparisons: O(1)
53+
# Comparisons: same as a == b, a < b, etc.
5454
operator.eq(5, 5) # True
5555
operator.ne(5, 3) # True
5656
operator.lt(3, 5) # True
@@ -59,7 +59,7 @@ operator.gt(5, 3) # True
5959
operator.ge(5, 3) # True
6060

6161
# With sorted: O(n log n)
62-
items = [5, 2, 8, 1, 9]
62+
items = [(5,), (2,), (8,), (1,), (9,)]
6363
sorted_items = sorted(items, key=operator.itemgetter(0)) # O(n log n)
6464
```
6565

@@ -68,14 +68,14 @@ sorted_items = sorted(items, key=operator.itemgetter(0)) # O(n log n)
6868
```python
6969
import operator
7070

71-
result = operator.eq(a, b) # O(1) space
71+
result = operator.eq(a, b) # Same as a == b
7272
```
7373

7474
## Item and Attribute Access
7575

7676
### itemgetter()
7777

78-
#### Time Complexity: O(1)
78+
#### Time Complexity: Depends on the called method
7979

8080
```python
8181
import operator
@@ -152,7 +152,7 @@ import operator
152152
caller = operator.methodcaller('upper')
153153
result = caller('hello') # 'HELLO' - O(n) for string
154154

155-
# With arguments: O(1)
155+
# With arguments: still depends on method complexity
156156
caller = operator.methodcaller('replace', 'a', 'b')
157157
result = caller('banana') # 'bbnbnb' - O(n) for string
158158

@@ -162,13 +162,13 @@ uppers = list(map(operator.methodcaller('upper'), strings)) # O(n*m)
162162
# Result: ['HELLO', 'WORLD', 'PYTHON']
163163
```
164164

165-
#### Space Complexity: O(m)
165+
#### Space Complexity: Depends on the called method
166166

167167
```python
168168
import operator
169169

170170
caller = operator.methodcaller('upper') # O(1) space for method ref
171-
result = caller(string) # O(m) space for result
171+
result = caller(string) # Same as string.upper()
172172
```
173173

174174
## In-Place Operations
@@ -266,9 +266,9 @@ import operator
266266
items = [5, 2, 8, 1, 9]
267267
count_gt_5 = sum(map(operator.gt, items, [5] * len(items))) # O(n)
268268

269-
# Apply to sequences: O(n*m)
269+
# Apply to sequences: O(n)
270270
data = {'a': 1, 'b': 2, 'c': 3}
271-
values = list(map(operator.itemgetter, data.keys(), [data] * len(data))) # O(n)
271+
values = list(map(operator.itemgetter(1), data.items())) # O(n)
272272
```
273273

274274
## Performance Characteristics

docs/stdlib/optparse.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The `optparse` module provides option parsing functionality (deprecated in favor
66

77
| Operation | Time | Space | Notes |
88
|-----------|------|-------|-------|
9-
| `OptionParser.parse_args()` | O(n) | O(n) | n = arguments |
9+
| `OptionParser.parse_args()` | O(n) avg, O(n·m) worst | O(n) | n = args, m = long options (abbr matching) |
1010
| Add option | O(1) | O(1) | Register option |
1111

1212
## Parsing Options
@@ -23,7 +23,7 @@ parser = optparse.OptionParser()
2323
parser.add_option("-f", "--file", dest="filename")
2424
parser.add_option("-v", "--verbose", action="store_true")
2525

26-
# Parse arguments - O(n)
26+
# Parse arguments - O(n) average; O(n·m) worst-case with long option abbreviations
2727
(options, args) = parser.parse_args()
2828

2929
print(options.filename)

docs/stdlib/os.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The `os` module provides a way to use operating system-dependent functionality l
3030
| `os.replace(src, dst)` | O(1) | O(1) | Rename, overwriting dst if exists |
3131
| `os.link(src, dst)` | O(1) | O(1) | Create hard link |
3232
| `os.symlink(src, dst)` | O(1) | O(1) | Create symbolic link |
33-
| `os.readlink(path)` | O(1) | O(n) | Read symlink target |
33+
| `os.readlink(path)` | O(n) | O(n) | n = length of symlink target |
3434
| `os.chmod(path, mode)` | O(1) | O(1) | Change permissions |
3535
| `os.chown(path, uid, gid)` | O(1) | O(1) | Change owner/group |
3636
| `os.truncate(path, length)` | O(1) | O(1) | Truncate file to length |
@@ -39,7 +39,7 @@ The `os` module provides a way to use operating system-dependent functionality l
3939

4040
| Operation | Time | Space | Notes |
4141
|-----------|------|-------|-------|
42-
| `os.getcwd()` | O(1) | O(n) | Get current working directory |
42+
| `os.getcwd()` | O(n) | O(n) | n = length of current working directory |
4343
| `os.chdir(path)` | O(1) | O(1) | Change working directory |
4444
| `os.getenv(key)` | O(1) | O(1) | Get environment variable |
4545
| `os.putenv(key, value)` | O(1) | O(1) | Set environment variable |
@@ -199,7 +199,7 @@ pid = os.getpid()
199199
# Parent process ID - O(1)
200200
ppid = os.getppid()
201201

202-
# Current working directory - O(1)
202+
# Current working directory - O(n) in path length
203203
cwd = os.getcwd()
204204

205205
# Change working directory - O(1)

0 commit comments

Comments
 (0)