Space Python Loading Python…

Debris grid

Write the codeMedium 2 stages

Before every orbit-raising burn, the radar scans the sector ahead and sends back a small map: each row is a string, # marks debris and . is clear space. The flight dynamics team needs the exact position of every piece.

Scan the grid for debris, then find the columns the ship could fly through.

Find every #, row by row, and print its row and column, both counted from 0, as indexes are. Then print how many there are:

Debris at row 0, column 2
Debris at row 2, column 5
Debris at row 3, column 0
Hits: 3

Loop over the rows with enumerate(grid), and inside that loop, over the characters of the row with enumerate(line).

Find every hit
Works for any scan

Finish stage 1 to unlock this stage.

The ship can only fly through a column with no debris in any row. After the hits, print the clear columns, or none:

Debris at row 0, column 2
Debris at row 2, column 5
Debris at row 3, column 0
Hits: 3
Clear columns: 1, 3, 4, 6, 7

Turn the loops around: the outer loop goes over the column numbers, range(len(grid[0])), and the inner loop over the rows, checking line[column]. A flag says whether the column was blocked.

Find the clear columns
Works for any scan
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

For Loop with enumerate and range

for-loop-enumerate-range.py
# range() produces a run of numbers so you do not have to type them out
print(list(range(5)))

# One argument: starts at zero, stops before the number you gave
for orbit in range(3):
    print("Orbit", orbit, "complete")

# Two arguments: a start and a stop. The stop is never included
for day in range(1, 6):
    print("Flight day", day)

# Three arguments: the last one is the step, and it can go backwards
for altitude in range(100, 0, -25):
    print(altitude, "km above the surface")

# enumerate() hands you the position and the item at the same time
checklist = ["hatch sealed", "fuel loaded", "guidance online", "range clear"]
for position, task in enumerate(checklist):
    print(position, task)

# start=1 numbers things the way a human would
for position, task in enumerate(checklist, start=1):
    print(f"Step {position}: {task}")

# Without enumerate you would be counting by hand
count = 0
for task in checklist:
    count = count + 1
print("Items checked:", count)
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].