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

pusding das to codesee #80

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
47 changes: 47 additions & 0 deletions data_struct/myexamples/algo/bfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
def how_it_workd(data, start, end, visited=[]):
queue = [start]

while queue:
current_node = queue.pop(0)

visited.append(current_node)
print(visited)

for i in data[current_node] - set(visited):
print("for looped --> {}".format(i))
print()
queue.append(i)

print("wants the data being bf-searched")
print(data)


def bfs(data, start, end, visited=[]):
queue = [start]

while queue:
current_node = queue.pop(0)
if current_node==end:
print("Path: " + "->".join(visited) + "->" + end)
return
visited.append(current_node)

for i in data[current_node] - set(visited):
print("for looping -->",i)
queue.append(i)
print("Path does not exist!")


if __name__ == '__main__':
data = {
'A': {'B'},
'B': {'C', 'D'},
'C': {'E'},
'D': {'E'},
'E': {'F'},
'F': set()
}
print("how it works")
how_it_workd(data, "A", "D")
print("out come")
bfs(data, 'A', 'D')
48 changes: 48 additions & 0 deletions data_struct/myexamples/algo/working_bfs_2nd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class node:
def __init__(self,val=None):
self.val = val
self.l = None
self.r = None

class bst:
def __init__(self):
self.head = None

def lvl_order(self):
""""
level order queue:
1.)give q the starting pos
2.)as long as the q is full

3.) take first thing in q mark as visited
4.) check if that popped items has children
if they do put then in the queue

""""
vis = []
q = []
q.append(self.head)

while len(q) > 0:
cur = q.pop(0)
vis.append(cur)

if cur.l:
q.append(cur.l)
if cur.r:
q.append(cur.r)

for x in vis:
print(x.val)

t = bst()
t.head = node(4)
t.head.l = node(2)
t.head.r = node(8)
t.head.l.l = node(1)
t.head.l.r = node(3)
t.head.r.l = node(5)
t.head.r.r = node(10)
t.lvl_order()


77 changes: 77 additions & 0 deletions data_struct/myexamples/bst.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class bst:
def __init__(self,val=None):
self.val = val
self.l = None
self.r = None


def inorder(self):

if self.l:
self.l.postorder()
print(self.val)
if self.r:
self.r.postorder()

def postorder(self):

print(self.val)
if self.l:
self.l.postorder()
if self.r:
self.r.postorder()

def insert(self,val):

if val < self.val:
if self.l == None:
self.l = bst(val)
else:
self.l.insert(val)
else:
if self.r == None:
self.r = bst(val)
else:
self.r.insert(val)

def prt2d(self,sp,h):
#dis between lv
sp += h

cur = self

if cur is None:
return

bst.prt2d(cur.r,sp,h)
print()
for i in range(h,sp):
print(" ",end="")
print(cur.val,end="")
print()
bst.prt2d(cur.l,sp,h)

tree = [bst(50),bst(50)]

tree[0].insert(20)
tree[0].insert(16)
tree[0].insert(10)
tree[0].insert(18)
tree[0].insert(60)
tree[0].insert(70)
tree[0].insert(65)
tree[0].insert(100)

tree[0].postorder()
print()
print("displaying 2d tree")
tree[0].prt2d(0,5)

print("##################")

tree[1].insert(40)
tree[1].insert(60)
tree[1].inorder()
print()
print("displaying 2d tree")
tree[1].prt2d(0,5)
35 changes: 35 additions & 0 deletions data_struct/myexamples/circular-linked.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class node:
def __init__(self,val=None):
self.val = val
self.next = None
self.prev = None

class linked:
def __init__(self):
self.head = None

def insert(self,val):
cur = self.head
prev = None
nd = node(val)

if cur is None:
cur = nd
else:
while cur.next:
cur = cur.next
cur.next = nd
nd.next = cur



l = linked()

l.head = node("start")
l.insert("a")


print("test")
print(l.head.val)
print(l.head.next.val)
print(l.head.next.next.val)
48 changes: 48 additions & 0 deletions data_struct/myexamples/double-ll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class node:
def __init__(self,val=None):
self.val = val
self.next = None
self.prev = None

class linked:
def __init__(self):
self.head = None
self.tail = None

def insert(self,val):
cur = self.head
tail = self.head
nd = node(val)

if cur is None:
cur = nd

while cur.next:
cur = cur.next
cur.next = nd
nd.prev = cur

def display_next(self):
cur = self.head
while cur :
print(cur.val,"-->",end="")
cur = cur.next
print("None")

def display_prev(self):
cur = self.head
while cur :
print(cur.val,"<--",end="")
cur = cur.next
print("None")

l = linked()
l.head = node("a")
l.insert("b")
l.insert("c")
l.insert("d")
l.insert("e")
l.insert("f")
l.insert("g")
l.display_next()
l.display_prev()
52 changes: 52 additions & 0 deletions data_struct/myexamples/example-hashmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#hashtable

class hashmap:
def __init__(self):
self.size = 20
self.ht = [[] for _ in range(0, self.size)]

def set(self,key,data):
hashed_key = hash(key) % self.size
buckets = self.ht[hashed_key]

exist = False
for i , kv in enumerate(buckets):
k,v = kv
if key == k:
exist = True
break

if exist:
buckets[i] = ((key,data))
else:
buckets.append((key,data))

def get(self,key):

hashed_key = hash(key) % self.size
buckets = self.ht[hashed_key]

found = False
for i , kv in enumerate(buckets):
k,v = kv
if key == k:
found = True
break
if found:
return key
else:
print("not found")


h = hashmap()


h.set("big_house", "martell")
h.set("med_house", "tony")
h.set("small_house", "emma")

print(h.ht)




Loading