algokit-py 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- algokit_py/__init__.py +8 -0
- algokit_py/search/__init__.py +4 -0
- algokit_py/search/binary.py +46 -0
- algokit_py/search/linear.py +19 -0
- algokit_py-0.1.0.dist-info/METADATA +52 -0
- algokit_py-0.1.0.dist-info/RECORD +8 -0
- algokit_py-0.1.0.dist-info/WHEEL +5 -0
- algokit_py-0.1.0.dist-info/top_level.txt +1 -0
algokit_py/__init__.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The True Minimum Requirements for Binary Search
|
|
3
|
+
Now we can state them precisely:
|
|
4
|
+
- The data must be sorted
|
|
5
|
+
- The data must support random access (you must be able to get the middle element)
|
|
6
|
+
- The elements must be comparable to the target
|
|
7
|
+
|
|
8
|
+
Every step of binary search answers one question only:
|
|
9
|
+
- "Which half can I safely throw away without losing the target?"
|
|
10
|
+
|
|
11
|
+
For binary search, always remember this sentence:
|
|
12
|
+
At every step, the algorithm maintains a range that is guaranteed to contain the target if it exists.
|
|
13
|
+
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from typing import Sequence, TypeVar
|
|
17
|
+
|
|
18
|
+
T = TypeVar('T')
|
|
19
|
+
# here we use Sequence because binary seach does not support for unindexed iterables
|
|
20
|
+
def binary_search(sequence: Sequence[T], target: T)-> int:
|
|
21
|
+
"""
|
|
22
|
+
This function will perform the binary search
|
|
23
|
+
|
|
24
|
+
:param sequence: sorted and indexed sequence
|
|
25
|
+
:param target: value that you want to find
|
|
26
|
+
:return: return index of a occurrence, -1 if target is not found
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
Time Complexity: O(log(n))
|
|
30
|
+
Space Complexity: O(1)
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
left,right = 0, len(sequence)-1
|
|
34
|
+
|
|
35
|
+
while left <= right:
|
|
36
|
+
mid = (left + right) // 2
|
|
37
|
+
|
|
38
|
+
if sequence[mid] == target:
|
|
39
|
+
return mid
|
|
40
|
+
|
|
41
|
+
if target < sequence[mid]:
|
|
42
|
+
right = mid - 1
|
|
43
|
+
else:
|
|
44
|
+
left = mid +1
|
|
45
|
+
|
|
46
|
+
return -1
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from typing import TypeVar, Iterable
|
|
2
|
+
|
|
3
|
+
T = TypeVar('T')
|
|
4
|
+
def linear_search(iterable: Iterable[T] , target: T)-> int:
|
|
5
|
+
"""
|
|
6
|
+
This function do linear search.
|
|
7
|
+
|
|
8
|
+
:param iterable:
|
|
9
|
+
:param target:
|
|
10
|
+
:return: return index of first occurrence, -1 if target is not found
|
|
11
|
+
|
|
12
|
+
Time complexity: O(n)
|
|
13
|
+
Space complexity: O(1)
|
|
14
|
+
"""
|
|
15
|
+
for i,v in enumerate(iterable):
|
|
16
|
+
if v == target:
|
|
17
|
+
return i
|
|
18
|
+
|
|
19
|
+
return -1
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: algokit-py
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A minimal Python algorithms library with clean implementations of search algorithms
|
|
5
|
+
Author: Chanaka Prasanna
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# algokit-py
|
|
14
|
+
|
|
15
|
+
A lightweight Python algorithms library with clean, well-documented implementations.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
- Linear Search
|
|
19
|
+
- Binary Search
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from algokit_py.search import linear_search, binary_search
|
|
25
|
+
|
|
26
|
+
print(linear_search([1, 2, 3], 2))
|
|
27
|
+
print(binary_search([1, 2, 3], 2))
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
We will improve this later.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## STEP 6: Local Install Test (Very Important)
|
|
36
|
+
|
|
37
|
+
From **project root**:
|
|
38
|
+
|
|
39
|
+
```powershell
|
|
40
|
+
pip install -e .
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Then test:
|
|
44
|
+
|
|
45
|
+
```powershell
|
|
46
|
+
python -c "from algokit_py.search import binary_search; print(binary_search([1,2,3], 2))"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
You must see:
|
|
50
|
+
```python
|
|
51
|
+
1
|
|
52
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
algokit_py/__init__.py,sha256=xhI99pue6hsmyVVOCd60YTglScw-RcWssAdGBUg3yNY,193
|
|
2
|
+
algokit_py/search/__init__.py,sha256=0xreC43bjUH_VRqJTv48VbPadrY4GQgP7y3PMQ5RR04,118
|
|
3
|
+
algokit_py/search/binary.py,sha256=3VZ0ZMtANDProjkGZkX1NUpZZPmwfZF5CevpeJ-Bd-Q,1336
|
|
4
|
+
algokit_py/search/linear.py,sha256=07bjqKVarTmYiC0Uw-Pg_EuNRcsAD4zsn9lfuprTJAc,451
|
|
5
|
+
algokit_py-0.1.0.dist-info/METADATA,sha256=E8L-a1xjSM5L-SJoVaQZ7U1kepUg2mB_jvxRDhkPjwo,1041
|
|
6
|
+
algokit_py-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
7
|
+
algokit_py-0.1.0.dist-info/top_level.txt,sha256=OqfCkcr7SFcMZHBQVKnTOy6Plaoq8t0A8M4CN6Hca0w,11
|
|
8
|
+
algokit_py-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
algokit_py
|