Skip to content

Instantly share code, notes, and snippets.

@stepheweffie
Last active March 22, 2018 11:27
Show Gist options
  • Save stepheweffie/e24672452cdae82bb07b58a4b2735588 to your computer and use it in GitHub Desktop.
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
# 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