Skip to content

Commit 499c25b

Browse files
Manual review of builtin functions: set-frozenset
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 935c618 commit 499c25b

2 files changed

Lines changed: 25 additions & 154 deletions

File tree

docs/builtins/frozenset_func.md

Lines changed: 9 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ The `frozenset()` function creates immutable sets from iterables.
1414

1515
## Basic Usage
1616

17+
!!! note "Element order in examples"
18+
Frozensets are unordered. The element order shown in comments below is illustrative; actual `repr()` output is not guaranteed and may differ between runs, Python versions, or hash-randomization settings.
19+
1720
### Create Empty Frozenset
1821

1922
```python
@@ -25,25 +28,25 @@ fs = frozenset() # frozenset()
2528

2629
```python
2730
# O(n) - where n = list length
28-
fs = frozenset([1, 2, 3]) # frozenset({1, 2, 3})
29-
fs = frozenset([1, 2, 2, 3, 3, 3]) # frozenset({1, 2, 3})
31+
fs = frozenset([1, 2, 3]) # frozenset({2, 1, 3})
32+
fs = frozenset([1, 2, 2, 3, 3, 3]) # frozenset({1, 3, 2})
3033
```
3134

3235
### From String
3336

3437
```python
3538
# O(n) - where n = string length
36-
fs = frozenset("hello") # frozenset({'h', 'e', 'l', 'o'})
37-
fs = frozenset("aabbcc") # frozenset({'a', 'b', 'c'})
39+
fs = frozenset("hello") # frozenset({'h', 'l', 'e', 'o'})
40+
fs = frozenset("aabbcc") # frozenset({'c', 'b', 'a'})
3841
```
3942

4043
### From Other Iterables
4144

4245
```python
4346
# O(n) - where n = iterable length
44-
fs = frozenset((1, 2, 3)) # frozenset({1, 2, 3})
47+
fs = frozenset((1, 2, 3)) # frozenset({2, 1, 3})
4548
fs = frozenset({1, 2, 3}) # frozenset({1, 2, 3})
46-
fs = frozenset(range(5)) # frozenset({0, 1, 2, 3, 4})
49+
fs = frozenset(range(5)) # frozenset({0, 2, 1, 3, 4})
4750
fs = frozenset(map(str, [1, 2])) # frozenset({'1', '2'})
4851
```
4952

@@ -165,93 +168,6 @@ fs1 & fs2 # frozenset({2, 3})
165168
s1 & fs2 # {2, 3} - mixed works
166169
```
167170

168-
## Practical Examples
169-
170-
### Config Validation
171-
172-
```python
173-
# O(n) - validate against allowed values
174-
ALLOWED_ROLES = frozenset(["admin", "user", "guest"])
175-
176-
def validate_roles(roles):
177-
role_set = frozenset(roles) # O(n)
178-
if not role_set.issubset(ALLOWED_ROLES): # O(n)
179-
raise ValueError("Invalid roles")
180-
return role_set
181-
182-
valid = validate_roles(["admin", "user"])
183-
```
184-
185-
### Cache Configuration
186-
187-
```python
188-
# O(n) - use frozenset as cache key
189-
from functools import lru_cache
190-
191-
@lru_cache(maxsize=256)
192-
def analyze_features(features):
193-
# features is frozenset - hashable
194-
return len(features)
195-
196-
result = analyze_features(frozenset(["a", "b", "c"])) # O(3)
197-
```
198-
199-
### Immutable Data Structure
200-
201-
```python
202-
# O(n) - create immutable set for safety
203-
class DataSet:
204-
def __init__(self, items):
205-
self._items = frozenset(items) # O(n)
206-
207-
def get_items(self):
208-
return self._items # O(1) - safe to return
209-
210-
def has_item(self, item):
211-
return item in self._items # O(1)
212-
213-
dataset = DataSet([1, 2, 3])
214-
items = dataset.get_items() # frozenset({1, 2, 3})
215-
# Can't modify items
216-
```
217-
218-
### Database Index Key
219-
220-
```python
221-
# O(n) - frozenset for database queries
222-
def query_by_tags(tags):
223-
tag_set = frozenset(tags) # O(n)
224-
225-
# Use as cache key
226-
cache_key = ("tags", tag_set) # O(1)
227-
228-
# Query database
229-
return db_lookup[cache_key] # O(1)
230-
231-
results = query_by_tags(["python", "database"])
232-
```
233-
234-
### Unique Constraint
235-
236-
```python
237-
# O(n) - enforce uniqueness immutably
238-
class User:
239-
def __init__(self, permissions):
240-
self.permissions = frozenset(permissions) # O(n)
241-
242-
def __hash__(self):
243-
return hash(self.permissions) # O(1) - hashable
244-
245-
def __eq__(self, other):
246-
return self.permissions == other.permissions
247-
248-
user1 = User(["read", "write"])
249-
user2 = User(["read", "write"])
250-
251-
# Can use in set
252-
unique_users = {user1, user2} # Only one (equal)
253-
```
254-
255171
## Edge Cases
256172

257173
### Empty Frozenset

docs/builtins/set_func.md

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ The `set()` function creates sets from iterables or creates empty sets.
1414

1515
## Basic Usage
1616

17+
!!! note "Element order in examples"
18+
Sets are unordered. The element order shown in comments below is illustrative; actual `repr()` output is not guaranteed and may differ between runs, Python versions, or hash-randomization settings.
19+
1720
### Create Empty Set
1821

1922
```python
@@ -25,25 +28,25 @@ s = set() # set()
2528

2629
```python
2730
# O(n) - where n = list length
28-
s = set([1, 2, 3]) # {1, 2, 3}
29-
s = set([1, 2, 2, 3, 3, 3]) # {1, 2, 3} - duplicates removed
31+
s = set([1, 2, 3]) # {3, 2, 1}
32+
s = set([1, 2, 2, 3, 3, 3]) # {2, 1, 3} - duplicates removed
3033
```
3134

3235
### From String
3336

3437
```python
3538
# O(n) - where n = string length
36-
s = set("hello") # {'h', 'e', 'l', 'o'}
37-
s = set("aabbcc") # {'a', 'b', 'c'} - each char once
39+
s = set("hello") # {'e', 'h', 'l', 'o'}
40+
s = set("aabbcc") # {'c', 'b', 'a'} - each char once
3841
```
3942

4043
### From Other Iterables
4144

4245
```python
4346
# O(n) - where n = iterable length
44-
s = set((1, 2, 3)) # {1, 2, 3} (from tuple)
45-
s = set({1, 2, 3}) # {1, 2, 3} (from set - copy)
46-
s = set(range(5)) # {0, 1, 2, 3, 4}
47+
s = set((1, 2, 3)) # {2, 1, 3} (from tuple)
48+
s = set({1, 2, 3}) # {1, 3, 2} (from set - copy)
49+
s = set(range(5)) # {0, 2, 1, 3, 4}
4750
s = set(map(str, [1, 2])) # {'1', '2'} (from generator)
4851
```
4952

@@ -52,7 +55,7 @@ s = set(map(str, [1, 2])) # {'1', '2'} (from generator)
5255
```python
5356
# O(n) - uses dict keys
5457
d = {"a": 1, "b": 2, "c": 3}
55-
s = set(d) # {'a', 'b', 'c'} - only keys, not values
58+
s = set(d) # {'b', 'a', 'c'} - only keys, not values
5659
```
5760

5861
## Complexity Details
@@ -76,10 +79,10 @@ s = set(items) # O(10)
7679
```python
7780
# O(n) - automatic deduplication
7881
items = [1, 1, 2, 2, 3, 3]
79-
s = set(items) # O(6) - creates {1, 2, 3}
82+
s = set(items) # O(6) - creates {2, 1, 3}
8083

81-
# Useful for removing duplicates while preserving unordered
82-
unique = set("mississippi") # {'m', 'i', 's', 'p'}
84+
# Useful for removing duplicates
85+
unique = set("mississippi") # {'i', 'm', 's', 'p'}
8386
```
8487

8588
### From String
@@ -98,7 +101,7 @@ long = set("a" * 1000) # O(1000) - {'a'} (one item!)
98101
# O(n) - fastest way to remove duplicates
99102
items = [1, 2, 2, 3, 3, 3, 4]
100103
unique = set(items) # O(7)
101-
# {1, 2, 3, 4}
104+
# {2, 1, 3, 4}
102105

103106
# Back to list if needed
104107
unique_list = list(set(items)) # O(n)
@@ -120,7 +123,7 @@ s1 = set([1, 2, 3])
120123
s2 = set([2, 3, 4])
121124

122125
intersection = s1 & s2 # {2, 3} - O(n)
123-
union = s1 | s2 # {1, 2, 3, 4} - O(n)
126+
union = s1 | s2 # {2, 1, 3, 4} - O(n)
124127
difference = s1 - s2 # {1} - O(n)
125128
symmetric_diff = s1 ^ s2 # {1, 4} - O(n)
126129
```
@@ -166,54 +169,6 @@ if 500 in s: # O(1)
166169
pass
167170
```
168171

169-
## Practical Examples
170-
171-
### Find Unique Words
172-
173-
```python
174-
# O(n) - get unique words from text
175-
text = "the quick brown fox jumps over the lazy dog"
176-
words = text.split()
177-
unique_words = set(words) # O(n)
178-
# {'the', 'quick', 'brown', 'fox', ...}
179-
```
180-
181-
### Common Elements
182-
183-
```python
184-
# O(n) - find common items
185-
list1 = [1, 2, 3, 4, 5]
186-
list2 = [4, 5, 6, 7, 8]
187-
188-
set1 = set(list1) # O(n)
189-
set2 = set(list2) # O(m)
190-
191-
common = set1 & set2 # {4, 5} - O(min(n,m))
192-
```
193-
194-
### Unique Count
195-
196-
```python
197-
# O(n) - count unique items
198-
data = [1, 1, 2, 2, 2, 3, 3, 3, 3, 4]
199-
unique_count = len(set(data)) # O(n) - 4 unique
200-
201-
# Useful for analytics
202-
log_entries = ["error", "error", "warning", "error"]
203-
unique_types = set(log_entries) # O(n)
204-
```
205-
206-
### Validate Uniqueness
207-
208-
```python
209-
# O(n) - check if all unique
210-
def has_duplicates(items):
211-
return len(items) != len(set(items)) # O(n)
212-
213-
assert not has_duplicates([1, 2, 3]) # True - unique
214-
assert has_duplicates([1, 2, 2]) # False - duplicates
215-
```
216-
217172
## Edge Cases
218173

219174
### Empty Set

0 commit comments

Comments
 (0)