Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrote different way to Graph Implementation.py #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
53 changes: 53 additions & 0 deletions Data Structures - Graph/Graph Implementation 2nd Way.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Graph:
def __init__(self):
self.adj_list = {}

def print_graph(self):
for vertex in self.adj_list:
print(vertex, ':', self.adj_list[vertex])

def add_vertex(self, vertex):
if vertex not in self.adj_list.keys():
self.adj_list[vertex] = []
return True
return False

def add_edge(self, v1, v2):
if v1 in self.adj_list.keys() and v2 in self.adj_list.keys():
self.adj_list[v1].append(v2)
self.adj_list[v2].append(v1)
return True
return False

def remove_edge(self, v1, v2):
if v1 in self.adj_list.keys() and v2 in self.adj_list.keys():
self.adj_list[v1].remove(v2)
self.adj_list[v2].remove(v1)
return True
return False

def remove_vertex(self, vertex):
if vertex in self.adj_list.keys():
for other_vertex in self.adj_list[vertex]:
self.adj_list[other_vertex].remove(vertex)
del self.adj_list[vertex]
return True
return False


my_graph = Graph()
my_graph.add_vertex('A')
my_graph.add_vertex('B')
my_graph.add_vertex('C')
my_graph.add_vertex('D')

my_graph.add_edge('A','B')
my_graph.add_edge('A','C')
my_graph.add_edge('A','D')
my_graph.add_edge('B','D')
my_graph.add_edge('C','D')

my_graph.remove_vertex('D')

my_graph.print_graph()

53 changes: 53 additions & 0 deletions Data Structures - Linked Lists/RemoveDuplicatesFromLinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This is an input class. Do not edit.
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None

def addMany(self, values):
current = self
while current.next is not None:
current = current.next
for value in values:
current.next = LinkedList(value)
current = current.next
return self

def getNodesInArray(self):
nodes = []
current = self
while current is not None:
nodes.append(current.value)
current = current.next
return nodes

def removeDuplicatesFromLinkedList(linkedList):

slowptr = linkedList
while slowptr.next is not None:
if slowptr.value == slowptr.next.value:
slowptr.next = slowptr.next.next
else:
slowptr = slowptr.next
return linkedList



llist = LinkedList(1)

llist.addMany([1, 3, 4, 4, 4, 5, 6, 6])
print(llist.getNodesInArray())

removeDuplicatesFromLinkedList(llist)
print(llist.getNodesInArray())


# O(n) -> time
# O(1) -> space



# Write your code here.
# # [1, 1, 3, 4, 4, 4, 5, 6, 6]

# if len(linkedList.getNodesInArray()) > 1: