Skip to content

Commit 73431ed

Browse files
Manual review of builtin functions: tuple-bytearray
1 parent 499c25b commit 73431ed

2 files changed

Lines changed: 0 additions & 139 deletions

File tree

docs/builtins/bytearray_func.md

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -187,67 +187,6 @@ ba[1] = 0x69 # O(1)
187187
modified = bytes(ba) # O(n)
188188
```
189189

190-
## Practical Examples
191-
192-
### Network Protocol Building
193-
194-
```python
195-
# O(n) - build binary protocol message
196-
def build_packet(header, payload):
197-
packet = bytearray()
198-
199-
packet.extend(header) # O(|header|)
200-
packet.append(len(payload)) # O(1)
201-
packet.extend(payload) # O(|payload|)
202-
203-
return bytes(packet) # O(n) - convert to immutable
204-
```
205-
206-
### Image Data Manipulation
207-
208-
```python
209-
# O(n) - modify pixel data
210-
image_data = bytearray([255, 0, 0, 0, 255, 0, 0, 0, 255])
211-
# Three RGB pixels
212-
213-
# Modify pixel values
214-
image_data[0] = 200 # O(1) - change red value
215-
image_data[4] = 200 # O(1) - change green value
216-
217-
# Process efficiently without copying
218-
```
219-
220-
### Cryptographic Operations
221-
222-
```python
223-
# O(n) - prepare data for encryption
224-
import hashlib
225-
226-
message = "Secret message"
227-
ba = bytearray(message, "utf-8") # O(n)
228-
229-
# Modify if needed
230-
ba[0] = ord('s') # O(1)
231-
232-
# Hash
233-
hashed = hashlib.sha256(ba).hexdigest() # O(n)
234-
```
235-
236-
### File Data Processing
237-
238-
```python
239-
# O(n) - read and modify file
240-
with open("data.bin", "rb") as f:
241-
data = bytearray(f.read()) # O(n) - read entire file
242-
243-
# Modify specific bytes
244-
data[0] = 0x00 # O(1) - change magic number
245-
246-
# Write back
247-
with open("output.bin", "wb") as f:
248-
f.write(data) # O(n)
249-
```
250-
251190
## Edge Cases
252191

253192
### Empty Bytearray

docs/builtins/tuple_func.md

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -175,84 +175,6 @@ tuples = [tuple(item) for item in data] # O(n*m)
175175
tuples = list(map(tuple, data)) # O(n*m)
176176
```
177177

178-
## Practical Examples
179-
180-
### Function Return Values
181-
182-
```python
183-
# O(n) - return multiple values
184-
def get_coordinates():
185-
x, y, z = 1, 2, 3
186-
return (x, y, z) # O(1) - literal
187-
188-
# or
189-
return tuple([x, y, z]) # O(3) - function
190-
```
191-
192-
### Dictionary Keys
193-
194-
```python
195-
# O(n) - tuples as keys
196-
locations = {
197-
(0, 0): "origin",
198-
(1, 2): "point A",
199-
(3, 4): "point B",
200-
}
201-
202-
# Can use tuple() to create key
203-
coord = (1, 2)
204-
location = locations.get(coord) # O(1)
205-
```
206-
207-
### Sequence of Pairs
208-
209-
```python
210-
# O(n) - zip creates iterator of tuples
211-
names = ["Alice", "Bob", "Charlie"]
212-
ages = [25, 30, 35]
213-
214-
pairs = list(zip(names, ages)) # O(n)
215-
# [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
216-
217-
# Convert to tuples explicitly
218-
pairs = [tuple(pair) for pair in ...] # O(n)
219-
```
220-
221-
### Named Tuple-like Structure
222-
223-
```python
224-
# O(n) - create tuple with labels
225-
def make_person(name, age, email):
226-
return (name, age, email)
227-
228-
person = make_person("Alice", 25, "alice@example.com")
229-
name, age, email = person # O(1) unpacking
230-
231-
# vs named tuple (more overhead)
232-
from collections import namedtuple
233-
Person = namedtuple("Person", ["name", "age", "email"])
234-
p = Person("Alice", 25, "alice@example.com")
235-
```
236-
237-
### Immutable State
238-
239-
```python
240-
# O(n) - create immutable snapshot
241-
class DataSnapshot:
242-
def __init__(self, data):
243-
self._data = tuple(data) # O(n)
244-
245-
def get_data(self):
246-
return self._data # O(1) - safe to return
247-
248-
def __hash__(self):
249-
return hash(self._data) # O(1) - hashable
250-
251-
snapshot = DataSnapshot([1, 2, 3])
252-
data = snapshot.get_data() # (1, 2, 3)
253-
# Can't modify
254-
```
255-
256178
## Edge Cases
257179

258180
### Empty Tuple

0 commit comments

Comments
 (0)