Advertisement
jbn6972

Untitled

Aug 1st, 2022
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. -----------PERMUTATION/MEMBER/CONCATENATION---------------------
  2. % ---------------------------------------------------------------------------------------------------------
  3. % Permuatation      input = perm([a,b,c,d],X).
  4. perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm).
  5. perm([],[]).
  6.    
  7. delete(X,[X|T],T).
  8. delete(X,[H|T],[H|NT]):-delete(X,T,NT).
  9.  
  10. % ---------------------------------------------------------------------------------------------------------
  11. % Memeber       input = list_member(b,[a,b,c]).
  12. list_member(X,[X|_]).
  13. list_member(X,[_|TAIL]) :- list_member(X,TAIL).
  14.  
  15. % ---------------------------------------------------------------------------------------------------------
  16. % Concatenation         input = list_concat([1,2],[a,b,c],NewList).
  17. list_concat([],L,L).
  18. list_concat([X1|L1],L2,[X1|L3]) :- list_concat(L1,L2,L3).
  19.  
  20.  
  21. ----------------------INTERSECTION UNION----------------------------------
  22. % ---------------------------------------------------------------------------------------------------------
  23. % Union      input = list_union([a,b,c,d,e],[a,e,i,o,u],L3).
  24. list_member(X,[X|_]).
  25. list_member(X,[_|TAIL]) :- list_member(X,TAIL).
  26.  
  27. list_union([X|Y],Z,W) :- list_member(X,Z),list_union(Y,Z,W).
  28. list_union([X|Y],Z,[X|W]) :- \+ list_member(X,Z), list_union(Y,Z,W).
  29. list_union([],Z,Z).
  30.  
  31. % ---------------------------------------------------------------------------------------------------------
  32. % Intersection      input = list_intersect([a,b,c,d,e],[],L3).
  33. list_member(X,[X|_]).
  34. list_member(X,[_|TAIL]) :- list_member(X,TAIL).
  35.  
  36. list_intersect([X|Y],Z,[X|W]) :-
  37.    list_member(X,Z), list_intersect(Y,Z,W).
  38. list_intersect([X|Y],Z,W) :-
  39.    \+ list_member(X,Z), list_intersect(Y,Z,W).
  40. list_intersect([],Z,[]).
  41.  
  42. ---------------------------------MONKEY BANANA--------------------------------------------------------
  43. % ---------------------------------------------------------------------------------------------------------
  44. % Union      input = list_union([a,b,c,d,e],[a,e,i,o,u],L3).
  45. list_member(X,[X|_]).
  46. list_member(X,[_|TAIL]) :- list_member(X,TAIL).
  47.  
  48. list_union([X|Y],Z,W) :- list_member(X,Z),list_union(Y,Z,W).
  49. list_union([X|Y],Z,[X|W]) :- \+ list_member(X,Z), list_union(Y,Z,W).
  50. list_union([],Z,Z).
  51.  
  52. % ---------------------------------------------------------------------------------------------------------
  53. % Intersection      input = list_intersect([a,b,c,d,e],[],L3).
  54. list_member(X,[X|_]).
  55. list_member(X,[_|TAIL]) :- list_member(X,TAIL).
  56.  
  57. list_intersect([X|Y],Z,[X|W]) :-
  58.    list_member(X,Z), list_intersect(Y,Z,W).
  59. list_intersect([X|Y],Z,W) :-
  60.    \+ list_member(X,Z), list_intersect(Y,Z,W).
  61. list_intersect([],Z,[]).
  62.  
  63.  
  64.  
  65. ---------------------------------------------BFS------------------------------------------------------------------
  66. from collections import defaultdict
  67. graph = defaultdict(list)
  68.  
  69. def addEdge(u,v):
  70.     graph[u].append(v)
  71.     graph[v].append(u)
  72.  
  73. def BFS(source):
  74.     open = []
  75.     closed = []
  76.     open.append(source)
  77.     while len(open) > 0:
  78.         node = open[0]
  79.         print(node,sep=" ")
  80.         closed.append(node)
  81.         open.pop(0)
  82.         # print(top)
  83.         neighbours = graph[node]
  84.  
  85.         for neighbour in neighbours:
  86.             if neighbour not in open and neighbour not in closed:
  87.                 # open = open.append(neighbour)
  88.                 open = open + [neighbour]
  89.  
  90.  
  91. addEdge(1,2)
  92. addEdge(1,3)
  93. addEdge(2,4)
  94. addEdge(2,5)
  95. addEdge(3,6)
  96. addEdge(3,7)
  97. BFS(1)
  98.  
  99.  
  100. --------------------------------------------DFS-----------------------------------------------------
  101. from collections import defaultdict
  102.  
  103. graph = defaultdict(list)
  104.  
  105. def addEdge(u,v):
  106.     graph[u].append(v)
  107.     graph[v].append(u)
  108.  
  109. def BFS(source):
  110.     open = []
  111.     closed = []
  112.     open.append(source)
  113.     while len(open) > 0:
  114.         node = open[0]
  115.         print(node,sep=" ")
  116.         closed.append(node)
  117.         open.pop(0)
  118.         # print(top)
  119.         neighbours = graph[node]
  120.  
  121.         for neighbour in neighbours:
  122.             if neighbour not in open and neighbour not in closed:
  123.                 # open = open.append(neighbour)
  124.                 open = [neighbour] + open
  125.  
  126. addEdge(1,2)
  127. addEdge(1,3)
  128. addEdge(2,4)
  129. addEdge(2,5)
  130. addEdge(3,6)
  131. addEdge(3,7)
  132. BFS(1)
  133.  
  134. ----------------------------------------------------N-Queens--------------------------------------------------------------------
  135. def isSafe(board,row,col):
  136.     for i in range(col):
  137.         if board[row][i] == 1:
  138.             return False
  139.  
  140.     for i,j in zip(range(row,-1,-1),range(col,-1,-1)):
  141.         if board[i][j] == 1:
  142.             return False
  143.  
  144.  
  145.     for i,j in zip(range(row,N),range(col,-1,-1)):
  146.         if board[i][j] == 1:
  147.             return False
  148.  
  149.     return True
  150.  
  151. def solve(board,col):
  152.     if col >= N:
  153.         return True
  154.  
  155.     for row in range(N):
  156.         if isSafe(board,row,col):
  157.             board[row][col] = 1
  158.  
  159.             if solve(board,col+1):
  160.                 return True
  161.  
  162.             board[row][col] = 0
  163.  
  164.     return False
  165.  
  166. N = 3
  167. board = [[0 for _ in range(N)] for _ in range(N)]
  168. if solve(board,0):
  169.     for row in board:
  170.         print(row)
  171. else:
  172.     print("Cant place "+ str(N) + " queens on NxN matrix")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement