Last active
March 22, 2018 11:27
-
-
Save stepheweffie/e24672452cdae82bb07b58a4b2735588 to your computer and use it in GitHub Desktop.
returns the max product of two items for all adjacent items in the list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the long way | |
def main(a, l): | |
for i in a: | |
if a.index(i) < a.index(a[-1]): | |
n = i * a[a.index(i) + 1] | |
l.append(n) | |
print(max(l)) | |
return max(l) | |
if __name__ == '__main__': | |
main([2,3,4,-5,6,-4], list()) | |
# pythonic list comprehension | |
def inputArray(a): | |
return max([i * a[a.index(i) + 1] for i in a if a.index(i) < a.index(a[-1])]) | |
inputArray([2,3,4,-5,6,-4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment