Basic data structures
todo:
- Integrate notes from codebase.py
Tuple
A tuple is a finite, ordered, immutable sequence of elements.
type((1)) # => int
type(()) # => tuple
type((1,)) # => tuple
Strings
Strings are a special type of array – one composed of characters.
| |
'ab'
| |
6
Unicode conversion
Convert character string into unicode code point
| |
(49, 50, 65, 66)
Convert unicode code point to character
| |
('1', '2', 'A', 'B')
Trick to convert integer to string representation
| |
'2'
Sequences
Useful stuff
Ignore minus sign in string number (using fact that False is 0 and True is 1.
| |
('123', '123')
Slice from (and/or) to particular characters
| |
'[def]'
this works because
| |
3
Sets
| |
| |
Lookup
Lookup has worst case time complexity O(n) and avearge time complexity O(1). Why?
| |
True
Difference
Basic set difference a-b has time complexity O(len(a)) (for every element in a move to new set, if not in b) and space complexity O(len(a) + len(b)), since a new set is being created.
| |
{'e', 'h'}
A variant is difference update, which has time complexity O(len(b)) (for every element in b remove from a if it exists) and space complexity O(1), as we don’t create a new set but update set a in place.
| |
{'e', 'h'}
Basic operations
| |
Named tuples
Basic use
| |
'aapl'
Replace values
| |
Stock(name='aapl', shares=200, price='55', date=None, time=None)
Use replace to populate named tuples with optional or missing fields
| |
[Stock(name='IBM', shares=100, price=91.1, date=None, time=None),
Stock(name='AAPL', shares=50, price=543.22, date=None, time=None),
Stock(name='FB', shares=200, price=21.09, date=None, time=None)]