Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Backtracking/binary_string.py
Original file line number Diff line number Diff line change
@@ -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)
34 changes: 34 additions & 0 deletions Backtracking/coin_denom.py
Original file line number Diff line number Diff line change
@@ -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)
63 changes: 63 additions & 0 deletions Backtracking/n_queen.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions Backtracking/string_permutations.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions Bit manipulation/brianKerninghan.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions Bit manipulation/checkBitSet.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions Bit manipulation/checkTwoPower.py
Original file line number Diff line number Diff line change
@@ -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))