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

create LinkedList.py #9

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
73 changes: 73 additions & 0 deletions LinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None

class LinkedList:
def __init__(self, *, ls = []):
self.__head = None
self.__sz = 0
for x in ls[::-1]:
self.push_front(x)

def __str__(self):
temp = self.__head
ret = "None" if temp is None else ""
while temp:
ret += str(temp.data) + " "
temp = temp.next
return ret

def __len__(self):
return self.__sz

def __del__(self):
self.__head = None

def push_front(self, data):
new_node = Node(data)
new_node.next = self.__head
self.__head = new_node
self.__sz += 1

def push_back(self, data):
if self.__head is None:
self.__head = Node(data)
self.__sz += 1
return

last = self.__head
while last.next:
last = last.next
last.next = Node(data)
self.__sz += 1


def array(self):
temp = self.__head
newList = []
while temp:
newList.append(temp.data)
temp = temp.next
return newList

def matrix(self, row, col):
if row * col < self.__sz:
print("Data is insufficient")
return
mat = []
temp = self.__head
for i in range(row):
res = []
for j in range(col):
res.append(temp.data)
temp = temp.next
mat.append(res)
return mat

if __name__ == '__main__':
newList = LinkedList(ls = [1, 2, 3, 4, 5])
arr = newList.array()
print(arr)
print(len(newList))
print(newList)
15 changes: 15 additions & 0 deletions binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def search(list, element):
first = 0
last = len(list)-1

while first <= last:
mid = (first + last) // 2
print(mid)
if list[mid] == element:
return mid
else:
if element < list[mid]:
last = mid-1
else:
first = mid + 1
return False