0

Hi,
How can I print this figure if I have an array like this?
It should be a for-loop but don´t know how.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

figure:
############. ##
## ##. . . . ##
##. . . ## ####
##. ############
##. . ## ##
####. ######
##. . ## ##
##. ############

4
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by pyTony
0
from __future__ import print_function # 

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

[[print("##" if char==0 else ".",end="" if i<len(line)-1 else "\n") for i,char in enumerate(line)]  for line in labyrinth]

Edited by slate: n/a

0

Wooo Slate and tonyjv are really complex

Infact the pseudo of slate is flawless but will a newbie understand the algos.?

I just wrote this as a step by step to the solution. I real life, i advice you to use Slate's example.

labyrinth = \
[[0,0,0,0,0,0,1,0],
[0,1,0,1,1,1,1,0],
[0,1,1,1,0,1,0,0],
[0,1,0,0,0,0,0,0],
[0,1,1,0,1,1,1,0],
[0,0,1,1,1,0,0,0],
[0,1,1,0,1,1,1,0],
[0,1,0,0,0,0,0,0]]

de=0
for x in labyrinth:
	for y in x:
		de=de+1
		if y == 0:
			print '##',
		else:
			print ".",
		if de == 8:
			print '\n'
			de=0

for fun ;)

Edited by richieking: n/a

0

We are not supposed to give ready answers. After this your try I must write cleaner version, though. Use 4 space tabs next time.
I prefer not to use line continuation character, but utilize automatic continuation inside parenthesis and brackets:

from __future__ import print_function

labyrinth = [
             [0,0,0,0,0,0,1,0],
             [0,1,0,1,1,1,1,0],
             [0,1,1,1,0,1,0,0],
             [0,1,0,0,0,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,0,1,1,1,0,0,0],
             [0,1,1,0,1,1,1,0],
             [0,1,0,0,0,0,0,0]]

for line in labyrinth:
    for isopen in line:
        print('  ' if isopen else "##", end='')
    print()
print('-'*40)

# in real life
print('\n'.join(''.join('  ' if isopen else "##" for isopen in line)
                        for line in labyrinth))

Edited by pyTony: n/a

0

but tonyjv there is a problem now, if you look at the figure there are some dots, how can I print them just like the figure?

0

The codes are not there, you maybe must apply one labyrinth solver algorith to find the path, like mine, which solved the labyrinth and when I set the marking of path properly, and gave open places as S and E gave results like this:

The solved labyrinth
############ .##
##  ## .......##
## .....##  ####
## .############
## .. ##      ##
####.     ######
## .. ##      ##
## .############


7.820 ms
Enter to finish

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.