Skip to content

S3Q2 · File Content Zigzag Shift

⚡ Quick Reference

Type: File-in, stdout-out

Core idea: spaces follow a zigzag cycle of length 2*(z-1). For line index i, compute pos = i % cycle, then spaces = pos if pos < z else cycle - pos.

import tempfile, sys

_, filename = tempfile.mkstemp(prefix="zigzag_")
with open(filename, "w") as f:
    f.write(sys.stdin.read())

with open(filename) as f:
    z     = int(f.readline().strip())
    cycle = 2 * (z - 1) if z > 1 else 1

    for i, line in enumerate(f):
        ch = line.rstrip("\n")
        if not ch:
            continue
        if z == 1:
            spaces = 0
        else:
            pos    = i % cycle
            spaces = pos if pos < z else cycle - pos
        print(" " * spaces + ch)

Key rules: - Zigzag cycle length = 2*(z-1) for z > 1; always 0 spaces for z == 1 - For position pos in cycle: ascending if pos < z, descending otherwise - No trailing spaces - print the character immediately after the spaces


Problem Statement

Problem (File I/O → stdout)

Read z from the first line, then one character per subsequent line. Print each character preceded by the correct number of spaces following the zigzag pattern.

Example (z=3, "appleoraNge"):

Input file
3
a
p
p
l
e
o
r
a
n
g
e
Output
a
 p
  p
 l
e
 o
  r
 a
n
 g
  e

Understanding the zigzag pattern

For z=3, the cycle length = 2*(3-1) = 4:

Line index:  0  1  2  3  4  5  6  7  8  9  10
pos (% 4):   0  1  2  3  0  1  2  3  0  1   2
spaces:      0  1  2  1  0  1  2  1  0  1   2

The rule: spaces = pos when pos < z (ascending), spaces = cycle - pos when pos >= z (descending).

At pos=3 for z=3: cycle - pos = 4 - 3 = 1 → descending back to 1. ✓


Tracing z=2 (cycle length 2)

i:      0  1  2  3  4  5  6  7  8  9  10
pos%2:  0  1  0  1  0  1  0  1  0  1   0
spaces: 0  1  0  1  0  1  0  1  0  1   0
chars:  a  p  p  l  e  o  r  a  n  g   e

Pattern: a, p, p, l, e, o, r, a, n, g, e


Solution approaches

import tempfile, sys

_, filename = tempfile.mkstemp(prefix="zigzag_")
with open(filename, "w") as f:
    f.write(sys.stdin.read())

with open(filename) as f:
    z     = int(f.readline().strip())
    cycle = 2 * (z - 1) if z > 1 else 1

    for i, line in enumerate(f):
        ch = line.rstrip("\n")
        if not ch:
            continue
        if z == 1:
            spaces = 0
        else:
            pos    = i % cycle
            spaces = pos if pos < z else cycle - pos
        print(" " * spaces + ch)
import tempfile, sys

_, filename = tempfile.mkstemp(prefix="zigzag_")
with open(filename, "w") as f:
    f.write(sys.stdin.read())

with open(filename) as f:
    z = int(f.readline().strip())

    if z == 1:
        cycle = 1
    else:
        cycle = 2 * (z - 1)

    i = 0
    for line in f:
        ch = line.rstrip("\n")
        if not ch:
            continue
        # Determine number of spaces
        if z == 1:
            spaces = 0
        else:
            pos = i % cycle
            if pos < z:
                spaces = pos          # ascending half
            else:
                spaces = cycle - pos  # descending half
        print(" " * spaces + ch)
        i += 1
import tempfile, sys
from itertools import cycle

_, filename = tempfile.mkstemp(prefix="zigzag_")
with open(filename, "w") as f:
    f.write(sys.stdin.read())

with open(filename) as f:
    z = int(f.readline().strip())

    if z == 1:
        pattern = cycle([0])
    else:
        zigzag = list(range(z)) + list(range(z-2, 0, -1))
        pattern = cycle(zigzag)

    for line in f:
        ch = line.rstrip("\n")
        if not ch:
            continue
        spaces = next(pattern)
        print(" " * spaces + ch)

Pre-builds the zigzag sequence as a list and cycles through it with itertools.cycle. For z=3: [0, 1, 2, 1] repeating.


Key takeaways

01

Cycle length = 2*(z-1)

The zigzag goes up to z-1 spaces then back down to 0 - one full cycle spans 2*(z-1) lines. Using i % cycle maps any line index into the current position within the cycle.

02

pos < z → ascending, pos ≥ z → descending

In the ascending half, spaces = pos. In the descending half, spaces = cycle - pos. At the peak (pos = z-1): spaces = z-1. At the bottom (pos = 0 or cycle): spaces = 0.

03

itertools.cycle - cleaner for pre-built patterns

Building [0, 1, ..., z-1, z-2, ..., 1] as a list and wrapping with cycle() avoids the index arithmetic entirely. Call next(pattern) for each character - reads like the problem description.