Python Forum
Need help getting the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help getting the output
#10
In order to enhance learning process I provide following explainer:

In expected output row number and position index for '\' are matching -> row[0]position[0], row[1]position[1]....row[4]position[4]. So the task is to have two iterators (one for row index and another for position index on row) and if they match put '\' into that position.

for i in range(start): for loop to get row indices / number of rows to output -> 0, 1, 2, 3, 4

for j, x in enumerate(range(1, start +1)) for loop to get position indices j (-> 0, 1, 2 , 3,4) and values x (-> 1, 2, 3, 4, 5) to output by using enumerate()

'\\' if i == j else x conditional expression to select what to yield - if row and position indices matches then '\' otherwise value

*(...) generator expression with unpacking generated values for rows -> row[0] \ 2 3 4 5, row[1] 1 \ 3 4 5..... row[4] 1 2 3 4 \

print(..., sep='') printing generated and unpacked row values without separation.

For better readability more descriptive names should have been used:

>>> start = 5
>>> for row in range(start):
...     print(*('\\' if row == position else num for position, num in enumerate(range(1, start + 1))), sep='')
... 
\2345
1\345
12\45
123\5
1234\
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Need help getting the output - by aankrose - Apr-10-2019, 04:41 PM
RE: Need help getting the output - by ichabod801 - Apr-10-2019, 05:51 PM
RE: Need help getting the output - by perfringo - Apr-10-2019, 07:12 PM
RE: Need help getting the output - by aankrose - Apr-10-2019, 09:02 PM
RE: Need help getting the output - by perfringo - Apr-11-2019, 04:12 AM
RE: Need help getting the output - by Yoriz - Apr-10-2019, 10:12 PM
RE: Need help getting the output - by ichabod801 - Apr-11-2019, 02:09 AM
RE: Need help getting the output - by aankrose - Apr-11-2019, 05:10 PM
RE: Need help getting the output - by loomski - Apr-11-2019, 07:52 PM
RE: Need help getting the output - by aankrose - Apr-12-2019, 02:35 AM
RE: Need help getting the output - by perfringo - Apr-11-2019, 09:01 PM

Forum Jump:

User Panel Messages

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