Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array and rectangles
#1
Hello guys,

I need your help to solve this problem. I need to print a rectangle inside an another rectangle.

Example :

Input :
9 19 (number of lignes of ".", number of column of ".")
1 (number of rectangle inside)
1 3 7 5 o (lig1, col1, lig2, col2, caracter)

Output :
...................
...ooo.............
...ooo.............
...ooo.............
...ooo.............
...ooo.............
...ooo.............
...ooo.............
...................


1) Method 1 that I tried :

nbLignes, nbColonnes = map(int, input().split(" "))
nbRectangles = int(input())
tab = [ ["."] * nbColonnes] * nbLignes
for i in range(len(tab)):
    for j in range(len(tab[i])):
        print(tab[i][j], end="")
    print()
input :
9 19
1

output :
...................
...................
...................
...................
...................
...................
...................
...................
...................

I don't know how to add the second rectangles inside the first one according to the parameter we specify in the input (1 3 7 5 o). Does anyone have an idea how to do it please ?


2) Method 2 that I tried :
I tried an another version without multidimensional array but I don't know if it's the right method.

nbLignes, nbColonnes = map(int, input().split(" "))
nbRectangles = int(input())

for loop in range(nbRectangles):
    lig1, col1, lig2, col2, couleur = input().split(" ")
    
    for i in range(nbLignes):
        for j in range(nbColonnes):
            print(".", end ="")
        print()
    
    for i in range(int(lig1), int(lig2)):
        for j in range(int(col1), int(col2)):
            print(couleur, end = "")
        print()
Input :
9 19
1
1 3 7 5 o

Output :
...................
...................
...................
...................
...................
...................
...................
...................
...................
oo
oo
oo
oo
oo
oo


Thank you for your help
Reply
#2
Because you cannot back up and type over the first rectangle, you need to type both rectangles at the same time. For each pixel you print you need to determine if you type a "." or an "o".

Printing more than 1 internal rectangle works the same way except you have to test each pixel against each rectangle. What do you print if a pixel is part of multiple rectangles, the first rectangle or the last?
Reply
#3
if a pixel is part of multiple rectangles, we print the last one.

Do you have an idea how to do it with mulidimension array ?
Reply
#4
You do not need a multi-dimensional array. You need to write a function that will tell you if x, y is part of a rectangle.

To do this you need a data structure that can store your rectangle. The code below saves rectangle information in a dictionary. You could also use a list or a tuple or a class:
line = input('Input rectangle coordinates x1 y1 x2 y2 pixel:')
v = line.split()
rect = {'x1':v[0], 'y1':v[1], 'x2':v[2], 'x3':v[3], 'pixel':v[4]}
You need to support having multiple rectangles, so you will have to store all the rectangles in a list.

A good next step is modifying your program so you can enter multiple rectangles and save them all in a list. Print out the list to verify the information was saved correctly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to generate multiple rectangles with randrange without overlapping? rafaberec 1 1,871 Dec-10-2018, 10:12 AM
Last Post: buran
  printing list of random generated rectangles Zatoichi 8 7,348 Feb-18-2018, 06:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020