From d7c9678e880eca3f86464f85711cf4b6af585e23 Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Tue, 9 Jun 2020 16:16:41 -0500 Subject: [PATCH 1/7] Week 2 String Manipulation --- week-2/Python/Erik-Torres.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 week-2/Python/Erik-Torres.py diff --git a/week-2/Python/Erik-Torres.py b/week-2/Python/Erik-Torres.py new file mode 100644 index 0000000..05a84ec --- /dev/null +++ b/week-2/Python/Erik-Torres.py @@ -0,0 +1,31 @@ +# Given a string, find the longest substring which is palindrome. For example, if the given string is "ababad", the output should be "ababa". +def longest(s): + palindrome = '' + for i in range(1, len(s) + 1): + for j in range(len(s) - i + 1): + ss = s[j:j + i] + if isPalindrome(ss) and len(ss) > len(palindrome): + palindrome = ss + return palindrome + + +def isPalindrome(s): + s_reverse = s[::-1] + if s == s_reverse: + return s + else: + return None + + +string = longest("ababad") +print(string) + +# Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For example, if given "abb", the output should be "abb abb bab bba bab bba" +from itertools import permutations + +string = "abb" + +lists = permutations(string) + +for i in list(lists): + print(''.join(i), end=' ') From 795f72ce0a7701751e49d120aeefcacfaf5a6e27 Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 24 Jun 2020 01:13:49 -0500 Subject: [PATCH 2/7] Erik-Torres --- week-4/Python/Erik-Torres | 173 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 week-4/Python/Erik-Torres diff --git a/week-4/Python/Erik-Torres b/week-4/Python/Erik-Torres new file mode 100644 index 0000000..038d6dd --- /dev/null +++ b/week-4/Python/Erik-Torres @@ -0,0 +1,173 @@ +# Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. + +class ListNode: + def __init__(self, data): + self.data = data + self.next = None + + + +def createList1(): + head = ListNode(5) + head.next = ListNode(6) + head.next.next = ListNode(3) + return head + + + +def createList2(): + head = ListNode(8) + head.next = ListNode(4) + head.next.next = ListNode(2) + return head + + + +def printList(head): + while head is not None: + print(head.data, end=' -> ') + head = head.next + print("NULL") + + + +def reverseList(head): + if head is None or head.next is None: + return head + prev_node = None + curr_node = head + next_node = head.next + while curr_node is not None: + next_node = curr_node.next + curr_node.next = prev_node + prev_node = curr_node + curr_node = next_node + return prev_node + + + +def get_sum(list1, list2): + head1 = reverseList(list1) + head2 = reverseList(list2) + + temp1 = head1 + temp2 = head2 + + head = ListNode(0) + prev_node = head + carry = 0 + while head1 is not None or head2 is not None: + val1 = 0 + val2 = 0 + if head1 is not None: + val1 = head1.data + head1 = head1.next + if head2 is not None: + val2 = head2.data + head2 = head2.next + total = val1 + val2 + carry + node = ListNode(total % 10) + carry = int(total / 10) + prev_node.next = node + prev_node = node + if (carry == 1): + prev_node.next = ListNode(1) + + list1 = reverseList(temp1) + list2 = reverseList(temp2) + + return reverseList(head.next) + + + +list1 = createList1() +list2 = createList2() +printList(list1) +printList(list2) +sum_list = get_sum(list1, list2) +printList(sum_list) + +#Given a Linked List which represents a sentence S such that each node represents a letter, the task is to reverse the sentence without reversing individual words. + +class ListNode: + def __init__(self, data): + self.data = data + self.next = None + + + +def createList(): + head = ListNode('I') + head.next = ListNode(' ') + head.next.next = ListNode('l') + head.next.next.next = ListNode('o') + head.next.next.next.next = ListNode('v') + head.next.next.next.next.next = ListNode('e') + head.next.next.next.next.next.next = ListNode(' ') + head.next.next.next.next.next.next.next = ListNode('G') + head.next.next.next.next.next.next.next.next = ListNode('e') + head.next.next.next.next.next.next.next.next.next = ListNode('e') + head.next.next.next.next.next.next.next.next.next.next = ListNode('k') + head.next.next.next.next.next.next.next.next.next.next.next = ListNode('s') + head.next.next.next.next.next.next.next.next.next.next.next.next = ListNode(' ') + head.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('f') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('o') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('r') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode(' ') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('G') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('e') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode('e') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode( + 'k') + head.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next.next = ListNode( + 's') + return head + + + +def printList(head): + while head is not None: + print(head.data, end=' -> ') + head = head.next + print("NULL") + + + +def reverseList(words): + start = 0 + end = len(words) - 1 + while (start < end): + temp = words[start] + words[start] = words[end] + words[end] = temp + start += 1 + end -= 1 + + + +def reverseWords(head): + temp = head + + string = "" + while temp is not None: + string += temp.data + temp = temp.next + + + words = string.split(" ") + + reverseList(words) + + string = " ".join(words) + index = 0 + + while head is not None: + head.data = string[index] + index += 1 + head = head.next + + +head = createList() +printList(head) +reverseWords(head) +printList(head) \ No newline at end of file From 71178f633c7779a658dd1178f4b9ac98bf45e9f8 Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 24 Jun 2020 01:37:35 -0500 Subject: [PATCH 3/7] Erik-Torres --- week-4/Python/Erik-Torres | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-4/Python/Erik-Torres b/week-4/Python/Erik-Torres index 038d6dd..b9ee5ad 100644 --- a/week-4/Python/Erik-Torres +++ b/week-4/Python/Erik-Torres @@ -1,4 +1,4 @@ -# Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. +# Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of adition of two input numbers. class ListNode: def __init__(self, data): From 5388e5bf45979e579d8cb2d5325f34f1dce8397e Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 24 Jun 2020 02:03:37 -0500 Subject: [PATCH 4/7] Week 4 Linked List --- week-4/Python/Erik-Torres | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-4/Python/Erik-Torres b/week-4/Python/Erik-Torres index b9ee5ad..038d6dd 100644 --- a/week-4/Python/Erik-Torres +++ b/week-4/Python/Erik-Torres @@ -1,4 +1,4 @@ -# Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of adition of two input numbers. +# Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. class ListNode: def __init__(self, data): From dd1b36bdac8dae11a7b57c30f555fd8e265a5568 Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 8 Jul 2020 13:27:50 -0500 Subject: [PATCH 5/7] Week 5 Trees --- week-4/Python/Erik-Torres | 28 +++++--- week-6/Python/Erik-Torres.py | 131 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 week-6/Python/Erik-Torres.py diff --git a/week-4/Python/Erik-Torres b/week-4/Python/Erik-Torres index 038d6dd..83d2623 100644 --- a/week-4/Python/Erik-Torres +++ b/week-4/Python/Erik-Torres @@ -1,12 +1,13 @@ # Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. +# class for a node in linked list class ListNode: def __init__(self, data): self.data = data self.next = None - +# method that creates a linked list-1 def createList1(): head = ListNode(5) head.next = ListNode(6) @@ -14,7 +15,7 @@ def createList1(): return head - +# method that creates a linked list-2 def createList2(): head = ListNode(8) head.next = ListNode(4) @@ -22,7 +23,7 @@ def createList2(): return head - +# method that prints the linked list def printList(head): while head is not None: print(head.data, end=' -> ') @@ -30,7 +31,7 @@ def printList(head): print("NULL") - +# method that reverses the linked list def reverseList(head): if head is None or head.next is None: return head @@ -45,7 +46,7 @@ def reverseList(head): return prev_node - +# method that performes the sum of 2 linkedlists def get_sum(list1, list2): head1 = reverseList(list1) head2 = reverseList(list2) @@ -79,7 +80,7 @@ def get_sum(list1, list2): return reverseList(head.next) - +# test case list1 = createList1() list2 = createList2() printList(list1) @@ -89,13 +90,14 @@ printList(sum_list) #Given a Linked List which represents a sentence S such that each node represents a letter, the task is to reverse the sentence without reversing individual words. +# class for a node in linked list class ListNode: def __init__(self, data): self.data = data self.next = None - +# method that creates a linked list def createList(): head = ListNode('I') head.next = ListNode(' ') @@ -124,7 +126,7 @@ def createList(): return head - +# method that prints the linked list def printList(head): while head is not None: print(head.data, end=' -> ') @@ -132,7 +134,7 @@ def printList(head): print("NULL") - +# method that reverses the words in list def reverseList(words): start = 0 end = len(words) - 1 @@ -144,23 +146,27 @@ def reverseList(words): end -= 1 - +# method that reverses the words in linked list def reverseWords(head): temp = head + # store entire linked list as a string string = "" while temp is not None: string += temp.data temp = temp.next - + # split the string into words words = string.split(" ") + # reverse the words in list reverseList(words) + # join words into a string string = " ".join(words) index = 0 + # add string data into linked list while head is not None: head.data = string[index] index += 1 diff --git a/week-6/Python/Erik-Torres.py b/week-6/Python/Erik-Torres.py new file mode 100644 index 0000000..05ac21a --- /dev/null +++ b/week-6/Python/Erik-Torres.py @@ -0,0 +1,131 @@ +# Find Maximum Difference Between a Node and its Descendants in a Binary Tree. + +class tree_node: + def __init__(self, key): + self.key = key + self.left = None + self.right = None + + +def max_difference(root, diff=float('-inf')): + if root is None: + return float('inf'), diff + + leftVal, diff = max_difference(root.left, diff) + rightVal, diff = max_difference(root.right, diff) + currentDiff = root.key - min(leftVal, rightVal) + + diff = max(diff, currentDiff) + + return min(min(leftVal, rightVal), root.key), diff + + +root = tree_node(6) +root.left = tree_node(3) +root.right = tree_node(8) +root.right.left = tree_node(2) +root.right.right = tree_node(4) +root.right.left.left = tree_node(1) +root.right.left.right = tree_node(7) + +print(max_difference(root)[1],'\n') + + +# Write an algorithm to print leaf to root path for every leaf node of the binary tree. + +class node: + + def __init__(self, data): + self.data = data + self.left = None + self.right = None + +def print_paths(root): + # list to store path + path = [] + print_paths_rec(root, path, 0) + + +def print_paths_rec(root, path, pathLen): + + if root is None: + return + + if (len(path) > pathLen): + path[pathLen] = root.data + else: + path.append(root.data) + + pathLen = pathLen + 1 + + if root.left is None and root.right is None: + + print_array(path, pathLen) + else: + + print_paths_rec(root.left, path, pathLen) + print_paths_rec(root.right, path, pathLen) + + +def print_array(ints, len): + for i in range(len-1,0,-1): + print(ints[i], "->", end="") + print(ints[0]) + print() + + +root = node(10) +root.left = node(8) +root.right = node(2) +root.left.left = node(3) +root.left.right = node(5) +root.right.left = node(2) +print_paths(root) + + +# Write an algorithm to convert a tree to its mirror. + +class new_node: + def __init__(self, data): + self.data = data + self.left = self.right = None + + +def mirror(node): + if (node == None): + return + else: + + temp = node + mirror(node.left) + mirror(node.right) + + temp = node.left + node.left = node.right + node.right = temp + + + +def in_order(node): + if (node == None): + return + + in_order(node.left) + print(node.data, end=" ") + in_order(node.right) + + +if __name__ == "__main__": + root = new_node(1) + root.left = new_node(2) + root.right = new_node(3) + root.left.left = new_node(4) + root.left.right = new_node(5) + + print("tree is: ") + in_order(root) + + mirror(root) + + print("\nmirror tree is: ") + in_order(root) From 0ae976455db780dca291d8518bb5f996d79287ed Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 8 Jul 2020 13:34:13 -0500 Subject: [PATCH 6/7] Week 6 Trees --- week-6/Python/Erik-Torres.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-6/Python/Erik-Torres.py b/week-6/Python/Erik-Torres.py index 05ac21a..fad01e1 100644 --- a/week-6/Python/Erik-Torres.py +++ b/week-6/Python/Erik-Torres.py @@ -1,4 +1,4 @@ -# Find Maximum Difference Between a Node and its Descendants in a Binary Tree. +# Find Maximum Difference Between a Node and its Descendants in a Binary Tree.. class tree_node: def __init__(self, key): From 8e6e8de76c550857ab128960ee74badabc122f29 Mon Sep 17 00:00:00 2001 From: Erik Torres Date: Wed, 22 Jul 2020 12:32:56 -0500 Subject: [PATCH 7/7] Erik Torres Week 8 Binary Search --- week-8/Python/Erik-Torres.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 week-8/Python/Erik-Torres.py diff --git a/week-8/Python/Erik-Torres.py b/week-8/Python/Erik-Torres.py new file mode 100644 index 0000000..00384e2 --- /dev/null +++ b/week-8/Python/Erik-Torres.py @@ -0,0 +1,49 @@ +#Stone's Love + +def bin_search_special(arr, ele): + l=0 + h=len(arr)-1 + while l<=h: + m = (l+h)//2 + if ele==arr[m]: + return m + elif arr[m]>ele: + h = m-1 + else: + l = m+1 + return l + +n,q = map(int, input().split()) +a = list(map(int, input().split())) +s=0 +for i in range(len(a)): + s+=a[i] + a[i]=s +m = list(map(int, input().split())) +for i in m: + print(bin_search_special(a, i)+1) + +#Repeated K Times +max_val = 100001 + +def search(ar, N, K): + result = max_val + 1 + for i in range(0, N): + count = 1 + + for j in range(i + 1, N): + if (ar[i] == ar[j]): + + count += 1 + + if (count == K): + result = min(result, ar[i]) + + return result + + +N = int(input()) +ar = list(map(int, input().split())) + +K = int(input()) +print(search(ar, N, K)) \ No newline at end of file