mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
reorganize dir
Signed-off-by: Mia Steinkirch <mia.steinkirch@gmail.com>
This commit is contained in:
parent
1b6f705e7c
commit
a8e71c50db
276 changed files with 23954 additions and 0 deletions
35
other_resources/interview_cake/bitwise_stuff/clean_bit.py
Normal file
35
other_resources/interview_cake/bitwise_stuff/clean_bit.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
''' 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'
|
37
other_resources/interview_cake/bitwise_stuff/count_bits.py
Normal file
37
other_resources/interview_cake/bitwise_stuff/count_bits.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
''' 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
|
20
other_resources/interview_cake/bitwise_stuff/get_bit.py
Normal file
20
other_resources/interview_cake/bitwise_stuff/get_bit.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/python
|
||||
''' 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'
|
19
other_resources/interview_cake/bitwise_stuff/update_bit.py
Normal file
19
other_resources/interview_cake/bitwise_stuff/update_bit.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
''' 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