Skip to content

Commit 31ef0ad

Browse files
authored
Merge branch 'master' into bugfix/attribut_data_not_needed_anymore
2 parents 7545034 + 0454bab commit 31ef0ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+7436
-2
lines changed

binary-search/README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,73 @@
1-
# How to Do a Binary Search in Python?
1+
# How to Do a Binary Search in Python
22

3-
Code snippets supplementing the [How to Do a Binary Search in Python?](https://realpython.com/binary-search-python/) article on [Real Python](https://realpython.com/).
3+
Code snippets supplementing the [How to Do a Binary Search in Python](https://realpython.com/binary-search-python/) article.
4+
5+
## Usage
6+
7+
### Download Sample Dataset
8+
9+
To download a sample dataset for benchmarking, run the following script:
10+
11+
```shell
12+
$ python download_imdb.py
13+
Fetching data from IMDb...
14+
Created "names.txt" and "sorted_names.txt"
15+
```
16+
17+
### Benchmark
18+
19+
**Note:** The hash-based search algorithm requires a separate data structure, so it's not included here. Feel free to treat it as an exercise.
20+
21+
Find the index of Arnold Schwarzenegger in unsorted names using random search:
22+
23+
```shell
24+
$ python benchmark.py -a random -f names.txt 'Arnold Schwarzenegger'
25+
Loading names...ok
26+
[1/10] Found at index=215 (21.68 s)
27+
[2/10] Found at index=215 (148.10 ms)
28+
[3/10] Found at index=215 (63.49 s)
29+
[4/10] Found at index=215 (15.93 s)
30+
[5/10] Found at index=215 (23.38 s)
31+
[6/10] Found at index=215 (60.98 s)
32+
[7/10] Found at index=215 (10.14 s)
33+
[8/10] Found at index=215 (15.81 s)
34+
[9/10] Found at index=215 (5.10 s)
35+
[10/10] Found at index=215 (13.36 s)
36+
best=148.10 ms, worst=63.49 s, avg=23.00 s, median=15.87 s
37+
```
38+
39+
Find the index of Arnold Schwarzenegger in unsorted names using linear search:
40+
41+
```shell
42+
$ python benchmark.py -a linear -f names.txt 'Arnold Schwarzenegger'
43+
Loading names...ok
44+
[1/10] Found at index=215 (30.95 µs)
45+
[2/10] Found at index=215 (26.86 µs)
46+
[3/10] Found at index=215 (26.26 µs)
47+
[4/10] Found at index=215 (26.55 µs)
48+
[5/10] Found at index=215 (26.24 µs)
49+
[6/10] Found at index=215 (25.88 µs)
50+
[7/10] Found at index=215 (25.77 µs)
51+
[8/10] Found at index=215 (25.89 µs)
52+
[9/10] Found at index=215 (25.91 µs)
53+
[10/10] Found at index=215 (25.99 µs)
54+
best=25.77 µs, worst=30.95 µs, avg=26.63 µs, median=26.11 µs
55+
```
56+
57+
Find the index of Arnold Schwarzenegger in *sorted* names using binary search:
58+
59+
```shell
60+
$ python benchmark.py -a binary -f sorted_names.txt 'Arnold Schwarzenegger'
61+
Loading names...ok
62+
[1/10] Found at index=782431 (18.82 µs)
63+
[2/10] Found at index=782431 (9.19 µs)
64+
[3/10] Found at index=782431 (11.08 µs)
65+
[4/10] Found at index=782431 (9.70 µs)
66+
[5/10] Found at index=782431 (10.14 µs)
67+
[6/10] Found at index=782431 (9.35 µs)
68+
[7/10] Found at index=782431 (9.42 µs)
69+
[8/10] Found at index=782431 (8.79 µs)
70+
[9/10] Found at index=782431 (8.66 µs)
71+
[10/10] Found at index=782431 (8.79 µs)
72+
best=8.66 µs, worst=18.82 µs, avg=10.39 µs, median=9.38 µs
73+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
import cffi_example
3+
4+
if __name__ == "__main__":
5+
# Sample data for our call:
6+
x, y = 6, 2.3
7+
8+
answer = cffi_example.lib.cmult(x, y)
9+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include "cmult.h"
3+
4+
5+
float cmult(int int_param, float float_param) {
6+
float return_value = int_param * float_param;
7+
printf(" In cmult : int: %d float %.1f returning %.1f\n", int_param,
8+
float_param, return_value);
9+
return return_value;
10+
}
11+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
float cmult(int int_param, float float_param);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include "cppmult.hpp"
4+
5+
6+
float cppmult(int int_param, float float_param) {
7+
float return_value = int_param * float_param;
8+
std::cout << std::setprecision(1) << std::fixed
9+
<< " In cppmul: int: " << int_param
10+
<< " float " << float_param
11+
<< " returning " << return_value
12+
<< std::endl;
13+
return return_value;
14+
}
15+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
float cppmult(int int_param, float float_param);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
""" Simple examples of calling C functions through ctypes module. """
3+
import ctypes
4+
import pathlib
5+
6+
7+
if __name__ == "__main__":
8+
# Load the shared library into c types.
9+
libname = pathlib.Path().absolute() / "libcmult.so"
10+
c_lib = ctypes.CDLL(libname)
11+
12+
# Sample data for our call:
13+
x, y = 6, 2.3
14+
15+
# This will not work:
16+
# answer = c_lib.cmult(x, y)
17+
18+
# This produces a bad answer:
19+
answer = c_lib.cmult(x, ctypes.c_float(y))
20+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
21+
print()
22+
23+
# You need tell ctypes that the function returns a float
24+
c_lib.cmult.restype = ctypes.c_float
25+
answer = c_lib.cmult(x, ctypes.c_float(y))
26+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
""" Example cython interface definition """
2+
3+
cdef extern from "cppmult.hpp":
4+
float cppmult(int int_param, float float_param)
5+
6+
def pymult( int_param, float_param ):
7+
return cppmult( int_param, float_param )
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
import cython_example
3+
4+
# Sample data for our call
5+
x, y = 6, 2.3
6+
7+
answer = cython_example.pymult(x, y)
8+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
import pybind11_example
3+
4+
if __name__ == "__main__":
5+
# Sample data for our call:
6+
x, y = 6, 2.3
7+
8+
answer = pybind11_example.cpp_function(x, y)
9+
print(f" In Python: int: {x} float {y:.1f} return val {answer:.1f}")

0 commit comments

Comments
 (0)