Skip to content
Merged
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
34 changes: 34 additions & 0 deletions adjacentElementProduct.py
Original file line number Diff line number Diff line change
@@ -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))])








50 changes: 50 additions & 0 deletions bresenham_line_algorithm.py
Original file line number Diff line number Diff line change
@@ -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)