Mittwoch, 9. Oktober 2013

 

Musterlösung zu Aufgabe 2 "Denksport mit Python, erste Anwendung von Mercurial" (POS1: 2BHIF)

Mögliche Lösungen zur Aufgabe 2 "Denksport mit Python, erste Anwendung von Mercurial sind (nur die Python-Scrips):
#!/usr/bin/env python3
"""
 File:    pythagorean_triple.py
 Desc:    Ein Programm zur Bestimmung eines pythagoräischen Tripel (a2+b2=c2
          für a < b < c), wobei a+b+c == 1000.
 Created: 2013-09-23, Harald R. Haberstroh
"""
from sys import argv
from math import sqrt


if __name__ == '__main__':
    summe = 1000
    if len(argv) == 2:
        summe = int(argv[1])
    for a in range(1, summe // 3):
        for b in range(a + 1, summe // 2):
            a2 = a * a
            b2 = b * b
            c2 = a2 + b2
            c = sqrt(c2)
            if a < b < c and a + b + c == summe:
                print(a, b, int(c))
                print(int(a * b * c))
#!/usr/bin/env python3
"""
 File:    sumdigits2pow1000.py
 Desc:    calculate the sum of the digits of 2**1000 (exponent can be changed
          with commandline argument)
 Created: 2013-09-23, Harald R. Haberstroh
"""

from sys import argv

if __name__ == '__main__':
    if len(argv) == 2:
        exponent = int(argv[1])
    else:
        exponent = 1000
    print(sum(list(map(int, list(str(2 ** exponent))))))
#!/usr/bin/env python3
"""
 File:    sumdigitsfact.py
 Desc:    calculate the sum of the digits of 100! (100 can be changed
          with commandline argument)
 Created: 2013-09-23, Harald R. Haberstroh
"""

from sys import argv

def fact(n):
    """n!"""
    f = 1
    for i in range(1, n + 1):
        f *= i
    return f

if __name__ == '__main__':
    if len(argv) == 2:
        n = int(argv[1])
    else:
        n = 100
    print(sum(list(map(int, list(str(fact(n)))))))

Die Palindrom-Beispiele sind ganz ähnlich und wurden daher in ein Programm gepackt, bei dem man als Parameter auf der Kommandozeile die Anzahl der Stellen angeben kann (4, 6, 8). Zusätzlich kann noch '-l' angegeben werden, um eine Liste der Produkte zu erhalten. Die Ergebnisliste ist nicht ganz vollständig, denn es wird das Ergebnis des Produkts als Schlüssel verwendet. Damit gehen Lösungen wie zum Beispiel 111111 = 143 * 777 = 231 * 481 = 259 * 429 verloren.
#!/usr/bin/env python3
"""
 File:    palindrome.py
 Desc:    largest palindrome of product of two n/2-digit numbers with n digits
 Created: 2013-09-23, Harald R. Haberstroh
"""

from sys import argv

def checkPalindrom(s):
    """
    checks whether string is a palindrome
    """
    l = len(s)
    for i in range(l // 2):
        if s[i] != s[l - i - 1]:
            return False
    return True

def genPalindrom(n):
    """
    build palindrome out of products
    """
    # Dictionary only for list of palindromes not for maximum
    palindromes = {}
    products = []
    maxPalindrome = 0
    maxPalindromeFactors = ""
    for i in range(10**((n - 1) // 2), 10 ** (n // 2)):
        for j in range(10**((n - 1) // 2), 10 ** (n // 2)):
            product = str(i * j)
            if checkPalindrom(product):
                if not product in palindromes:
                    palindromes[product] = str(i) + " * " + str(j) + " = " + product
                    products.append(product)
                    if int(product) > maxPalindrome:
                        maxPalindrome = int(product)
                        maxPalindromeFactors = str(i) + " * " + str(j) + " = " + product
    return maxPalindromeFactors, palindromes

if __name__ == '__main__':
    n = 2
    if len(argv) > 1:
        n = int(argv[1])
    maxPalindrome, palindromes = genPalindrom(n)
    if "-l" in argv:
        lstPalindromes = list(palindromes.values())
        lstPalindromes.sort()
        for palindrome in lstPalindromes:
            print(palindrome)
    print("largest palindrome: %s" % maxPalindrome)

Labels: , ,


Kommentare:

Kommentar veröffentlichen

Abonnieren Kommentare zum Post [Atom]





<< Startseite

This page is powered by Blogger. Isn't yours?

Abonnieren Posts [Atom]