diff --git a/Backtracking/binary_string.py b/Backtracking/binary_string.py new file mode 100644 index 0000000..acbee29 --- /dev/null +++ b/Backtracking/binary_string.py @@ -0,0 +1,22 @@ +# To print all the binary string for a given 'n' value + +def binary(n,bi): + + if (n == -1): + print ''.join(bi) + return + else: + bi[n] = str(0) + binary(n-1, bi) + bi[n] = str(1) + binary(n-1, bi) + + +n = input() +bi = [] + +# Defining a list of size n with dummy values +for i in range(n): + bi.append(-1) + +binary(n-1, bi) diff --git a/Backtracking/coin_denom.py b/Backtracking/coin_denom.py new file mode 100644 index 0000000..3efd32d --- /dev/null +++ b/Backtracking/coin_denom.py @@ -0,0 +1,34 @@ +""" +Coin denomination problem - Topdown Memoization Approach (Dynamic Programming) +Example: Given the coins with denomination [1,2,3]. Assume that there are infinite number of coins. Find the minimum number of coins to get the value 'x'. +""" + +# maxint returns maximum integer value supported python 2.7 +from sys import maxint + + +def coin_denom(coins,dictionary,remain): + + if remain == 0: + return 0 + elif remain < 0: + return -1 + + if remain in dictionary: + return dictionary[remain] + + # In Python 2.7, maximum integer value is 2147483647 + min_val = maxint + + for i in coins: + temp = 1 + coin_denom(coins,dictionary,remain-i) + if temp < min_val and temp != 0: + min_val = temp + + if remain not in dictionary: + dictionary[remain] = min_val + + return min_val + + ### Testcases ### +#print coin_denom([1,2,3,4], {}, 7) diff --git a/Backtracking/n_queen.py b/Backtracking/n_queen.py new file mode 100644 index 0000000..63029c3 --- /dev/null +++ b/Backtracking/n_queen.py @@ -0,0 +1,63 @@ +""" +N-Queen problem +Time Complexiy is exponential (it is based on the size of the table) +Space Complexiy - O(n) +""" + +class N_Queen(object): + + def possibility(self,row,column,q_row,q_column): + left_diagonal = q_row + q_column + right_diagonal = q_row - q_column + + if q_row == row: + return False + elif q_column == column: + return False + elif row + column == left_diagonal: + return False + elif row - column == right_diagonal: + return False + else: + return True + + + def queen(self,n,row,previous_queen,result): + if row == n: + print result + return True + + # Iterating the columns + for j in range(n): + valid = True + for q in range(len(previous_queen)): + if(not(self.possibility(row,j,previous_queen[q][0],previous_queen[q][1]))): + valid = False + break + + if(valid): + previous_queen.append([row,j]) + result[row][j] = 'Queen' + if(self.queen(n,row+1,previous_queen,result)): + return True + else: + previous_queen.remove([row,j]) + result[row][j] = 0 + + return False + + + def solveNQueens(self, n): + + result = [] + for i in range(n): + result.append([]) + for j in range(n): + # Void is zero - 0 + result[i].append(0) + previous_queen = [] + self.queen(n,0,previous_queen,result) + + +obj = N_Queen() +obj.solveNQueens(4) diff --git a/Backtracking/string_permutations.py b/Backtracking/string_permutations.py new file mode 100644 index 0000000..aca29ba --- /dev/null +++ b/Backtracking/string_permutations.py @@ -0,0 +1,19 @@ +""" +Print all the string permutations using backtracking method. +""" +def permutations(string,index,size): + + if index == size: + print ''.join(string) + return + + for i in range(index,size): + string[i], string[index] = string[index], string[i] + permutations(string,index+1,size) + string[i], string[index] = string[index], string[i] + + +string = raw_input() +string = list(string) +size = len(string) +print permutations(string,0,size) diff --git a/Bit manipulation/brianKerninghan.py b/Bit manipulation/brianKerninghan.py new file mode 100644 index 0000000..bdfbb23 --- /dev/null +++ b/Bit manipulation/brianKerninghan.py @@ -0,0 +1,17 @@ +""" +Brian Kerninghan - Count the number of set bits in an integer +Time Complexity - O(log n) +Key idea: n & (n-1) +""" + +def brianKerninghan(n): + count = 0 + while (n): + n = n & (n-1) + count += 1 + return count + + +# ### Testcases ### +# print(brianKerninghan(7)) +# print(brianKerninghan(1024)) diff --git a/Bit manipulation/checkBitSet.py b/Bit manipulation/checkBitSet.py new file mode 100644 index 0000000..96b22d2 --- /dev/null +++ b/Bit manipulation/checkBitSet.py @@ -0,0 +1,15 @@ +""" +To check whether the m'th bit in an integer is set or not +""" + +def checkBitSet(n, m): + aux = 1 << (m-1) + if (n & m): + return True + else: + return False + + +# ### Testcases ### +# print(checkBitSet(5,2)) +# print(checkBitSet(5,3)) diff --git a/Bit manipulation/checkTwoPower.py b/Bit manipulation/checkTwoPower.py new file mode 100644 index 0000000..008e0c4 --- /dev/null +++ b/Bit manipulation/checkTwoPower.py @@ -0,0 +1,15 @@ +""" +To check whether a number is divisible by powers of 2 +""" + +def checkTwoPower(n): + if (not(n & n -1)): + return True + else: + return False + + +# ### Testcases ### +# print(checkTwoPower(8)) +# print(checkTwoPower(5)) +# print(checkTwoPower(1))