Created
June 24, 2016 20:05
-
-
Save alothings/95dfe2b490f272f8956969e95ed7a4b4 to your computer and use it in GitHub Desktop.
Given a large number and a small integer n, convert it to string, and find the maximum product among the nearest n numbers
This file contains hidden or 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
''' | |
Write a function that wil receive a big number as a string | |
and a number of digits to multiply and will return the biggest | |
product contained in the number. | |
IO: NUMB (big number), int (number of digits to multiply | |
''' | |
def adjacent_digits_product(NUMB, n): | |
my_str = str(NUMB) | |
my_list = list(my_str) | |
# print type(my_list) | |
max_prod = 0 | |
for i in range(0, len(my_list)-n + 1): | |
# print my_list[0:] | |
prod = 1 | |
# print 'item', my_list[i] | |
for j in range(i, i+n): | |
# print my_list[j], prod | |
prod = prod*int(my_list[j]) | |
if(prod >= max_prod): | |
# print 'max prod changing', max_prod | |
max_prod = prod | |
return max_prod | |
NUMB = 12345678910111213 | |
print adjacent_digits_product(NUMB, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment