mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-08 23:22:48 -04:00
cleaning up and organizing old problems (builtin)
This commit is contained in:
parent
6afe96fa4d
commit
3fdbc2a605
106 changed files with 480 additions and 1472 deletions
33
src/bitwise/bitwise.txt
Executable file
33
src/bitwise/bitwise.txt
Executable file
|
@ -0,0 +1,33 @@
|
|||
BIT-WISE
|
||||
----------------------
|
||||
|
||||
1. To find a number:
|
||||
11000101 is 2^0+2^2+2^6+2^7 = 197
|
||||
|
||||
|
||||
2. Left shifting:
|
||||
0010 1011 << 4 ---> 1011 000
|
||||
|
||||
|
||||
3. Right shifting:
|
||||
0010 1011 >> 4 ---> 0000 0010
|
||||
or it can be filled with the copy of the first bit, instead of 0:
|
||||
1011 0010 >> 4 ---> 1111 1011
|
||||
|
||||
|
||||
4. XOR can cancels out:
|
||||
15 ^ 12 ^ 15 = 12
|
||||
|
||||
|
||||
5. 2^x:
|
||||
left-shift 1 by x:
|
||||
0000 0001 << x
|
||||
|
||||
so if x = 2, 2^2 = 4 -> 100
|
||||
|
||||
0000 0001 << 2 ---> 0000 0100
|
||||
|
||||
|
||||
6. Is power of 2?
|
||||
just do x&(x-1).
|
||||
if 0 --> yes!
|
37
src/bitwise/clear_bits.py
Executable file
37
src/bitwise/clear_bits.py
Executable file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' Clear a bit in a binary number.
|
||||
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
|
||||
'''
|
||||
|
||||
|
||||
|
||||
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__':
|
||||
num = int('10010000', 2)
|
||||
print clear_bit(num, 4) # '0b10000000'
|
||||
|
||||
num = int('10010011', 2)
|
||||
print clear_all_bits_from_i_to_0(num, 2) # '0b10010000'
|
||||
|
||||
num = int('1110011', 2)
|
||||
print clear_all_bits_from_most_sig_to_1(num, 2) #'0b11'
|
25
src/bitwise/find_bit_len.py
Executable file
25
src/bitwise/find_bit_len.py
Executable file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' 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))
|
||||
'''
|
||||
|
||||
|
||||
def find_bit_len(int_num):
|
||||
lenght = 0
|
||||
while int_num:
|
||||
int_num >>= 1
|
||||
lenght += 1
|
||||
return lenght
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(17):
|
||||
print(find_bit_len(i))
|
||||
print i.bit_length()
|
||||
print
|
27
src/bitwise/get_bit.py
Executable file
27
src/bitwise/get_bit.py
Executable file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' 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
|
||||
'''
|
||||
|
||||
|
||||
def get_bit(num, i):
|
||||
mask = 1 << i
|
||||
return num & mask != 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
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
|
||||
|
40
src/bitwise/num_bits_to_convert_2_nums.py
Executable file
40
src/bitwise/num_bits_to_convert_2_nums.py
Executable file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
''' This method returns the number of bits that are necessary to change to convert two
|
||||
numbers A and B:
|
||||
1) XOR
|
||||
2) count 1s
|
||||
'''
|
||||
|
||||
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__':
|
||||
a = int('10010000', 2)
|
||||
b = int('01011010', 2)
|
||||
print count_bits_swap(a, b) #4
|
||||
print count_bits_swap2(a, b) #4
|
30
src/bitwise/set_bit.py
Executable file
30
src/bitwise/set_bit.py
Executable file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
''' 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
|
||||
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def set_bit(num, i):
|
||||
mask = 1 << i
|
||||
return bin( num | mask )
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
num = int('0100100', 2)
|
||||
print set_bit(num, 0) #'0b100101'
|
||||
print set_bit(num, 1) #'0b100110'
|
||||
print set_bit(num, 2) # nothing change '0b100100'
|
||||
print set_bit(num, 3) #'0b101100'
|
||||
print set_bit(num, 4) #'0b110100'
|
||||
print set_bit(num, 5) # nothing change '0b100100'
|
21
src/bitwise/swap_in_place.py
Executable file
21
src/bitwise/swap_in_place.py
Executable file
|
@ -0,0 +1,21 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
'''
|
||||
swapping values in place without extra memory
|
||||
'''
|
||||
|
||||
|
||||
def swap_bit(a, b):
|
||||
a = a^b
|
||||
b = a^b
|
||||
a = a^b
|
||||
return a, b
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
a = 14
|
||||
b = 73
|
||||
a2, b2 = swap_bit(a, b)
|
||||
print "a was {0}, now it is {1}. \nb was {2}, now it is {3}".format(a, a2, b, b2)
|
22
src/bitwise/update_bit.py
Executable file
22
src/bitwise/update_bit.py
Executable file
|
@ -0,0 +1,22 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
''' 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
|
||||
'''
|
||||
|
||||
|
||||
|
||||
def update_bit(num, i, v):
|
||||
mask = ~ (1 << i)
|
||||
return bin( (num & mask) | (v << i) )
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
num = int('10010000', 2)
|
||||
print update_bit(num, 2, 1) # '0b10010100'
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue