Python algorithm design source code: https://github.com/MakerChen66/Python3Algorithm
Copyright statement: Originality is not easy, this article prohibits plagiarism, reprinting, infringement must be investigated!
Table of contents
1. Binary addition | finite state machine
Adding binary numbers can be done as taught in school, just remember 1 + 1 = 10 and follow certain rules. There's a lot to be said for this seemingly simple arithmetic, for example you can look at the implementation of a parallel adder, there's a lot of interesting stuff in it.
I decided to use a serial implementation via a finite state machine, with 4 states based on combinations of output 0/1 and carry 0/1 and groups of transitions based on input bits.
The question is do you really need four states? In fact only three states can be used, what can you find?
Python algorithm implementation:
import itertools
p0c0 = 0, {
}
p1c0 = 1, {
}
p0c1 = 0, {
}
p1c1 = 1, {
}
# 各进程状态之间的转换
p0c0[1].update({
(0, 0): p0c0, (1, 0): p1c0,
(0, 1): p1c0, (1, 1): p0c1})
p1c0[1].update({
(0, 0): p0c0, (1, 0): p1c0,
(0, 1): p1c0, (1, 1): p0c1})
p0c1[1].update({
(0, 0): p1c0, (1, 0): p0c1,
(0, 1): p0c1, (1, 1): p1c1})
p1c1[1].update({
(0, 0): p1c0, (1, 0): p0c1,
(0, 1): p0c1, (1, 1): p1c1})
def add(x, y):
x = map(int, reversed(x))
y = map(int, reversed(y))
z = []
# 模拟自动机
value, transition = p0c0
for r, s in itertools.zip_longest(x, y, fillvalue=0):
value, transition = transition[r, s]
z.append(value)
z.append(transition[0, 0][0])
return ''.join(map(str, reversed(z)))
print(add('1100100100100', '100100011000'))
Notice:
- itertools
is python's iterator module, which provides very useful functions for manipulating iterative objects, such as the zip_longest() function. The tools provided by itertools are quite efficient and save memory. Using these tools, you will be able to create your own custom iterators for efficient looping. - zip_longest(it_obj1, …, it_objN,
fillvalue=None), the functions implemented by its function are roughly the same as the built-in zip function (realize one-to-one correspondence), but the built-in zip function is based on the object with the least elements, while the zip_longest function is based on the element The most objects are used as the benchmark, and the empty places are filled with the value of fillvalue - map(function,iterable,…) will map the specified sequence according to the provided function. The first parameter function
calls the function function with each element in the parameter sequence, and returns a new list containing the return value of each function function. - Binary addition starts from the far right, so the input binary number should be reversed with the reversed() function, and then reversed after the operation
Output result:
As shown in the figure, the result of binary addition operation is 10001000111100, the result is correct
2. Source code download
Python algorithm design source code download:
- GitHub download link: Portal
- Link to the original text: Read the original text
3. Author Info
Author: Xiaohong's Fishing Daily, Goal: Make programming more interesting!
Original WeChat public account: " Xiaohong Xingkong Technology ", focusing on school recruitment (introduction/bijing/face-to-face), algorithm, crawler, website, game development, data analysis, natural language processing, AI, etc., looking forward to your attention , let us grow and code together!
Copyright Note: This article prohibits plagiarism and reprinting, and infringement must be investigated!