mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Added source files (Programmieren Tutorium)
This commit is contained in:
parent
1be8f17cc0
commit
ad81fa802b
244 changed files with 8252 additions and 0 deletions
56
presentations/Programmieren-Tutorium/Tutorium-04/euler28.py
Normal file
56
presentations/Programmieren-Tutorium/Tutorium-04/euler28.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
def printArr(a):
|
||||
for line in a:
|
||||
print(line)
|
||||
|
||||
def initialise(n):
|
||||
array = [[0 for j in xrange(0,n)] for i in xrange(0,n)]
|
||||
return array
|
||||
|
||||
def spiralFill(a):
|
||||
n = len(a)
|
||||
x = y = n/2
|
||||
number = 1
|
||||
# r u l o
|
||||
order = [(1,0), (0,1), (-1,0), (0,-1)]
|
||||
iOrder = 0
|
||||
length = 1
|
||||
a[y][x] = number
|
||||
while not (x == (n-1) and y == 0):
|
||||
for j in xrange(0, length):
|
||||
xAdd, yAdd = order[iOrder]
|
||||
x += xAdd
|
||||
y += yAdd
|
||||
number += 1
|
||||
a[y][x] = number
|
||||
if x == (n-1) and y==0:
|
||||
break
|
||||
if iOrder == 1 or iOrder == 3:
|
||||
length += 1
|
||||
iOrder = (iOrder+1) % 4
|
||||
return a
|
||||
|
||||
def diagonalSum(a):
|
||||
n = len(a)
|
||||
sum = -1 # you will have the element in the middle (1) twice
|
||||
for i in xrange(0, n):
|
||||
sum += a[i][i]
|
||||
sum += a[n-i-1][i]
|
||||
return sum
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="ProjectEuler: 28")
|
||||
parser.add_argument("-n", metavar='N', type=int,
|
||||
help="length of the spiral", required=True)
|
||||
parser.add_argument("-d", action="store_true",default=False,
|
||||
help="display the spiral")
|
||||
args = parser.parse_args()
|
||||
array = initialise(args.n)
|
||||
array = spiralFill(array)
|
||||
if args.d:
|
||||
printArr(array)
|
||||
print diagonalSum(array)
|
Loading…
Add table
Add a link
Reference in a new issue