mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
easy as a piece of cake
This commit is contained in:
parent
85ecba5ace
commit
d99d1d27dc
54
interview_cake/math/highest_product_3_int.py
Normal file
54
interview_cake/math/highest_product_3_int.py
Normal file
@ -0,0 +1,54 @@
|
||||
#!/bin/python
|
||||
|
||||
"""
|
||||
Given a list of integers, find the highest product you can get from three of the integers.
|
||||
|
||||
The input list_of_ints will always have at least three integers.
|
||||
"""
|
||||
|
||||
def highest_num(list_of_ints):
|
||||
|
||||
if len(list_of_ints) == 3:
|
||||
return list_of_ints[0]*list_of_ints[1]*list_of_ints[2]
|
||||
|
||||
sorted_list = sorted(list_of_ints)
|
||||
|
||||
return sorted_list[-3]*sorted_list[-2]*sorted_list[-1]
|
||||
|
||||
|
||||
def highest_product_of_3_On(list_of_ints):
|
||||
|
||||
highest = max(list_of_ints[0], list_of_ints[1])
|
||||
lowest = min(list_of_ints[0], list_of_ints[1])
|
||||
highest_product_of_2 = list_of_ints[0] * list_of_ints[1]
|
||||
lowest_product_of_2 = list_of_ints[0] * list_of_ints[1]
|
||||
|
||||
highest_product_of_3 = list_of_ints[0] * list_of_ints[1] * list_of_ints[2]
|
||||
|
||||
for i in range(2, len(list_of_ints)):
|
||||
current = list_of_ints[i]
|
||||
|
||||
highest_product_of_3 = max(highest_product_of_3,
|
||||
current * highest_product_of_2,
|
||||
current * lowest_product_of_2)
|
||||
|
||||
highest_product_of_2 = max(highest_product_of_2,
|
||||
current * highest,
|
||||
current * lowest)
|
||||
|
||||
lowest_product_of_2 = min(lowest_product_of_2,
|
||||
current * highest,
|
||||
current * lowest)
|
||||
|
||||
highest = max(highest, current)
|
||||
|
||||
lowest = min(lowest, current)
|
||||
|
||||
return highest_product_of_3
|
||||
|
||||
list_of_ints = [4, 2, 5, 6]
|
||||
print highest_num(list_of_ints)
|
||||
print "Should be 120"
|
||||
|
||||
print highest_product_of_3_On(list_of_ints)
|
||||
print "Should be 120"
|
38
interview_cake/math/product_every_int.py
Normal file
38
interview_cake/math/product_every_int.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/bin/python
|
||||
|
||||
"""
|
||||
You have a list of integers, and for each index you want to find the product of every integer except the integer at that index.
|
||||
|
||||
Write a function get_products_of_all_ints_except_at_index() that takes a list of integers and returns a list of the products.
|
||||
|
||||
For example, given:
|
||||
|
||||
[1, 7, 3, 4]
|
||||
|
||||
your function would return:
|
||||
|
||||
[84, 12, 28, 21]
|
||||
|
||||
by calculating:
|
||||
|
||||
[7 * 3 * 4, 1 * 3 * 4, 1 * 7 * 4, 1 * 7 * 3]
|
||||
|
||||
Here's the catch: You can't use division in your solution!
|
||||
"""
|
||||
|
||||
def get_products_of_all_ints_except_at_index(array):
|
||||
prod_array = []
|
||||
|
||||
for i, num in enumerate(array):
|
||||
prod = 1
|
||||
for other_num in array[:i] + array[i+1:]:
|
||||
prod *= other_num
|
||||
|
||||
prod_array.append(prod)
|
||||
|
||||
return prod_array
|
||||
|
||||
|
||||
array = [1, 7, 3, 4]
|
||||
print get_products_of_all_ints_except_at_index(array)
|
||||
print "Should be [84, 12, 28, 21]"
|
Loading…
x
Reference in New Issue
Block a user