mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-08 23:22:48 -04:00
abstracted structures fixed
This commit is contained in:
parent
3fdbc2a605
commit
01703751f1
98 changed files with 305 additions and 856 deletions
32
src/bitwise/bit_operations/bit_array.py
Normal file
32
src/bitwise/bit_operations/bit_array.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Example of how to use a bit array in python as a "counter" dict:
|
||||
|
||||
>>> l1 = [0, 1, 2, 3, 4, 2, 6, 7, 8, 9]
|
||||
>>> print_dupl_ba(l1)
|
||||
2
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def print_dupl_ba(l1):
|
||||
bs = bytearray(10)
|
||||
for i in range(len(l1)):
|
||||
if i == l1[i]:
|
||||
bs[i] = 1
|
||||
for index, bit in enumerate(bs):
|
||||
if bit == 0:
|
||||
return l1[index]
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
40
src/bitwise/bit_operations/clear_bits.py
Normal file
40
src/bitwise/bit_operations/clear_bits.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Clear a bit in a binary number. It is almost like the reverse of set bit:
|
||||
1) first create a number filled of 1s, with 0 at i (can create 0001000 and ~)
|
||||
2) AND the number so it clears the ith bit
|
||||
|
||||
>>> num = int('10010000', 2)
|
||||
>>> clear_bit(num, 4)
|
||||
'0b10000000'
|
||||
>>> num = int('10010011', 2)
|
||||
>>> clear_all_bits_from_i_to_0(num, 2)
|
||||
'0b10010000'
|
||||
>>> num = int('1110011', 2)
|
||||
>>> clear_all_bits_from_most_sig_to_1(num, 2)
|
||||
'0b11'
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def clear_bit(num, i):
|
||||
mask = ~ (1 << i) # -0b10001
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
def clear_all_bits_from_i_to_0(num, i):
|
||||
mask = ~ ( (1 << (i+1)) - 1)
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
def clear_all_bits_from_most_sig_to_1(num, i):
|
||||
mask = ( 1 << i) -1
|
||||
return bin(num & mask)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
28
src/bitwise/bit_operations/find_bit_len.py
Normal file
28
src/bitwise/bit_operations/find_bit_len.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Find how many bits a int has:
|
||||
1) Start with a mask of 1
|
||||
2) Mask with AND
|
||||
3) if result (if true): count += 1
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
|
||||
|
||||
>>> for i in range(17): print(find_bit_len(i))
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def find_bit_len(int_num):
|
||||
lenght = 0
|
||||
while int_num:
|
||||
int_num >>= 1
|
||||
lenght += 1
|
||||
return lenght
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
34
src/bitwise/bit_operations/find_how_many_1_binary.py
Normal file
34
src/bitwise/bit_operations/find_how_many_1_binary.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Find how many 1s in the binary:
|
||||
1) Start with a mask of 1
|
||||
2) Mask with AND
|
||||
3) if result (if true): count += 1
|
||||
(obs: to find the int of a bin do int('1001', 2)) and to show in bin do bin(int))
|
||||
|
||||
>>> find_how_many_1_in_a_binary(9)
|
||||
2
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def find_how_many_1_in_a_binary(n):
|
||||
counter = 0
|
||||
while n:
|
||||
if n & 1:
|
||||
counter += 1
|
||||
n >>= 1
|
||||
return counter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
41
src/bitwise/bit_operations/get_bit.py
Normal file
41
src/bitwise/bit_operations/get_bit.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Get a bit in a binary number:
|
||||
1) Shifts 1 over by i bits
|
||||
2) make an AND with the number
|
||||
3) all the other than the bit at i are clean, now compare to 0
|
||||
4) if the new value is not 0, bit i is 1
|
||||
>>> num = int('0100100', 2)
|
||||
>>> get_bit(num, 0)
|
||||
0
|
||||
>>> get_bit(num, 1)
|
||||
0
|
||||
>>> get_bit(num, 2)
|
||||
1
|
||||
>>> get_bit(num, 3)
|
||||
0
|
||||
>>> get_bit(num, 4)
|
||||
0
|
||||
>>> get_bit(num, 5)
|
||||
1
|
||||
>>> get_bit(num, 6)
|
||||
0
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def get_bit(num, i):
|
||||
mask = 1 << i
|
||||
return num & mask != 0
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
35
src/bitwise/bit_operations/get_float_rep_bin.py
Normal file
35
src/bitwise/bit_operations/get_float_rep_bin.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Given a real number between 0 and 1 (eg: 0.72), this method print the binary
|
||||
representation. If the Number cannot be represented accurately in binary, with at
|
||||
most 32 chars, print error:
|
||||
|
||||
>>> get_float_rep(0.72)
|
||||
('Error 2', '.1011100001010001111010111000010')
|
||||
>>> get_float_rep(0.1)
|
||||
('Error 2', '.0001100110011001100110011001100')
|
||||
>>> get_float_rep(0.5)
|
||||
'.1'
|
||||
'''
|
||||
|
||||
def get_float_rep(num):
|
||||
if num >= 1 or num <= 0: return 'Error 1'
|
||||
result = '.'
|
||||
while num:
|
||||
if len(result) >= 32: return 'Error 2', result
|
||||
r = num*2
|
||||
if r >= 1:
|
||||
result += '1'
|
||||
num = r - 1
|
||||
else:
|
||||
result += '0'
|
||||
num = r
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
35
src/bitwise/bit_operations/insert_small_bin_into_big_bin.py
Normal file
35
src/bitwise/bit_operations/insert_small_bin_into_big_bin.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Given two 32-bit numbers, N and M, and two bit positions, i and j, this
|
||||
method insert M into N such that M starts at bit j and ends at bit i:
|
||||
1) clear the bits j thru i in N'
|
||||
2) shift M so that it lines up with bits j thru i
|
||||
3) merge M and N
|
||||
|
||||
>>> N = 0b10000000000
|
||||
>>> M = 0b10011
|
||||
>>> j = 6
|
||||
>>> i = 2
|
||||
>>> insert_small_bin_into_big_bin(M, N, i, j)
|
||||
'0b10001001100'
|
||||
'''
|
||||
|
||||
def insert_small_bin_into_big_bin(M, N, i, j):
|
||||
allOnes = ~0
|
||||
left = allOnes << (j+1) # 1110000
|
||||
right = ( (1 << i) - 1) # 0000111
|
||||
mask = left | right # 1110111
|
||||
N_cleared = N & mask
|
||||
M_shifted = M << i
|
||||
|
||||
return bin( N_cleared | M_shifted)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
64
src/bitwise/bit_operations/next_with_same_num_1s.py
Normal file
64
src/bitwise/bit_operations/next_with_same_num_1s.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Give a positive int, print the next smallest and next largest ints with
|
||||
same number of 1 bits.
|
||||
The brute force is:
|
||||
1) find number of 1 bits
|
||||
2) loop above and down until find same, checking for each
|
||||
|
||||
>>> num = 0b1001
|
||||
>>> next = '0b1010'
|
||||
>>> prev = '0b110'
|
||||
>>> print_prev_same_1s(num) == prev
|
||||
True
|
||||
>>> print_next_same_1s(num) == next
|
||||
True
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def print_prev_same_1s(num):
|
||||
n1s = find_num_1s(num)
|
||||
# find prev
|
||||
i = num-1
|
||||
while True:
|
||||
n1s_here = find_num_1s(i)
|
||||
if n1s_here == n1s:
|
||||
return bin(i)
|
||||
i -= 1
|
||||
if i < 0:
|
||||
return None
|
||||
|
||||
def print_next_same_1s(num):
|
||||
n1s = find_num_1s(num)
|
||||
# find next
|
||||
i = num+1
|
||||
while True:
|
||||
n1s_here = find_num_1s(i)
|
||||
if n1s_here == n1s:
|
||||
return bin(i)
|
||||
i += 1
|
||||
if i < 0:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def find_num_1s(num):
|
||||
counter = 0
|
||||
while num:
|
||||
if num & 1:
|
||||
counter += 1
|
||||
num >>= 1
|
||||
return counter
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
45
src/bitwise/bit_operations/num_bits_to_convert_2_nums.py
Normal file
45
src/bitwise/bit_operations/num_bits_to_convert_2_nums.py
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' This method returns the number of bits that are necessary to change to convert two
|
||||
numbers A and B:
|
||||
1) XOR
|
||||
2) count 1s
|
||||
|
||||
>>> a = int('10010000', 2)
|
||||
>>> b = int('01011010', 2)
|
||||
>>> count_bits_swap(a, b)
|
||||
4
|
||||
>>> count_bits_swap2(a, b)
|
||||
4
|
||||
'''
|
||||
|
||||
def count_bits_swap2(a, b):
|
||||
count = 0
|
||||
m = a^b
|
||||
while m:
|
||||
count +=1
|
||||
m = m & (m-1)
|
||||
return count
|
||||
|
||||
|
||||
|
||||
def count_bits_swap(a, b):
|
||||
m = a^b
|
||||
return count_1s(m)
|
||||
|
||||
|
||||
def count_1s(m):
|
||||
count = 0
|
||||
while m:
|
||||
if m& 1 :
|
||||
count +=1
|
||||
m >>= 1
|
||||
return count
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
38
src/bitwise/bit_operations/set_bit.py
Normal file
38
src/bitwise/bit_operations/set_bit.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Set a bit in a binary number:
|
||||
1) Shifts 1 over by i bits
|
||||
2) make an OR with the number, only the value at bit i will change and all the others bit
|
||||
of the mask are zero so will not affect the num
|
||||
>>> num = int('0100100', 2)
|
||||
>>> set_bit(num, 0)
|
||||
'0b100101'
|
||||
>>> set_bit(num, 1)
|
||||
'0b100110'
|
||||
>>> set_bit(num, 2) # nothing change
|
||||
'0b100100'
|
||||
>>> set_bit(num, 3)
|
||||
'0b101100'
|
||||
>>> set_bit(num, 4)
|
||||
'0b110100'
|
||||
>>> set_bit(num, 5) # nothing change
|
||||
'0b100100'
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def set_bit(num, i):
|
||||
mask = 1 << i
|
||||
return bin( num | mask )
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
38
src/bitwise/bit_operations/swap_odd_even.py
Normal file
38
src/bitwise/bit_operations/swap_odd_even.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' Swap odd and even bits in a smart way in a binary:
|
||||
1) first for odds, take n and move the odd:
|
||||
(a) Mask all odd bits with 10101010 (0xAA)
|
||||
(b) shift by right by 1
|
||||
2) do the same to ints with 01010101
|
||||
3) merge
|
||||
|
||||
>>> num = 0b11011101
|
||||
>>> result = '0b1101110'
|
||||
>>> swap_odd_even(num) == result
|
||||
True
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def swap_odd_even(num):
|
||||
mask_odd = 0xAA # 0b10101010
|
||||
mask_even = 0x55 # 0b1010101
|
||||
odd = num & mask_odd
|
||||
odd >>= 1
|
||||
even = num & mask_even
|
||||
even >>= 1
|
||||
return bin(odd | even)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
26
src/bitwise/bit_operations/update_bit.py
Normal file
26
src/bitwise/bit_operations/update_bit.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/python3
|
||||
# mari von steinkirch @2013
|
||||
# steinkirch at gmail
|
||||
|
||||
''' This method merges set bit and clean bit:
|
||||
1) first clear the bit at i using a mask such as 1110111
|
||||
2) then shift the intended value v by i bits
|
||||
3) this will create a number with bit i to v and all other to 0
|
||||
4) finally update the ith bit with or
|
||||
|
||||
>>> num = int('10010000', 2)
|
||||
>>> update_bit(num, 2, 1)
|
||||
'0b10010100'
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def update_bit(num, i, v):
|
||||
mask = ~ (1 << i)
|
||||
return bin( (num & mask) | (v << i) )
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue