diff --git a/interview_cake/binary_search.py b/interview_cake/binary_search.py new file mode 100644 index 0000000..fef66b5 --- /dev/null +++ b/interview_cake/binary_search.py @@ -0,0 +1 @@ +#!/usr/bin/env python \ No newline at end of file diff --git a/interview_cake/bitwise_stuff/clean_bit.py b/interview_cake/bitwise_stuff/clean_bit.py new file mode 100644 index 0000000..6222c15 --- /dev/null +++ b/interview_cake/bitwise_stuff/clean_bit.py @@ -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' \ No newline at end of file diff --git a/interview_cake/bitwise_stuff/count_bits.py b/interview_cake/bitwise_stuff/count_bits.py new file mode 100644 index 0000000..596c5cd --- /dev/null +++ b/interview_cake/bitwise_stuff/count_bits.py @@ -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 \ No newline at end of file diff --git a/interview_cake/bitwise_stuff/get_bit.py b/interview_cake/bitwise_stuff/get_bit.py new file mode 100644 index 0000000..ab768ae --- /dev/null +++ b/interview_cake/bitwise_stuff/get_bit.py @@ -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' \ No newline at end of file diff --git a/interview_cake/bitwise_stuff/update_bit.py b/interview_cake/bitwise_stuff/update_bit.py new file mode 100644 index 0000000..78e1413 --- /dev/null +++ b/interview_cake/bitwise_stuff/update_bit.py @@ -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' \ No newline at end of file diff --git a/interview_cake/float_bin_num.py b/interview_cake/float_bin_num.py new file mode 100644 index 0000000..3ca34ad --- /dev/null +++ b/interview_cake/float_bin_num.py @@ -0,0 +1,26 @@ +#!/usr/bin/python + +''' 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: +''' + +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__': + print get_float_rep(0.72) #('Error 2', '.1011100001010001111010111000010') + print get_float_rep(0.1) # ('Error 2', '.0001100110011001100110011001100') + print get_float_rep(0.5) #'.1' \ No newline at end of file diff --git a/interview_cake/merge_sorted_list.py b/interview_cake/merge_sorted_list.py index 44473e8..2c9aeb0 100644 --- a/interview_cake/merge_sorted_list.py +++ b/interview_cake/merge_sorted_list.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """ In order to win the prize for most cookies sold, my friend Alice and # I are going to merge our Girl Scout Cookies orders and enter as one unit. diff --git a/interview_cake/palindrome.py b/interview_cake/palindrome.py index ef7bcb0..0caba72 100644 --- a/interview_cake/palindrome.py +++ b/interview_cake/palindrome.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + """ Write an efficient function that checks whether any permutation of an input string is a palindrome. diff --git a/interview_cake/reverse_in_place.py b/interview_cake/reverse_in_place.py index 3f1d7f4..9f5e801 100644 --- a/interview_cake/reverse_in_place.py +++ b/interview_cake/reverse_in_place.py @@ -1,5 +1,9 @@ -# Write a function that takes a list of characters and reverses the letters in place. -# O(n) time and O(1)O(1) space. +#!/usr/bin/env python + +""" +Write a function that takes a list of characters and reverses the letters in place. +O(n) time and O(1)O(1) space. +""" def reverse_in_place(char_list): return char_list[::-1]