From 033c4343616dc04daf3bc1e0ad9a5e7e8a7dd936 Mon Sep 17 00:00:00 2001 From: Lucas Pierre de Alencar Date: Fri, 11 Oct 2019 13:37:53 -0300 Subject: [PATCH 1/2] create LinkedList.py --- LinkedList.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 LinkedList.py diff --git a/LinkedList.py b/LinkedList.py new file mode 100644 index 0000000..64b90ea --- /dev/null +++ b/LinkedList.py @@ -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) From 5c87901c9baca96054ba26b1eff1ed66bd4e6d23 Mon Sep 17 00:00:00 2001 From: Lucas Pierre de Alencar Date: Fri, 11 Oct 2019 13:42:08 -0300 Subject: [PATCH 2/2] create binary_search.py --- binary_search.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 binary_search.py diff --git a/binary_search.py b/binary_search.py new file mode 100644 index 0000000..2d189fc --- /dev/null +++ b/binary_search.py @@ -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