Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
creating a rectangle
#1
Hello
Yesterday I downloaded python and I need to create this program.

input:
2 3
8 2 5
1 7 9

output:
8 1
2 7
5 9

2 3 means that the rectangle in input will be 2x3
My mission is to create the rectangle in output. Can someone tell me which tutorials should I watch to have an idea how to create something like this? Im total beginner.

Thank You for your time
Reply
#2
You are going to need to read the input into a list of lists, and then transpose and flip the list. So you will definitely need to know how lists work in Python. Reading them into the list is going to depend on how the input is provided to the program, which is not clear. However, you will probably need to understand how strings in Python work for at least part of that. That will also be useful for the output, which will use the print function.

So at least tutorials on lists, strings, and the print function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-10-2019, 02:12 PM)ichabod801 Wrote: You are going to need to read the input into a list of lists, and then transpose and flip the list. So you will definitely need to know how lists work in Python. Reading them into the list is going to depend on how the input is provided to the program, which is not clear. However, you will probably need to understand how strings in Python work for at least part of that. That will also be useful for the output, which will use the print function.

So at least tutorials on lists, strings, and the print function.

p = input()
M = str(p).split(" ")
A = int(M[0])
R = int(M[1])

X=[]

for i in range(A):
    l=list(map(int,input().split(" ")))
    X.append(l)

for i in range(R):
    for j in range(A):

        print(X[j][i], end = " ")
    print ()


I got this but have one problem. I need to remove the space after last numbers in rows. How can I do it? How can I replace the end= " " ?
Reply
#4
I would use join. Also, it is better to loop directly over the list rather than over the indexes of the list:

for row in R:
    print(' '.join(row))
If you haven't covered the join string method and aren't allowed to use it, you could build the string up piece by piece, rather than printing it piece by piece. Then at the end of the inner loop, print the stripped version of it to get rid of the trailing space.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rotated Rectangle overlap using Shapely pyNew 0 1,713 Feb-25-2021, 04:54 AM
Last Post: pyNew
  Return draw.rectangle pixel coordinate data to .csv file CephloRhod 0 2,392 May-20-2020, 10:37 AM
Last Post: CephloRhod

Forum Jump:

User Panel Messages

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