diff --git a/week11/weighted_path.md b/week11/weighted_path.md new file mode 100644 index 0000000..19ab133 --- /dev/null +++ b/week11/weighted_path.md @@ -0,0 +1,13 @@ +--- +Author: Kayode +--- + +Have the function WeightedPath(strArr) take strArr which will be an array of strings which models a non-looping weighted Graph. The structure of the array will be as follows: The first element in the array will be the number of nodes N (points) in the array as a string. The next N elements will be the nodes which can be anything (A, B, C .. Brick Street, Main Street .. etc.). Then after the Nth element, the rest of the elements in the array will be the connections between all of the nodes along with their weights (integers) separated by the pipe symbol (|). They will look like this: (A|B|3, B|C|12 .. Brick Street|Main Street|14 .. etc.). Although, there may exist no connections at all. + +An example of strArr may be: ["4","A","B","C","D","A|B|1","B|D|9","B|C|3","C|D|4"]. Your program should return the shortest path when the weights are added up from node to node from the first Node to the last Node in the array separated by dashes. So in the example above the output should be A-B-C-D. Here is another example with strArr being ["7","A","B","C","D","E","F","G","A|B|1","A|E|9","B|C|2","C|D|1","D|F|2","E|D|6","F|G|2"]. The output for this array should be A-B-C-D-F-G. There will only ever be one shortest path for the array. If no path between the first and last node exists, return -1. The array will at minimum have two nodes. Also, the connection A-B for example, means that A can get to B and B can get to A. A path may not go through any Node more than once. + +Examples +Input: ["4","A","B","C","D", "A|B|2", "C|B|11", "C|D|3", "B|D|2"] +Output: A-B-D +Input: ["4","x","y","z","w","x|y|2","y|z|14", "z|y|31"] +Output: -1 \ No newline at end of file diff --git a/week11/weighted_path.py b/week11/weighted_path.py new file mode 100644 index 0000000..bd99ceb --- /dev/null +++ b/week11/weighted_path.py @@ -0,0 +1,140 @@ +# def WeightedPath(strArr): +# nodeno= int(strArr[0]) +# nodearr= strArr[1:nodeno+1] +# patharr= strArr[nodeno+1:] +# patharr2= [] +# startnode= nodearr[0] +# endnode= nodearr[-1] +# allpos= [] +# temppos= [] +# posweight= [] + +# for i in patharr: +# patharr2.append(i.split('|')) + +# for i in patharr2: +# if nodearr.index(i[0]) > nodearr.index(i[1]): +# i[0], i[1] = i[1], i[0] + +# # for i,j in enumerate(patharr2): +# # for k,l in enumerate(patharr2): +# # if (j[0], j[1]) == (l[0], l[1]) and i != k and int(j[2]) < int(l[2]): +# # patharr2.pop(k) + +# for i in patharr2: +# if i[0] == startnode: +# temppos.append([i]) + +# for i in temppos: +# for j in patharr2: +# if i[-1][1] == j[0]: +# x= i.copy() +# x.append(j) +# temppos.append(x) + +# for i in temppos: +# if i[-1][1] == endnode: +# allpos.append(i) + +# if allpos == []: +# return print(-1) + +# for i in allpos: +# w= 0 +# for j in i: +# w= w + int(j[-1]) +# posweight.append(w) + +# minindex= posweight.index(min(posweight)) +# leastpath= allpos[minindex] + +# path= "" +# for i in leastpath: +# path= path + i[0] + '-' +# else: +# path= path + endnode + + + + +# # print(pospath) +# # print(nodearr) +# # print(patharr2) +# # print(temppos) +# # print(allpos) +# # print(posweight) +# # print(leastpath) +# return print(path) + + +def WeightedPath(strArr): + nodeno= int(strArr[0]) + nodearr= strArr[1:nodeno+1] + patharr= strArr[nodeno+1:] + patharr2= [] + startnode= nodearr[0] + endnode= nodearr[-1] + allpos= [] + temppos= [] + posweight= [] + + for i in patharr: + patharr2.append(i.split('|')) + + for i in patharr2: + if nodearr.index(i[0]) > nodearr.index(i[1]): + i[0], i[1] = i[1], i[0] + + # for i,j in enumerate(patharr2): + # for k,l in enumerate(patharr2): + # if (j[0], j[1]) == (l[0], l[1]) and i != k and int(j[2]) < int(l[2]): + # patharr2.pop(k) + + for i in patharr2: + if i[0] == startnode: + temppos.append([i]) + + for i in temppos: + for j in patharr2: + if i[-1][1] == j[0] or i[-1][1] == j[1]: + if i[-1][1] == j[0]: + y= [j[0], j[1], j[2]] + elif i[-1][1] == j[1]: + y= [j[1], j[0], j[2]] + + if y not in i: + x= i.copy() + x.append(y) + temppos.append(x) + + for i in temppos: + if i[-1][1] == endnode: + allpos.append(i) + + if allpos == []: + return print(-1) + + for i in allpos: + w= 0 + for j in i: + w= w + int(j[-1]) + posweight.append(w) + + minindex= posweight.index(min(posweight)) + leastpath= allpos[minindex] + + path= "" + for i in leastpath: + path= path + i[0] + '-' + else: + path= path + endnode + + return print(path) + + + + +# WeightedPath(["4","A","B","C","D","A|B|1","B|D|9","B|C|3","C|D|4"]) +WeightedPath(["7","A","B","C","D","E","F","G","A|B|1","A|E|9","B|C|2","C|D|1","D|F|2","E|D|6","F|G|2"]) +# WeightedPath(["4","x","y","z","w","x|y|2","y|z|14", "z|y|31"]) +# WeightedPath(["8","C","B","A","D","E","F","G","H","C|D|1","D|F|2","G|F|2","G|E|1","E|B|1","H|B|1","C|A|13","B|A|2","C|E|9"]) \ No newline at end of file diff --git a/week12/intersecting_lines.md b/week12/intersecting_lines.md new file mode 100644 index 0000000..4bc43f8 --- /dev/null +++ b/week12/intersecting_lines.md @@ -0,0 +1,14 @@ +--- +Author: Kayode +--- + + +Intersecting Lines + +Have the function IntersectingLines(strArr) take strArr which will be an array of 4 coordinates in the form: (x,y). Your program should take these points where the first 2 form a line and the last 2 form a line, and determine whether the lines intersect, and if they do, at what point. For example: if strArr is ["(3,0)","(1,4)","(0,-3)","(2,3)"], then the line created by (3,0) and (1,4) and the line created by (0,-3) (2,3) intersect at (9/5,12/5). Your output should therefore be the 2 points in fraction form in the following format: (9/5,12/5). If there is no denominator for the resulting points, then the output should just be the integers like so: (12,3). If any of the resulting points is negative, add the negative sign to the numerator like so: (-491/63,-491/67). If there is no intersection, your output should return the string "no intersection". The input points and the resulting points can be positive or negative integers. + +Examples +Input: ["(9,-2)","(-2,9)","(3,4)","(10,11)"] +Output: (3,4) +Input: ["(1,2)","(3,4)","(-5,-6)","(-7,-8)"] +Output: no intersection \ No newline at end of file diff --git a/week12/intersecting_lines.py b/week12/intersecting_lines.py new file mode 100644 index 0000000..356924a --- /dev/null +++ b/week12/intersecting_lines.py @@ -0,0 +1,171 @@ +import math +from fractions import Fraction + +# def IntersectingLines(arr): +# arr2= [] +# arr3= [] +# plot1= [] +# plot2= [] +# intersect= [] + +# for i in arr: +# x= i.replace("(", "").replace(")", "").split(',') +# arr2.append(x) + +# for i,j in arr2: +# k= int(i) +# l= int(j) +# arr3.append([k,l]) + +# l1x1= arr3[0][0] +# l1y1= arr3[0][1] +# l1x2= arr3[1][0] +# l1y2= arr3[1][1] +# l2x1= arr3[2][0] +# l2y1= arr3[2][1] +# l2x2= arr3[3][0] +# l2y2= arr3[3][1] + +# m1= (l1y2 - l1y1) / (l1x2 - l1x1) +# m2= (l2y2 - l2y1) / (l2x2 - l2x1) +# b1= l1y1 - (m1 * l1x1) +# b2= l2y1 - (m2 * l2x1) + +# if (l1x1 > 0 and l1x2 > 0): +# if l1x1 > l1x2: +# dif1= abs(l1x1) - abs(l1x2) +# elif l1x2 > l1x1: +# dif1= abs(l1x2) - abs(l1x1) +# elif l1x1 < 0 and l1x2 < 0: +# if l1x1 > l1x2: +# dif1= abs(l1x2) - abs(l1x1) +# elif l1x2 > l1x1: +# dif1= abs(l1x1) - abs(l1x2) +# else: +# dif1= abs(l1x1) + abs(l1x2) + +# if (l2x1 > 0 and l2x2 > 0): +# if l2x1 > l2x2: +# dif2= abs(l2x1) - abs(l2x2) +# elif l2x2 > l2x1: +# dif2= abs(l2x2) - abs(l2x1) +# elif l2x1 < 0 and l2x2 < 0: +# if l2x1 > l2x2: +# dif2= abs(l2x2) - abs(l2x1) +# elif l2x2 > l2x1: +# dif2= abs(l2x1) - abs(l2x2) +# else: +# dif2= abs(l2x1) + abs(l2x2) + +# counter1= 0 +# counter2= 0 +# if l1x1 < l1x2: +# s1= l1x1 +# else: +# s1= l1x2 + +# if l2x1 < l2x2: +# s2= l2x1 +# else: +# s2= l2x2 + +# for i in range(int(dif1*10000)): +# d= s1 + counter1 +# e= round(d, 5) +# c= (e * m1) + b1 +# f= round(c, 5) +# plot1.append([e, f]) +# counter1= counter1 + 0.00010000000000000000000000000 + +# for i in range(int(dif2*10000)): +# d= s2 + counter2 +# e= round(d, 5) +# c= (e * m2) + b2 +# f= round(c, 5) +# plot2.append([e, f]) +# counter2= counter2 + 0.0001000000000000000000000000 + +# for i in plot1: +# if i in plot2: +# intersect.append(i) + +# print(plot1) +# print(plot2) +# if intersect == []: +# return print("no intersection") +# else: +# ans= "(" + str(Fraction(intersect[0][0]).limit_denominator(10000)) + "," + str(Fraction(intersect[0][1]).limit_denominator(10000)) + ")" +# print(ans) + +# # print(arr2) +# # print(arr3) +# # print(m1, m2) +# # print(dif1, dif2) +# # print(plot1) +# # print(plot2) + + +def IntersectingLines(arr): + arr2= [] + arr3= [] + plot1= [] + plot2= [] + intersect= [] + + for i in arr: + x= i.replace("(", "").replace(")", "").split(',') + arr2.append(x) + + for i,j in arr2: + k= int(i) + l= int(j) + arr3.append([k,l]) + + l1x1= arr3[0][0] + l1y1= arr3[0][1] + l1x2= arr3[1][0] + l1y2= arr3[1][1] + l2x1= arr3[2][0] + l2y1= arr3[2][1] + l2x2= arr3[3][0] + l2y2= arr3[3][1] + + try: + m1= (l1y2 - l1y1) / (l1x2 - l1x1) + except ZeroDivisionError: + ix= l1x2 + m1= "undefined" + + try: + m2= (l2y2 - l2y1) / (l2x2 - l2x1) + except ZeroDivisionError: + ix= l2x2 + m2= "undefined" + + if m1 != "undefined": + b1= l1y1 - (m1 * l1x1) + if m2 != "undefinfed": + b2= l2y1 - (m2 * l2x1) + + try: + if m1 != "undefined" and m2 != "undefined": + ix= (b2 - b1) / (m1 - m2) + except ZeroDivisionError: + return print("no intersection") + + if m1 != "undefined": + iy= (ix * m1) + b1 + else: + iy= (ix * m2) + b2 + + ans= "(" + str(Fraction(ix).limit_denominator(1000)) + "," + str(Fraction(iy).limit_denominator(1000)) + ")" + return print(ans) + + + +# IntersectingLines(["(9,-2)","(-2,9)","(3,4)","(10,11)"]) +# IntersectingLines( ["(3,0)","(1,4)","(0,-3)","(2,3)"]) +# IntersectingLines(["(0,15)","(3,-12)","(2,1)","(13,7)"]) +# IntersectingLines(["(1,2)","(3,4)","(-5,-6)","(-7,-8)"]) +# IntersectingLines(["(0,0)","(0,-1)","(1,1)","(0,1)"]) +IntersectingLines(["(1,-1)","(1,1)","(-5,-5)","(-7,-7)"]) \ No newline at end of file