Нахождение индекса N самых больших элементов в Python Array / List Эффективно

На самом деле вы можете сделать это с Python, просто взгляните:

actualProduct = 0
highestPalindrome = 0

# Setting the numbers. In case it's two digit 10 and 99, in case is three digit 100 and 999, etc.
num1 = 100
num2 = 999

def isPalindrome(number):
        number = str(number)
        reversed = number[::-1]
        if number==reversed:
                return True
        else:
                return False

a = 0
b = 0

for i in range(num1,num2+1):
        for j in range(num1,num2+1):
                actualProduct = i * j
                if (isPalindrome(actualProduct) and (highestPalindrome < actualProduct)):
                        highestPalindrome = actualProduct
                        a = i
                        b = j


print "Largest palindrome made from the product of two %d-digit numbers is [ %d ] made of %d * %d" % (len(str(num1)), highestPalindrome, a, b)
19
задан inspectorG4dget 24 August 2013 в 22:14
поделиться