From 4ed1198f418c2580acea0d22c72a59b4bf9eec12 Mon Sep 17 00:00:00 2001 From: 7daylenrobertson <9daylenrobertson@gmail.com> Date: Sat, 6 Jun 2020 00:31:50 -0400 Subject: [PATCH 1/6] Both python questions are in this file. Remove line 29 doc strings to use the first one --- .vscode/settings.json | 3 + week-2/Python/daylen-robertson.py | 112 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 week-2/Python/daylen-robertson.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ceea2a2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\9dayl\\AppData\\Local\\Programs\\Python\\Python37\\python.exe" +} \ No newline at end of file diff --git a/week-2/Python/daylen-robertson.py b/week-2/Python/daylen-robertson.py new file mode 100644 index 0000000..30d63bf --- /dev/null +++ b/week-2/Python/daylen-robertson.py @@ -0,0 +1,112 @@ +""" +1. Given a string, find the longest substring +which is palindrome. For example, if the given +string is "ababad", the output should be "ababa". +""" +"""First thing is to anaylze the problem. +Since it's a palindrome, we know if its backwards +it will be the same forward. So we reverse the string +with the [start:stop:step] index slice parameters +and then compare it to itself and see if it's the same. + +In this case I used the [::-1] parameters +so I can start at the VERY beginning and the VERY end +when reversing. If you dont do this you wont be slicing +the whole list since the STOP parameter is where you will +stop and it wont be counted.""" + +"""string is what we will be taking in so we can have +any test case. palinSave will be the biggest palindrome""" + + +"""The double for loop allows us to start at the beginning, +check the whole length, and go to the next index and check +the whole length again. My 2nd loop range has a +(len(string),0,-1) in order to reverse it using -1 +as a reverse step. Going reverse made more sense to me conceptually""" + + +"""Remove this line and doc strings on line 41 for solution + +string=input() +palinSave="" + +for letter in range(0,len(string)): + for endletter in range(len(string),0,-1): + palinCheck=string[letter:endletter] + palinCompare=palinCheck[::-1] + if palinCheck==palinCompare: + #Now that we found one, check if its the longest + if len(palinCheck)>len(palinSave): + palinSave=palinCheck +print(palinSave) + +""" + +""" +2. 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 +allperms= list(permutations("abb")) + +#Creates a structure with no duplicates +set_of_all_perms=set(allperms) + +for i in allperms: + i="".join(i) + print(i) + +"""I just used python's permutations module from the +itertools library because I felt like there wasnt a point +in reinventing the wheel. But, here's what I'd do if I +didnt use the module: +Basically, I'd use shuffle to create the perms but I +wont add them unless they arent in the list. + +I'd use the factorial length of the string as my length. +If there are duplicates, Ill cut the size down in half +This runtime is terrible and I prefer my above solution: + +import random +string="addd" + +total=1 +for num in range(len(string)+1): + if num==0: + continue + else: + total*=num + +dupliCheck=dict.fromkeys(string) +if len(string)!=len(dupliCheck): + total/=2 + +string= string.replace(""," ")[1:-1] +string= string.split(" ") + + +perms=[] +random.shuffle(string) +grabber="".join(string) +print(grabber) +perms.append(grabber) + +while len(perms) Date: Sun, 7 Jun 2020 00:51:43 -0400 Subject: [PATCH 2/6] Applied feedback and changed answers to two functions --- week-2/Python/daylen-robertson.py | 101 +++++++++++++++--------------- 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/week-2/Python/daylen-robertson.py b/week-2/Python/daylen-robertson.py index 30d63bf..a855059 100644 --- a/week-2/Python/daylen-robertson.py +++ b/week-2/Python/daylen-robertson.py @@ -26,22 +26,19 @@ as a reverse step. Going reverse made more sense to me conceptually""" -"""Remove this line and doc strings on line 41 for solution - -string=input() -palinSave="" - -for letter in range(0,len(string)): - for endletter in range(len(string),0,-1): - palinCheck=string[letter:endletter] - palinCompare=palinCheck[::-1] - if palinCheck==palinCompare: - #Now that we found one, check if its the longest - if len(palinCheck)>len(palinSave): - palinSave=palinCheck -print(palinSave) - -""" +def palin(): + string=input() + palinSave="" + + for letter in range(0,len(string)): + for endletter in range(len(string),0,-1): + palinCheck=string[letter:endletter] + palinCompare=palinCheck[::-1] + if palinCheck==palinCompare: + #Now that we found one, check if its the longest + if len(palinCheck)>len(palinSave): + palinSave=palinCheck + print(palinSave) """ 2. Given a string str, the task is to print all the @@ -51,15 +48,17 @@ For example, if given "abb", the output should be "abb abb bab bba bab bba" """ -from itertools import permutations -allperms= list(permutations("abb")) +def perms(): + from itertools import permutations + string=input() + allperms= list(permutations(string)) -#Creates a structure with no duplicates -set_of_all_perms=set(allperms) + #Creates a structure with no duplicates + set_of_all_perms=set(allperms) -for i in allperms: - i="".join(i) - print(i) + for i in allperms: + i="".join(i) + print(i) """I just used python's permutations module from the itertools library because I felt like there wasnt a point @@ -71,42 +70,40 @@ I'd use the factorial length of the string as my length. If there are duplicates, Ill cut the size down in half This runtime is terrible and I prefer my above solution: +""" +def secondSolution (string): + import random -import random -string="addd" - -total=1 -for num in range(len(string)+1): - if num==0: - continue - else: - total*=num - -dupliCheck=dict.fromkeys(string) -if len(string)!=len(dupliCheck): - total/=2 + total=1 + for num in range(len(string)+1): + if num==0: + continue + else: + total*=num -string= string.replace(""," ")[1:-1] -string= string.split(" ") + dupliCheck=dict.fromkeys(string) + if len(string)!=len(dupliCheck) : + total/=2 + string= string.replace(""," ")[1:-1] + string= string.split(" ") -perms=[] -random.shuffle(string) -grabber="".join(string) -print(grabber) -perms.append(grabber) -while len(perms) Date: Wed, 10 Jun 2020 22:03:33 -0400 Subject: [PATCH 3/6] Update daylen-robertson.py --- week-2/Python/daylen-robertson.py | 34 ++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/week-2/Python/daylen-robertson.py b/week-2/Python/daylen-robertson.py index a855059..fe337df 100644 --- a/week-2/Python/daylen-robertson.py +++ b/week-2/Python/daylen-robertson.py @@ -27,19 +27,34 @@ def palin(): - string=input() - palinSave="" - - for letter in range(0,len(string)): - for endletter in range(len(string),0,-1): - palinCheck=string[letter:endletter] + string=input("Add a string: ") + if len(string)==1 or string=="": + return string + #saves first index so if no palindrome this would pass + palinSave=string[0] + + #init + start=0 + counter=len(string) + + while start< len(string): + #for letter in range(0,len(string)) + #for endletter in range(len(string),0,-1) + palinCheck=string[start:counter] palinCompare=palinCheck[::-1] if palinCheck==palinCompare: #Now that we found one, check if its the longest if len(palinCheck)>len(palinSave): palinSave=palinCheck - print(palinSave) - + if counter!=start: + counter-=1 + else: + counter=len(string) + start+=1 + + return palinSave +result= palin() +print(result) """ 2. Given a string str, the task is to print all the permutations of str. A permutation is an @@ -85,8 +100,7 @@ def secondSolution (string): if len(string)!=len(dupliCheck) : total/=2 - string= string.replace(""," ")[1:-1] - string= string.split(" ") + string= list(string) perms=[] From 1284b08fcc743176842323860219c0bc3168e7f2 Mon Sep 17 00:00:00 2001 From: 7daylenrobertson <9daylenrobertson@gmail.com> Date: Sun, 5 Jul 2020 21:52:57 -0400 Subject: [PATCH 4/6] Sorry this took so long, we've been moving this week so it was hard to find time to code and teach myself linked list and deque --- week-4/Python/main.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/week-4/Python/main.py b/week-4/Python/main.py index e69de29..2bb850a 100644 --- a/week-4/Python/main.py +++ b/week-4/Python/main.py @@ -0,0 +1,45 @@ +from collections import deque + + +def sum_lists(): + #I dont know how we were supposed to input the #'s but + # I'd just use a standard iterative input loop until None + ll1=deque([5,6,3]) + ll2=deque([8,4,2]) + + #used to represent the tens place + multi=1 + total=0 + #this makes multiplying easy since I can start at the + #ones place + ll1.reverse() + for num in ll1: + total+=num*multi + multi*=10 + + #Repeats the same process + multi=1 + ll2.reverse() + + for num in ll2: + total+=num*multi + multi*=10 + print(total) + +def words_reverse(): + s=deque(["I"," ","l","o","v","e"," ","g","e","e","k","s"," ","f","o","r"," ","g","e","e","k","s"]) + templist="" + #check to see if it's one word + if " " not in s: + print(s) + else: + #copy deque using a string, then split by spaces + for i in s: + templist+=i + templist=templist.split(" ") + + #return split version to s, then reverse it + s=deque(templist) + s.reverse() + print(s) + From dbdf2f44bde99cb2ac1f8c1be0e8b39c9afff787 Mon Sep 17 00:00:00 2001 From: 7daylenrobertson <9daylenrobertson@gmail.com> Date: Fri, 17 Jul 2020 00:03:19 -0400 Subject: [PATCH 5/6] week 6 tree turn in --- .vscode/settings.json | 1 - week-6/Python/daylen-robertson.py | 91 +++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 week-6/Python/daylen-robertson.py diff --git a/.vscode/settings.json b/.vscode/settings.json index ceea2a2..7a73a41 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,2 @@ { - "python.pythonPath": "C:\\Users\\9dayl\\AppData\\Local\\Programs\\Python\\Python37\\python.exe" } \ No newline at end of file diff --git a/week-6/Python/daylen-robertson.py b/week-6/Python/daylen-robertson.py new file mode 100644 index 0000000..2849a37 --- /dev/null +++ b/week-6/Python/daylen-robertson.py @@ -0,0 +1,91 @@ +import binarytree +from binarytree import Node, build + + +#Creates a mirror of the tree +def mirror(): + #Pass in any values to create a tree + values = [7, 3, 2, 6, 9, 1, 5, 8] + if len(values)==1: + print(values) + elif len(values)==0: + print("None") + else: + tree1=build(values) + + for i in range(len(tree1)): + try: + temp=tree1[i].left + tree1[i].left=tree1[i].right + tree1[i].right=temp + except: + print("no index") + + print(tree1) + + + + + +#By alternating, we're able to go down the path where the leaf is +def make_branches(leaf,branches,branchess): + + for num in range(len(tree1)): + if leaves[leaf] in tree1[num].left: + #adds to branch + branches.append(tree1[num].left) + #checks if we're done with this branch + if tree1[num].left==leaves[leaf]: + branchess.append(branches) + branches=[] + break + elif leaves[leaf] in tree1[num].right: + branches.append(tree1[num].right) + if tree1[num].right==leaves[leaf]: + branchess.append(branches) + branches=[] + break + +#This runs the code that recursively creates root to leaf branches +#Pass in any values you'd like +values = [7, 3, 2, 6, 9, 1, 5, 8] +if len(values)==1: + print(values) +elif len(values)==0: + print("None") +else: + tree1=build(values) + branchess=[] + branch=list(tree1) + leaves=tree1.leaves + for leaf in range(len(leaves)): + branches=[tree1[0]] + make_branches(leaf,branches,branchess) + print(branchess) + + +#MUST RUN BRANCH CREATION FUNCTION FIRST +#finds max difference between node and differences +def max_diff(branchess): + branchess.extend((list(tree1),list(tree1.left),list(tree1.right))) + + newBranch=[] + newBranches=[] + #This is to get rid of the new lines and help us retrieve the numeric representation of the nodes + for branch in branchess: + branch=str(branch).strip('\n') + for i in list(branch): + if i.isnumeric(): + newBranch.append(int(i)) + newBranches.append(newBranch) + newBranch=[] + + print(newBranches) + maxDiff=0 + + #this is to compare the differences between each node and it's decendants + for branchDiff in newBranches: + + if maxDiff<(max(branchDiff)-min(branchDiff)): + maxDiff=(max(branchDiff)-min(branchDiff)) + print(maxDiff) \ No newline at end of file From a0aaa872e85111364ba62821100a361b25d32c1a Mon Sep 17 00:00:00 2001 From: 7daylenrobertson <9daylenrobertson@gmail.com> Date: Mon, 20 Jul 2020 04:42:10 -0400 Subject: [PATCH 6/6] week 8 search turn ins --- week-8/Python/daylen-robertson.py | 46 +++++++++++++++++++++++++++++++ week-8/Python/main.py | 0 2 files changed, 46 insertions(+) create mode 100644 week-8/Python/daylen-robertson.py delete mode 100644 week-8/Python/main.py diff --git a/week-8/Python/daylen-robertson.py b/week-8/Python/daylen-robertson.py new file mode 100644 index 0000000..c7b8184 --- /dev/null +++ b/week-8/Python/daylen-robertson.py @@ -0,0 +1,46 @@ + +def stone_love(): + from bisect import bisect_left + #On hacker earth the first test case is incorrect bc their output doesn't make sense with the input. + #I could be wrong though, if so, I have code that will run for that too, just a simple nested for loop + #The loop uses a dictionary with # stone keys and day values and checks if the value is >= Q + #That was time inefficient though + num=input().split() + n=int(num[0]) + qq=int(num[1]) + numStone=input().split() + qStones=input().split() + + + + total=0 + array=[] + for j in numStone: + total+=int(j) + array.append(total) + + for q in qStones: + print(bisect_left(array,int(q))+1) + + +def repeated_k_times(): + n=int(input()) + array=input().split() + array.sort() + arrayset=set(array) + arrayset=sorted(arrayset) + dic=dict.fromkeys(arrayset) + + + k=int(input()) + + #sets values to 0 + for i in dic: + dic[i]=0 + + #When it equals k, print # + for i in array: + dic[i]+=1 + if int(dic[i])==k: + print (i) + break \ No newline at end of file diff --git a/week-8/Python/main.py b/week-8/Python/main.py deleted file mode 100644 index e69de29..0000000