Skip to content

Instantly share code, notes, and snippets.

@gameguy43
Created June 16, 2014 01:53
Show Gist options
  • Save gameguy43/19c9b833b96b2ce934f2 to your computer and use it in GitHub Desktop.
Save gameguy43/19c9b833b96b2ce934f2 to your computer and use it in GitHub Desktop.
def largest_product_of_3(array_of_ints):
greatest = array_of_ints[0]
smallest = array_of_ints[0]
greatest_product_of_two = array_of_ints[0] * array_of_ints[1]
smallest_product_of_two = array_of_ints[0] * array_of_ints[1]
greatest_product_of_three = array_of_ints[0] * array_of_ints[1] * array_of_ints[2]
for current_int in array_of_ints:
# do we have a new greatest product of 3?
greatest_product_of_three = max(current_int * greatest_product_of_two, current_int * smallest_product_of_two, greatest_product_of_three)
# do we have a new greatest product of two?
greatest_product_of_two = max(current_int * greatest, current_int * smallest, greatest_product_of_two)
# do we have a new least product of two?
smallest_product_of_two = min(current_int * greatest, current_int * smallest, smallest_product_of_two)
# do we have a new greatest?
greatest = max(greatest, current_int)
# do we have a new smallest?
smallest = min(smallest, current_int)
return greatest_product_of_three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment