4
\$\begingroup\$

enter image description hereSorry, I'm new and not confident. This is right, yes? It seems to work but, again, other people's answers seem more complicated:

grid = [
    ['.', '.', '.', '.', '.', '.'],
    ['.', 'O', 'O', '.', '.', '.'],
    ['O', 'O', 'O', 'O', '.', '.'],
    ['O', 'O', 'O', 'O', 'O', '.'],
    ['.', 'O', 'O', 'O', 'O', 'O'],
    ['O', 'O', 'O', 'O', 'O', '.'],
    ['O', 'O', 'O', 'O', '.', '.'],
    ['.', 'O', 'O', '.', '.', '.'],
    ['.', '.', '.', '.', '.', '.']
]

row = 0
for entry in grid:
    for subentry in grid[row]:
        print(subentry, end = "")
    print("\n")
    row = row + 1
\$\endgroup\$
3
  • \$\begingroup\$ Without knowing the purpose of this code, it's hard to review it. \$\endgroup\$
    – l0b0
    Jan 17, 2021 at 7:45
  • \$\begingroup\$ Sorry, I've edited with the original task. You're supposed to print out the picture without the commas and brackets \$\endgroup\$
    – user236240
    Jan 17, 2021 at 8:07
  • 3
    \$\begingroup\$ Thank you. It's also best to transcribe text instead of pasting images of text - images can't be read by screen readers, don't have the same controls as text for adapting to vision problems, and can't be indexed by search engines. \$\endgroup\$
    – l0b0
    Jan 17, 2021 at 8:21

2 Answers 2

1
\$\begingroup\$

The question itself

It's not great. The textbook makes some decisions that haven't done the programmer any favours. First, you will notice that the input and output are mirrored about the line \$x=y\$. This is not a coincidence: it's the result of the textbook using column-major order when row-major order makes much more sense in this context.

Also, the grid presented is not only a sequence of sequences, it's a sequence of sequences of sequences since there are inner strings.

To solve both problems, ignore what the textbook tells you and instead attempt to code for this grid:

grid = (
    '..OO.OO..',
    # ...
)

and use [y][x] indexing instead of [x][y] indexing.

Such changes will reduce this to a much more trivial problem that does not require any explicit loops, and can be done via

print('\n'.join(grid))
\$\endgroup\$
4
  • \$\begingroup\$ huh? So why is mine simpler? \$\endgroup\$
    – user236240
    Jan 19, 2021 at 4:35
  • \$\begingroup\$ I don't understand the question. Yours isn't simpler - it has explicit loops and an additional level of sequence nesting in the input data. \$\endgroup\$
    – Reinderien
    Jan 19, 2021 at 5:26
  • \$\begingroup\$ That's like saying a 10 page book on transcendental logic is easier than a 100 page book on the Teletubbies because it's shorter \$\endgroup\$
    – user236240
    Jan 20, 2021 at 6:28
  • \$\begingroup\$ Very funny. By "simpler" here I mean shorter code, with less complexity presented to the interpreter, and likely to execute more quickly. If you have specific questions about how this works feel free to ask them. \$\endgroup\$
    – Reinderien
    Jan 20, 2021 at 14:53
0
\$\begingroup\$

for subentry in grid[row]: can be simplified as for subentry in entry:, meaning that you no longer need the row variable. As for

That doesn't work

:

$ diff <(python original.py) <(python my.py) && echo "No difference"
No difference
$ diff --unified original.py my.py 
--- original.py 2021-01-18 19:34:02.177350487 +1300
+++ my.py   2021-01-18 19:34:43.855590559 +1300
@@ -12,7 +12,7 @@
 
 row = 0
 for entry in grid:
-    for subentry in grid[row]:
+    for subentry in entry:
         print(subentry, end = "")
     print("\n")
     row = row + 1
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.