Skip to content

Instantly share code, notes, and snippets.

@eduardoklosowski
Created August 9, 2017 03:29
Show Gist options
  • Save eduardoklosowski/6e654cfe34646ae632338c8380b1ebc4 to your computer and use it in GitHub Desktop.
Save eduardoklosowski/6e654cfe34646ae632338c8380b1ebc4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
# Entrada
lado = int(raw_input('Lado: '))
if lado % 2 == 0 or lado <= 0:
print('Valor do lado inválido')
sys.exit(1)
inicial = int(raw_input('Inicial: '))
passo = int(raw_input('Passo: '))
# Fórmulas
casas = lado ** 2
final = inicial + passo * (casas - 1)
centro = (inicial + final) / 2
soma = centro * lado
print 'Casas:', casas
print 'Final:', final
print 'Centro:', centro
print 'Soma:', soma
# Quadrado
quadrado = [[0 for j in range(lado)] for i in range(lado)]
x0 = -((lado - 1) / 2)
y0 = ((lado + 1) / 2 - 1)
for casa in range(casas):
l = casa / lado
c = casa % lado
x = x0 + c + l
y = y0 - c + l
if y < 0:
y += lado
elif y >= lado:
y %= lado
if x < 0:
x += lado
elif x >= lado:
x %= lado
quadrado[y][x] = inicial + passo * casa
mask = '%%%dd ' % len(str(final))
for linha in quadrado:
for casa in linha:
print mask % casa,
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment