diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2feb5e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +#Ignoring dir +.*/ \ No newline at end of file diff --git a/classwork/01/reverse.py b/classwork/01/reverse.py new file mode 100644 index 0000000..6305354 --- /dev/null +++ b/classwork/01/reverse.py @@ -0,0 +1,10 @@ +# Function to reverse array + +def reverse(A): + A = A[::-1] + return A + + +if __name__ == '__main__': + a = [1, 2, 3, 4, 5] + print(reverse(a)) diff --git a/classwork/02/sum_target.py b/classwork/02/sum_target.py new file mode 100644 index 0000000..5d61d9c --- /dev/null +++ b/classwork/02/sum_target.py @@ -0,0 +1,19 @@ +def target_sum(arr, target): + sum_indices = [] + + for i in range(len(arr)): + for j in range(i, len(arr) - 1): + if arr[i] + arr[j+1] == target: + sum_indices = [i, j + 1] + + break + else: + continue + break + + return sum_indices + + +if __name__ == '__main__': + print(target_sum([2, 4, 11, 8], 12)) + print(target_sum([2, 3, 4], 6)) diff --git a/classwork/03/evaluate.py b/classwork/03/evaluate.py new file mode 100644 index 0000000..c759dce --- /dev/null +++ b/classwork/03/evaluate.py @@ -0,0 +1,30 @@ +import re + + +def evaluate(S): + # Remove all whitespaces in string + s = S.replace(' ', '') + + # Split string at operators into array + arr = re.split(r'(\D)', s) + + # Intialize first element of array as result + result = int(arr[0]) + + # Iterate through the array operating on the numbers depending on operator + # and update result + for i in range(0, len(arr) - 2, 2): + + if arr[i + 1] == '+': + result += int(arr[i + 2]) + + else: + result -= int(arr[i + 2]) + + return result + + +if __name__ == '__main__': + s = "8 + 3 + 6 + 2 - 3" + + print(evaluate(s)) diff --git a/classwork/04/singly_linked_list.py b/classwork/04/singly_linked_list.py new file mode 100644 index 0000000..13c3a25 --- /dev/null +++ b/classwork/04/singly_linked_list.py @@ -0,0 +1,89 @@ +# the Node class - contains value and address to next node +class Node(object): + def __init__(self, val): + self.val = val + self.next = None + + def get_data(self): + return self.val + + def set_data(self, val): + self.val = val + + def get_next(self): + return self.next + + def set_next(self, next): + self.next = next + + +# the LinkedList class +class LinkedList(object): + def __init__(self, head=None): + self.head = head + self.count = 0 + + def get_count(self): + return self.count + + def insert(self, data): + new_node = Node(data) + new_node.set_next(self.head) + self.head = new_node + self.count += 1 + + def find(self, val): + item = self.head + while (item != None): + if item.get_data() == val: + return item + else: + item = item.get_next() + return None + + def deleteAt(self, idx): + if idx > self.count: + return + if self.head == None: + return + else: + tempIdx = 0 + node = self.head + while tempIdx < idx-1: + node = node.get_next() + tempIdx += 1 + node.set_next(node.get_next().get_next()) + self.count -= 1 + + def printList(self): + tempnode = self.head + while (tempnode != None): + print("Node: ", tempnode.get_data()) + tempnode = tempnode.get_next() + + def sumList(self): + tempnode = self.head + self.sum = 0 + while (tempnode != None): + self.sum += tempnode.get_data() + tempnode = tempnode.get_next() + print('Sum of list:', self.sum) + + + +if __name__ == "__main__": + # create a linked list and insert some items + itemlist = LinkedList() + itemlist.insert(3) + itemlist.insert(10) + itemlist.insert(1) + itemlist.insert(5) + itemlist.insert(6) + + #Print the List + itemlist.printList() + + #GEt sum + itemlist.sumList() + +