Skip to content
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
31 changes: 31 additions & 0 deletions week-2/Python/Erik-Torres.py
Original file line number Diff line number Diff line change
@@ -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=' ')
179 changes: 179 additions & 0 deletions week-4/Python/Erik-Torres
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# 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)
head.next.next = ListNode(3)
return head


# method that creates a linked list-2
def createList2():
head = ListNode(8)
head.next = ListNode(4)
head.next.next = ListNode(2)
return head


# method that prints the linked list
def printList(head):
while head is not None:
print(head.data, end=' -> ')
head = head.next
print("NULL")


# method that reverses the linked list
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


# method that performes the sum of 2 linkedlists
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)


# test case
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 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(' ')
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


# method that prints the linked list
def printList(head):
while head is not None:
print(head.data, end=' -> ')
head = head.next
print("NULL")


# method that reverses the words in list
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


# 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
head = head.next


head = createList()
printList(head)
reverseWords(head)
printList(head)
131 changes: 131 additions & 0 deletions week-6/Python/Erik-Torres.py
Original file line number Diff line number Diff line change
@@ -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)
49 changes: 49 additions & 0 deletions week-8/Python/Erik-Torres.py
Original file line number Diff line number Diff line change
@@ -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))