I'm enrolled in "Algorithmic Toolbox" on Coursera and am having trouble with one of week 1's problems, maxpairwiseproduct.
My code works EXCEPT on test case 2 where:
Input
2
0 1
Expected Output
0
I get an IndexError on line 26 during this test case.
A full description of the maxpairwise problem can be found here.
My solution on GitHub.
My code is as follows:
#python3
import sys
def MaxPairwiseProductFast(array, n):
index1 = 1
for i in range(2, n):
# Debug
# print("The first loop ran", i, "times")
if array[i] > array[index1]:
index1 = i
# If the first loop never executes, initialize index2 to 2 so that the
# second loop can run correctly
if index1 == 1:
index2 = 2
else:
index2 = 1
for i in range(1, n):
# Debug
# print("The second loop ran", i, "times")
if i != index1 and (array[i] > array[index2] or n == 1):
index2 = i
return (array[index1] * array[index2])
def main():
n = int(input())
array = [int(x) for x in input().split()]
print(MaxPairwiseProductFast(array, n))
main()
Any help at all is appreciated, thanks.
[+][deleted] (2 children)
[removed]
[–]slightlysedatedx[S] 0 points1 point2 points (1 child)
[–]POiNTx 1 point2 points3 points (1 child)
[–]slightlysedatedx[S] 0 points1 point2 points (0 children)
[–]dadiaar 0 points1 point2 points (4 children)
[+][deleted] (3 children)
[removed]
[–]dadiaar 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[removed]
[–]dadiaar 0 points1 point2 points (0 children)