Skip to content

Commit 8c81eb5

Browse files
authored
Merge pull request BeeBombshell#1 from BeeBombshell/main
Updating
2 parents 4bd9377 + f2c1b97 commit 8c81eb5

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

Backtracking/Knights_tour.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def knights_tour(n, row, col):
2+
visited = [[False]*n for i in range(n)]
3+
res = []
4+
def path_gen(path,r,c,visited,step):
5+
if r < 0 or c < 0 or r >= n or c >= n or visited[r][c]:
6+
return
7+
if step == n*n:
8+
res.append(path + f'({r},{c})')
9+
return
10+
visited[r][c] = True
11+
path_gen(path + f'({r},{c}) ', r - 2, c + 1, visited, step+1)
12+
path_gen(path + f'({r},{c}) ', r - 2, c - 1, visited, step+1)
13+
path_gen(path + f'({r},{c}) ', r - 1, c + 2, visited, step+1)
14+
path_gen(path + f'({r},{c}) ', r - 1, c - 2, visited, step+1)
15+
path_gen(path + f'({r},{c}) ', r + 2, c + 1, visited, step+1)
16+
path_gen(path + f'({r},{c}) ', r + 2, c - 1, visited, step+1)
17+
path_gen(path + f'({r},{c}) ', r + 1, c + 2, visited, step+1)
18+
path_gen(path + f'({r},{c}) ', r + 1, c - 2, visited, step+1)
19+
visited[r][c] = False
20+
path_gen('', row, col,visited,1)
21+
return res
22+
23+
print(knights_tour(5, 2, 2))

Backtracking/N_queens.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def n_queens(n):
2+
res = []
3+
board = [['.']*n for i in range(n)]
4+
cols = set()
5+
pdiags = set()
6+
ndiags = set()
7+
def gen(r):
8+
if r == n:
9+
res.append([''.join(row) for row in board])
10+
return
11+
for c in range(n):
12+
if c in cols or r+c in pdiags or r-c in ndiags:
13+
continue
14+
board[r][c] = 'Q'
15+
cols.add(c)
16+
pdiags.add(r+c)
17+
ndiags.add(r-c)
18+
gen(r + 1)
19+
board[r][c] = '.'
20+
cols.remove(c)
21+
pdiags.remove(r+c)
22+
ndiags.remove(r-c)
23+
gen(0)
24+
return res
25+
26+
print(n_queens(8))

DSA/Queue_using_list.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Implement Queue using List(Functions)
2+
q=[]
3+
def Enqueue():
4+
if len(q)==size: # check wether the stack is full or not
5+
print("Queue is Full!!!!")
6+
else:
7+
element=input("Enter the element:")
8+
q.append(element)
9+
print(element,"is added to the Queue!")
10+
def dequeue():
11+
if not q:# or if len(stack)==0
12+
print("Queue is Empty!!!")
13+
else:
14+
e=q.pop(0)
15+
print("element removed!!:",e)
16+
def display():
17+
print(q)
18+
size=int(input("Enter the size of Queue:"))
19+
while True:
20+
print("Select the Operation:1.Add 2.Delete 3. Display 4. Quit")
21+
choice=int(input())
22+
if choice==1:
23+
Enqueue()
24+
elif choice==2:
25+
dequeue()
26+
elif choice==3:
27+
display()
28+
elif choice==4:
29+
break
30+
else:
31+
print("Invalid Option!!!")

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ The following topics will be elaborated:
3737
* Linear Search
3838
* Binary Search
3939
* Recursion
40+
* Ackermann Function
4041
* Backtracking
4142
* Dynamic Programming
4243
* Linked List

Recursion/Ackermann.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def Ackermann(m, n):
2+
if m == 0:
3+
return n + 1
4+
elif m > 0 and n == 0:
5+
return Ackermann(m - 1, 1)
6+
else:
7+
return Ackermann(m - 1, Ackermann(m, n - 1))
8+
m = int(input())
9+
n = int(input())
10+
print(Ackermann(m, n))

Recursion/is_Palindrome.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''A recursive Python program to check whether a string is palindrome or not'''
2+
3+
def isPalindrome(s, i):
4+
if(i > len(s)/2): #base case
5+
return True
6+
ans = False
7+
if((s[i] is s[len(s) - i - 1]) and isPalindrome(s, i + 1)): #recursive step
8+
ans = True
9+
return ans
10+
11+
str = "racecar"
12+
if (isPalindrome(str, 0)):
13+
print("Yes")
14+
else:
15+
print("No")
16+

0 commit comments

Comments
 (0)