Writing a binary search algorithm is surprisingly error-prone. The solution: trick the built-in bisect module into doing it for you.
The documentation for bisect says that it works on lists, but it really works on anything with a __getitem__ method. You can exploit this fact to make bisect work in ways that you may not have thought of.
Example: Using a library that controls a digital video camera, I wanted to do a...
This is an implementation of the binary search algorithm in (almost) one line. Given a number 'n' and a list 'L', the function returns the index of the number on the list, or -1.
Lempel Ziv algorithm implementation for binary sequences. It has also a test file for unit test
[dec_cod, code_bin, code_book] = lempel_ziv(alf, str)
It's a function with three output variables and two input variables. 'dec_cod' is the decimal code that codify the input string 'str'. 'code_bin' is the binary code of the...
This example adds MATLABdlTÂ« to the list of languages in this C++ vs Java vs Python vs Ruby: a first impression (http://www.dmh2000.com/cjpr/ ) article at www.dmh2000.com, which compares implementations of a Red Black Tree binary search algorithm...
This code implements Bresenham Line Algorithm with slight modification of error term in respect of computation efficiency that it does not require any division process. Hence it is more efficient. Also it has been generalized to draw lines of any...
The gliding box algorithm proposed by Tolle et al., Physica D, 237, 306-315, 2008 is used to calculate the lacunarity of a binary image.
This function estiamtes the Kolmogorov Complexity of a binary sequence. The algorithm is as described in Lempel and Ziv (IEEE trans inf theory IT-22, 75 (1976)) and Kaspar and Schuster (Physical Review A, vol 36, no 2, pg 842). Input is...
findMIS is an heuristic algorithm for solving Maximum Independent Set problem (MIS). An independent set of a graph is a subset of vertices in which no two vertices are adjacent. Given a set of vertices, the maximum independent set...
Extended compact genetic algorithm (ECGA) is an algorithm that can solve hard problems in the binary domain. ECGA is reliable and accurate because of the capability of detecting building blocks, but certain difficulties are encountered...
This demonstrates a simple binary search through sorted data. A binary search is a basic algorithm provided by bisect in Python. The binary search can be summarized by two lines of code: list.sort() item_insert_point =...
I was trying out modifications of the LMS algorithm so that it will converge faster and the mean square error will also be smaller. Getting to one of the drawbacks of LMS, that it has only one controllable parameter "mu", the selection...
A portable in-place bitwise binary Fredkin trie algorithm which allows for near constant time insertions, deletions, finds, closest fit finds and iteration. Is approx. 50-100% faster than red-black trees and up to 20% faster than O(1) hash tables.
This is an evolutionary algorithm that returns a random list of prime numbers. This code is highly inefficient for a reason. This algorithm is more of a proof of concept that if a prime was a heritable trait, it would not be a desired one.
One way to read files that contain binary fields is to use the struct module. However, to do this properly one must learn struct's format characters, which may look especially cryptic when sprinkled around the code. So instead, I use a wrapper...
This is a pure Python implementation of the rsync algorithm. On my desktop (3.0GHz dual core, 7200RPM), best case throughput for target file hash generation and delta generation is around 2.9MB/s. Absolute worst case scenario (no blocks in common)...
A-star (A*) Shortest Path Algorithm
Dijkstra shortest path algorithm.
Really short snipped of a decimal and binary prefix calculator in Python.
Without covering the entire International System of Units (SI) range, it does fit my daily needs. The code could be way smarter, but i wanted to keep the...
This is a very simple look-up based method for converting integer to binary string. In my tests, using 'timeit', its the fastest around. And with increasing value of input decimal number, it surpases any 'run time computation' based method doing... |