rica324342
rica324342

Reputation: 33

Draw letters python

I have a homework and I have to do this:

The purpose of this program is to draw a set of rectangles (with sides parallel to the axes) using characters.

You will receive a sequence of rectangles, one per line, defined by the coordinates (X and Y) of your upper left and lower right corners.

You should "paint" all the rectangles using the # character. The X and Y coordinates grow right and down, respectively.

Input example:

0 0 2 8

0 7 8 8

10 0 12 8

10 0 18 1

10 7 18 8

20 0 22 8

20 0 28 1

20 7 28 8

Output example:

    ###       ######### #########
    ###       ######### #########
    ###       ###       ###      
    ###       ###       ###      
    ###       ###       ###      
    ###       ###       ###      
    ###       ###       ###      
    ######### ######### #########
    ######### ######### #########

I already made this:

import sys

def make_rectangules(first_coordenates, second_coordenates):

    for y in range(second_coordenates[1]-first_coordenates[1]+1):
        for x in range(second_coordenates[0]-first_coordenates[0]+1):    
            print('#')


def main():
    aux_list = []
    for line in sys.stdin:
        line = line.strip('\n').split()
        line = list(map(int,line))
        first_coordenates = (line[0],line[1])
        second_coordenates = (line[2],line[3])
        make_rectangules(first_coordenates, second_coordenates)

main()

But I don't get the same result.

Upvotes: 0

Views: 1535

Answers (2)

Olvin Roght
Olvin Roght

Reputation: 7812

Code:

input_data = '''0 0 2 8
0 7 8 8
10 0 12 8
10 0 18 1
10 7 18 8
20 0 22 8
20 0 28 1
20 7 28 8'''

rectangles = [line.split(' ') for line in input_data.splitlines()]
output_list = []

for rect in rectangles:
    x1, y1, x2, y2 = [int(coord) for coord in rect]
    for i in range(y1, y2 + 1):
        prev = output_list[i] if i < len(output_list) else ''
        if len(prev) < x2:
            prev += ' ' * (x2 - len(prev) + 1)
        prev = prev[:x1] + '#' * (x2 - x1 + 1) + prev[x2 + 1:]
        if i < len(output_list):
            output_list[i] = prev
        else:
            output_list.append(prev)

output = '\n'.join(output_list)
print(output)

Output:

###       ######### #########
###       ######### #########
###       ###       ###
###       ###       ###
###       ###       ###
###       ###       ###
###       ###       ###
######### ######### #########
######### ######### #########

Feel free to ask anything regarding code.

Upvotes: 0

Kremas
Kremas

Reputation: 71

I did it:

import sys

def main():
    aux_list = [[0, 0, 2, 8],
                [0, 7, 8, 8], 
                [10, 0, 12, 8],
                [10, 0, 18, 1],
                [10, 7, 18, 8],
                [20, 0, 22, 8],
                [20, 0, 28, 1],
                [20, 7, 28, 8]
               ]

    max_x = 0
    max_y = 0
    for elem in aux_list:
        if elem[2] > max_x:
            max_x = elem[2]
        if elem[3] > max_y:
            max_y = elem[3]

    array = [[" " for x in range(max_y+1)] for y in range(max_x+1)]
    for elem in aux_list:
        for i in range(elem[0], elem[2]+1):
            for j in range(elem[1], elem[3]+1):
                array[i][j] = "#"

    for x in range(max_y+1):
        for y in range(max_x+1):
            print(array[y][x], end='')
        print("")
main()

Result :

    >>python draw.py
###       ######### #########
###       ######### #########
###       ###       ###
###       ###       ###
###       ###       ###
###       ###       ###
###       ###       ###
######### ######### #########
######### ######### #########

This isn't the cleanest way to do it, but it works and this code sample can help you. Please take your time to understand this code, and ask me questions if you need some clarifications.

Upvotes: 1

Related Questions