0

I am trying to create an image that needs to be printed vertically:

enter image description here

From the for loop, I can print a image fine by indenting to a new line; however, I want the image to rotate counter clockwise 90 degrees (Is this transpose?).

I tried to use from itertools import zip_longest but it gives:

TypeError: zip_longest argument #1 must support iteration

class Reservoir:
    def __init__(self,landscape):
        self.landscape = landscape
        self.image = ''
        for dam in landscape:
            self.image += '#'*dam + '\n'

        print(self.image)

landscape = [4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0,
             1, 1, 2, 5, 6, 5, 2, 2, 2, 3, 3, 3, 4, 5, 3, 2, 2]  
lake = Reservoir(landscape)
print(lake)          
sobolevn
  • 16,714
  • 6
  • 62
  • 60

1 Answers1

2

I don't know if you will find a function or a lib that do that for you. But you can code this rotation by hand.

You don't want to display a real image here, but to print chars that represents a landscape. You have to print the "image" line by line, but since your landscape array represents the number of '#' you want in each column, you have to loop over the total number of lines you want, and for each char in that line, print a ' ' or a '#' depending on the corresponding landscape column value

With

h = max(landscape)

you calculate the total number of lines you want to print by finding the max of the landscape values.

Then, you loop over theses lines

for line in reversed(range(h)):

in that loop, line takes values 6, 5, 4, etc.

For each line, you have to loop over the whole landscape array to determine, for each column if you want to print a space or a '#', depending on the value of the landscape column (v) and the current line

for v in self.landscape:
    self.image += ' ' if line >= v else '#'

The full program:

class Reservoir:
    def __init__(self, landscape):
        self.landscape = landscape

        h = max(landscape)

        self.image = ''
        for line in reversed(range(h)):
            for v in self.landscape:
                self.image += ' ' if line >= v else '#'
            self.image += '\n'

landscape = [4, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 5, 6, 5, 2, 2, 2, 3, 3, 3, 4, 5, 3, 2, 2]
lake = Reservoir(landscape)
print(lake.image)

The result:

                 #            
                ###       #   
#               ###      ##   
##              ###   ######  
####           ###############
######       #################
Stefan
  • 5,203
  • 8
  • 27
  • 51
Antwane
  • 20,760
  • 7
  • 51
  • 84
  • This is a fantastic information Antwane! Thank you! Regarding how stackoverflow works, How can I format the result like the way you did?? I can't seem to figure out how you did that. Thank you in advance :) – Blake Johnosn Nov 18 '15 at 14:35
  • Another question, how does the reversed function works here? Does it work like counting backward such as list[:-1]? – Blake Johnosn Nov 18 '15 at 14:38
  • To format the result, you can use a [code or preformatted text block](http://stackoverflow.com/editing-help#code), similar to what you used to post the python sample code in your question – Antwane Nov 19 '15 at 08:02
  • [reversed()](https://docs.python.org/2/library/functions.html#reversed) is a python built-in. It reverses the sequence given as argument and returns an iterator. reversed() return a lazy-evaluated iterator, so it is a better option here, because we only need the inverted list in the for loop. If you had to re-use the resulting list in the rest of the code, you should use `the_list[::-1]` (http://stackoverflow.com/q/3705670/1887976) – Antwane Nov 19 '15 at 08:11