### [2016-01-25] Challenge #251 [Easy] Create Nonogram description

from Tkinter import * import math

def roundup(x): return int(math.ceil(x / 5.0)) * 5

def file_2_mat(filename):

``````nano_map = []
temp_line = []

nanogram_map = open(filename)
for line in nanogram_map.readlines():
for char in line:
if char is ' ':
temp_line.append(0)
if char is '*':
temp_line.append(1)
nano_map.append(temp_line)
temp_line = []
return nano_map
``````

def print_mat(number,mat, (x_border, y_border)):

``````root = Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
print screen_height,screen_width

mat_weidth_size = len(max(mat,key=len))
mat_height_size = len(mat)
max_x_border = len(max(x_border,key=len))
max_y_border = len(max(y_border,key=len))

max_size = mat_height_size if mat_height_size > mat_weidth_size else mat_weidth_size

if max_size % 5:
max_size = roundup(max_size)

square_size = 50
size_matrix = max_size * square_size
size_border_x = square_size*(max_x_border)
size_border_y = square_size*(max_y_border)

print size_matrix

while size_matrix + size_border_x >= screen_height or size_matrix + size_border_y >= screen_height:
square_size //= 2
size_matrix = max_size * square_size
size_border_x = square_size*(max_x_border)
size_border_y = square_size*(max_y_border)

text_font = "Times " + str(square_size-4) + " bold"

canvas = Canvas(
width=size_matrix+size_border_y, height=size_matrix+size_border_x, bg='white', highlightthickness=0)
canvas.pack(padx=20, pady=20)
canvas.create_text(size_border_y//2, size_border_x//2, fill="black", font="Times " + str(square_size*2) + " bold", text=number)

# matrix

for i in range(max_size+1):
actual_x = i * square_size + size_border_y
actual_y = i * square_size + size_border_x
if i is max_size or i is 0:
canvas.create_line(
0, actual_y, size_matrix+size_border_y, actual_y, width=5.0)
canvas.create_line(
actual_x, 0, actual_x, size_matrix+size_border_x , width=5.0)
else:
canvas.create_line(
0, actual_y, size_matrix+size_border_y, actual_y, width=2.0)
canvas.create_line(
actual_x, 0, actual_x, size_matrix+size_border_x , width=2.0)

# borders

for i in range(max_x_border):
if i is 0:
pass
canvas.create_line(size_border_y,i*square_size,size_matrix+size_border_y,i*square_size, width=5.0)
else:
canvas.create_line(size_border_y,i*square_size,size_matrix+size_border_y,i*square_size, width=2.0)

for i in range(max_y_border):
if i is 0:
pass
canvas.create_line(i*square_size,size_border_x,i*square_size,size_matrix+size_border_x, width=5.0)
else:
canvas.create_line(i*square_size,size_border_x,i*square_size,size_matrix+size_border_x, width=2.0)

# print
for line in range(mat_height_size):
for char in range(mat_weidth_size):
actual_x = char * square_size + size_border_y
actual_y = line * square_size + size_border_x
try:
if mat[line][char] is 1:
canvas.create_rectangle(
actual_x, actual_y, actual_x + square_size, actual_y + square_size, fill="black")
except:
pass

font_base_x = size_border_y - square_size // 2
font_base_y = square_size // 2 + size_border_x

for x in range(len(y_border)):
for num in range(len(y_border[x])):
canvas.create_text(font_base_x-square_size*num, font_base_y+square_size*x, fill="black", font=text_font, text=y_border[x][num])

font_base_x = size_border_y + square_size // 2
font_base_y = size_border_x - square_size // 2

for y in range(len(x_border)):
for num in range(len(x_border[y])):
canvas.create_text(font_base_x+square_size*y,font_base_y-square_size*num, fill="black", font= text_font, text=x_border[y][num])

root.mainloop()
``````

def mat_2_puzzle_x_y(mat):

``````line_puzzle_x = []
line_puzzle_y = []
count_x = 0
last_x = 0
count_y = 0
last_y = 0
max_y = len(mat)
max_x = len(max(mat,key=len))
for x in range(max_x):
temp_x = []
for vert_line in range(max_y):
try:
if mat[vert_line][x] == 1:
if vert_line == max_y - 1:
temp_x.append(count_x + 1)
count_x = 0
last_x = 0
else:
count_x += 1
last_x = 1
else:
if last_x == 1:
temp_x.append(count_x)
count_x = 0
last_x = 0
except:
pass
line_puzzle_x.append(tuple(temp_x))

temp_y = []
for vert_line in range(len(mat)):
try:
if mat[x][vert_line] == 1:
if vert_line == max_y - 1:
temp_y.append(count_y + 1)
count_y = 0
last_y = 0
else:
count_y += 1
last_y = 1
else:
if last_y == 1:
temp_y.append(count_y)
count_y = 0
last_y = 0
except:
pass

line_puzzle_y.append(tuple(temp_y))
return line_puzzle_x, line_puzzle_y
``````

def main():

``````nanogram_map = file_2_mat("nanogram.txt")

print_mat(1,nanogram_map,mat_2_puzzle_x_y(nanogram_map))
``````

main()