From 010f349215b7f37e288f44d60463aded55d75617 Mon Sep 17 00:00:00 2001 From: Hasan Date: Sun, 13 Oct 2019 10:54:05 +0500 Subject: [PATCH 1/2] Adjacent Element Product --- adjacentElementProduct.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 adjacentElementProduct.py diff --git a/adjacentElementProduct.py b/adjacentElementProduct.py new file mode 100644 index 0000000..b98810d --- /dev/null +++ b/adjacentElementProduct.py @@ -0,0 +1,34 @@ +#Given an array of integers, +#find the pair of adjacent elements +#that has the largest product and +#return that product. + +def adjacentElementsProduct(inputArray): + + length = int(len(inputArray)) + + maxm = inputArray[0]*inputArray[1] + product = 1 + for i in range(1, length-1): + product = inputArray[i]*inputArray[i+1] + + if product>maxm: + maxm = product + + return maxm + + +# print(adjacentElementsProduct([3,6,7,5])) + +print(adjacentElementsProduct([3, 6, -2, -5, 7, 3])) + +#Alternate solution +#return max([inputArray[i]*inputArray[i+1] for i in range(0, int(len(inputArray)-1))]) + + + + + + + + From 144ab33c6ade54d355baa8b472d9d74ee5fbe211 Mon Sep 17 00:00:00 2001 From: Hasan Date: Sun, 13 Oct 2019 10:56:10 +0500 Subject: [PATCH 2/2] Bresenham Algorithm --- bresenham_line_algorithm.py | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bresenham_line_algorithm.py diff --git a/bresenham_line_algorithm.py b/bresenham_line_algorithm.py new file mode 100644 index 0000000..8a59d8d --- /dev/null +++ b/bresenham_line_algorithm.py @@ -0,0 +1,50 @@ +def lineGenerator(x1, y1, x2, y2): + dx = x2 - x1 + dy = y2 - y1 + + slope = 2*dy - dx + + x = x1 + y = y1 + while x < x2: + + #Print current coordinates + print(x, y) + + #X increases any ways + x+= 1 + + # 2dy is always added in the slope. Do it. + slope += 2*dy + #Check for the current slope + if slope >= 0: + y += 1 + slope -= 2 * (x2-x1) + + elif slope <=0: + #No changes are made. + slope = slope + + +lineGenerator(3, 2, 15, 5) + + # if P1[0]==P2[0] and P1[1]==P2[1]: + # return 0 + # #P1 is the point given. Initial point + # #P2 is the point to reach. Final point + # print(P1) + # #Check if the point is above or below the line. + # dx = P2[0]-P1[0] + # dy = P2[1]-P1[0] + + # di = 2*dy - dx + + # currX = P1[0] + # currY = P1[1] + + # if di > 0: + # P1 = (currX+1, currY+1) + # else: + # P1 = (currX+1, currY) + + # return lineGenerator(P1, P2) \ No newline at end of file