DEV Community

Discussion on: Daily Challenge #306 - Largest Possible Product

Collapse
 
agtoever profile image
agtoever • Edited

Python 3 oneliner with testcases and TIO link:

adjacentProduct = lambda arr: max(arr[i] * arr[i+1] for i in range(len(arr) - 1))

cases = [
    ([1, 2, 3], 6),
    ([3, 4, 5], 20),
    ([3, 7, 9], None),
    ([-3, -4, 15], None),
    ([-4, -1, -10], None),
]

for arg, ans in cases:
    print(f"adjacentProduct({arg}) = {adjacentProduct(arg)}{f'; answer should be: {ans}' if ans else ''}.")

if all(adjacentProduct(arg) == ans for arg, ans in cases if ans):
    print('All test cases are correct')
else:
    print('NOT all test cases are correct')
Enter fullscreen mode Exit fullscreen mode

Try it online!