What would you like to learn?

Try PowerShell, networks, XSS, or Step 138

Browse the full curriculum →

Python

Step 54. Algorithm Training 1 — The Muscle That Translates Problems into Code

Step 54Estimated practice · 4 hours (split across several days)

Level 1 — Programming and the Computer’s Insides | Difficulty ★★☆☆☆ | Estimated time: 4 hours (split across several days)

Prerequisites: Steps 41–53 complete. You know conditionals, loops, and string handling. Today you need to sign up for Baekjoon (acmicpc.net).

  • What you need: an internet connection, a Baekjoon account (you’ll make it today), one folder to store your solutions.
  • Caution: today’s practice is 100% safe. The code you submit to the judging site is pure practice code that computes only on your own computer. Don’t rush — it’s normal to split this chapter across several days.

Until now we’ve learned tools. Variables, loops, functions, files. But knowing tools and solving problems are different things. Just as knowing every kitchen tool doesn’t make you a cook, knowing syntax doesn’t produce a program. In between you need "the muscle that translates problems into code," and that muscle grows only through repeated training.

Today that training begins. Why is it needed for security study? CTF problems are, in the end, a contest of "analyzing a problem and automating it with code within a time limit." Calculating the attempt counts of a brute-force attack that tries every password candidate, finding patterns in massive data — all of it stands on algorithmic sense. Today is the first day of training that will continue for a lifetime.


1. Learning Objectives

By the end of this chapter, you will be able to:

  • Run the whole cycle of signing up for Baekjoon, reading a problem, submitting code, and getting judged
  • Use the procedure of breaking a problem into small pieces and moving it into code (plain-language solution first)
  • Verify by experiment how much slower code gets as input grows (time complexity)
  • Read the six kinds of judging verdicts and decide your next action
  • Receive "Accepted" verdicts on 10 problems and store your solution code in proper form

2. Background Knowledge — Today’s Tools and Concepts

Today’s Tools at a Glance

Category Details
Language/environment Python 3 + a browser (Baekjoon judging). Local experiments use the time module
Today’s syntax a, b = map(int, input().split()), input = sys.stdin.readline (fast input), time.time() (measuring time)
Concepts needed Automated judging systems, standard input/output formats, time complexity (O(n), O(n²)), boundary values

2-1. Baekjoon and the Judging System

Baekjoon (BOJ, acmicpc.net) is the most widely used algorithm problem-solving site in Korea. Pick a problem and submit code, and the server feeds it countless prepared inputs; if it passes them all, you get the "맞았습니다!!" (Accepted) verdict. It’s a weighty teacher that teaches you "works on my computer" and "works on any input" are different things.

2-2. The Rules of Input and Output

The judging program hands the problem’s data to our code through standard input (the one you receive with input()) and takes the answer through standard output (print). So following the input format and output format written in each problem exactly is half the battle. Most cases of "the examples work but it’s wrong" are format problems.

2-3. Time Complexity — A Feel for the Law of Slowing Down

When the input doubles, how many times slower does my code get? With one loop, roughly two times; with a double loop, four times. This relationship is called time complexity, written like O(n) and O(n²). For now the feel matters more than the notation — "if the input is 100,000, a double loop (100,000 × 100,000 = 10 billion rounds) takes minutes." This feel later becomes the ruler that measures "how many days would it take to brute-force this password blindly?"

2-4. The Problem-Solving Procedure

The difference between beginners and experts is not talent but procedure. Experts do this: (a) read the problem three times. (b) Trace the example input by hand. (c) Write the solution in plain language. (d) Move that plain language into code. (e) Check with the examples and submit. The "write in plain language" of (c) is the key — jumping straight into code is the beginner’s trap.


3. Follow Along

3-1. Signing Up and Your First Problem

  1. Go to acmicpc.net in your browser and sign up
  2. Type 1000 in the search box — it’s the "A+B" problem
  3. Read the problem: receive two numbers and print their sum

Code to submit:

a, b = map(int, input().split())
print(a + b)

How to read the code: input().split() splits one input line ("3 5") on whitespace, map(int, ...) converts both to integers, and a, b = stores them separately. It’s the standard first syntax of judging sites. Press the submit button, choose Python 3 as the language, and submit.

맞았습니다!!

(The judging result is given by the Baekjoon server, so this is a screen example — "맞았습니다!!" means "Accepted!!". It’s a result you’ll receive yourself.)

Why: this one problem is a full run of the whole "sign up–read–code–submit–verdict" cycle. Running this cycle ten times is this chapter’s goal.

3-2. Getting the Flow — The I/O and Arithmetic Stage

In the problem-set menu, open "단계별로 풀어보기" (Solve by Step) → the "입출력과 사칙연산" (I/O and Arithmetic) stage. Solve from the top in order: 2557 (Hello World), 1000 (A+B), 1001 (A-B), 10998 (A×B)…

10926 (??!) is string-format practice. When the input comes in as joonas:

name = input()
print(name + "??!")
joonas??!

(Local measurement 2026-09-09. Output verified by typing the input directly.)

How to read it: simple as it looks, it’s training in "input format exactly as given, output format exactly right." One space, one letter of case separates right from wrong.

Predict: if the input is baekjoon, what’s the output? Write it by hand, then submit and check.

3-3. Plain Language First — Applying the Procedure to a Loop Problem

Let’s practice the procedure with problem 2739 (the multiplication table). Problem: receive N and print the N-times table.

(c) Plain-language solution first:

1. Receive one number N.
2. For each number i from 1 to 9:
   print in the shape "N * i = N-times-i".

(d) Moving it into code:

n = int(input())
for i in range(1, 10):
    print(n, "*", i, "=", n * i)

The front part of the output with input 2 (measured 2026-09-09):

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6

How to read it: one line of the plain-language solution became one line of code. When you can see this correspondence, the procedure is starting to stick to your body. The output format (the spaces around "=", etc.) must match the problem’s example output exactly.

3-4. Fast Input — The Timeout-Prevention Vaccine

Later, when you meet a problem with tens of thousands of input lines, input() can be slow enough to cause a timeout. Let’s learn the standard technique for that in advance. At the very top of your code:

import sys
input = sys.stdin.readline

How to read it: an incantation that replaces input() with a faster one. In exchange, a newline character rides along at the end of the input, so when using it as a number, wrapping it like int(input()) works (the newline is ignored during conversion), and when you need a string, strip the newline with input().strip().

Why: even if you don’t need it right now, it’s putting a card in your hand to pull out when you hit the wall called "time limit exceeded."

3-5. Seeing the Slowdown Yourself — The Double-Loop Experiment

We experiment on my computer, not Baekjoon. speed.py:

import time

for size in (1000, 2000, 4000, 8000):
    start = time.time()
    count = 0
    for i in range(size):
        for j in range(size):
            count += 1
    print(size, "→", round(time.time() - start, 3), "sec")
1000 → 0.033 sec
2000 → 0.132 sec
4000 → 0.562 sec
8000 → 2.243 sec

(Measured 2026-09-09. The numbers differ per computer, but the multipliers come out similar.)

How to read the output: when the input doubles (1000→2000→4000→8000), the time grows fourfold each step — the measurement too came out at 0.033 → 0.132 → 0.562 → 2.243, exactly the four-times law. This is what feeling O(n²) in your body looks like. At this trend, 100,000 would take tens of minutes.

Why: the ability to know "you can’t solve it this way within the time limit" before writing code. That’s the real harvest of algorithm training, and it’s also the eye that calculates the limits of brute force.

3-6. Debugging Practice — Fixing Broken Code

Let’s fix deliberately broken code. Problem: "print the sum from 1 to N."

n = int(input())
total = 0
for i in range(n):
    total += i
print(total)

This code is wrong. range(n) runs from 0 to n-1, so n is left out. Measured with n=5 (2026-09-09):

Buggy code: 10      ← the answer is 15, but 10
Fixed code: 15      ← after fixing to range(1, n + 1)

How to read it: the fix is range(1, n + 1). Before fixing, calculate by hand what this code prints when n=3 (0+1+2=3, while the answer is 6). Then run it and compare. "Hand calculation → run and compare" is the standard move of algorithm debugging.

Why: judging only passes when everything is right, so you need the sense that separates "almost-right code" from "right code." That sense grows from the habit of substituting boundary values (the first and the last) by hand.

3-7. The Training of Recording — The Format of a Solution Note

From today, write your solution files in the same format:

Problem: 2739 multiplication table
Approach: loop from 1 to 9, printing "N * i = product"
Where I got stuck: the position of spaces in the output format
What I learned: the habit of comparing with the example output character by character

How to read it: four lines are enough. "Approach" is a one-line summary of the plain-language solution, and "where I got stuck" and "what I learned" are the heart of the review. As these records pile up, you can see as data which types of problems block you — someone who makes frequent format mistakes, someone who gets stuck at boundary values, someone who misreads problems — because the training method differs per weakness.

3-8. How to Read Judging Results

Let’s organize the meanings of the verdicts you’ll receive when you submit:

Verdict Meaning Next action
맞았습니다!! (Accepted) Passed every test input Congratulations, on to the next problem
틀렸습니다 (Wrong Answer) The answer differs on some input Suspect boundary values and logic
출력 형식이 잘못되었습니다 (Presentation Error) The answer is close but the format differs Compare spaces, newlines, letter case
시간 초과 (Time Limit Exceeded) The method is slow Fast input, reduce loops
런타임 에러 (Runtime Error) It dies during execution Check indexes, division, type conversion
컴파일 에러 (Compile Error) A syntax error prevents it from even running Run it locally once before submitting

How to read it: knowing these six turns the blankness of "why doesn’t it work?" into the diagnosis of "which kind of problem is it?" A verdict is not a punishment but a diagnosis chart. If you’re right it’s something learned, and if you’re wrong it’s something to learn — either way, it’s a game you win.


4. Missions & Exercises

Mission — Completing 10 Problems and a Solution Archive

Do it across several days. Don’t rush:

  1. Solve all of stage 1 (I/O and Arithmetic) of Baekjoon’s "Solve by Step"
  2. From stage 2 (Conditionals) and stage 3 (Loops) combined, receive "맞았습니다!!" on 10 or more problems
  3. Each time you solve one, store three things as files: the problem number, the solution code, the plain-language solution
    • Folder structure example: baekjoon/1000.py, baekjoon/1000_notes.txt
  4. If you meet even one "틀렸습니다" (Wrong Answer), diagnose the cause and write it in one line in the solution file (format? logic? boundary values?)
  5. When you’ve filled 10 problems, pick the one problem you were stuck on longest and review it in your notes: what blocked you, and how did you break through

Exercises

Q1. How does the judging server exchange things with our code? What are the two most common causes of "the examples work but it’s wrong"?

Q2. There’s double-loop code that took 0.03 seconds at input 1,000. Roughly how long will it take at input 100,000? Calculate using the multipliers from the 3-5 measurement as grounds.

Q3. State the five steps of the problem-solving procedure in order, and which step beginners skip most easily.

Q4. Name two things to try when you receive a "Time Limit Exceeded" verdict.


Answers & completion criteria · expand/collapse

5. Model Answers & Completion Criteria

Mission Model Answer

The mission is not a single piece of code but completing the procedure. What a passing archive looks like:

baekjoon/
 ├─ 1000.py
 ├─ 1000_notes.txt     ← approach: receive two numbers, print the sum. stuck: none
 ├─ 2557.py
 ├─ 2739.py
 ├─ 2739_notes.txt     ← stuck: spaces around "=". learned: compare with the example character by character
 └─ ... (10 or more problems)

How to verify: ① Is the "solved problems" count on your Baekjoon profile 10 or more? ② Are code and plain-language solutions paired in the folder? ③ Does the file of a problem you got wrong have a one-line cause diagnosis? ④ Is there a review note? Taking three days to fill 10 problems is normal — the unit of this training is not problems but days.

Exercise Solutions

Q1 solution. The server hands test data through standard input and takes the answer through standard output, comparing it with the prepared correct answers. The common causes of "the examples work but it’s wrong" are ① the logic collapses at boundary values absent from the examples (0, the maximum, etc.), and ② the output format (spaces, newlines) differs slightly.

Q2 solution. For a double loop, when the input doubles, the time quadruples (3-5 measurement: 0.033 → 0.132 → 0.562 → 2.243 sec, about 4× each time). Input 1,000 → 100,000 is 100×, so the time is 100 × 100 = 10,000×, roughly 0.03 sec × 10,000 ≈ 300 sec (about 5 minutes). For inputs like this, you must find a different approach, not a double loop.

Q3 solution. ① Read the problem three times → ② trace the examples by hand → ③ write a plain-language solution → ④ move it into code → ⑤ check with the examples, then submit. What beginners skip most easily is ③ — jumping straight into code is the beginner’s trap, and the thirty seconds of writing in plain language prevents thirty minutes of wandering.

Q4 solution. First, switch to the fast input from section 3-4 (import sys; input = sys.stdin.readline). Second, look at whether loops overlap — "can this double loop be reduced to a single loop" is the standard question for timeout problems. And since the input range (a line like "N is at most 1,000,000") decides the approach, don’t forget to reread the problem’s input range.

Completion Criteria Checklist

  • [ ] I signed up for Baekjoon and solved my first problem (#1000)
  • [ ] I experienced the whole cycle of sign up–read–code–submit–verdict
  • [ ] I used the procedure of writing a plain-language solution first, then moving it into code
  • [ ] I confirmed "double → quadruple" with my own eyes through the double-loop experiment
  • [ ] I know the meanings of the six judging verdicts and the next action for each
  • [ ] I diagnosed the buggy code (sum calculation) by hand calculation and fixed it
  • [ ] Mission: I completed 10 correct answers and a solution archive

6. Common Pitfalls & Fixes

Wall 1. The examples work, but "Wrong Answer"

Symptom: the examples work perfectly on my computer, but judging says wrong.
Cause: the logic collapses at inputs absent from the examples (boundary values like 0, negatives, the maximum), or the output format differs slightly.
Fix: reread the problem’s input range, and substitute the smallest and largest inputs by hand. For format, compare the example output and your output character by character.

Wall 2. Time Limit Exceeded

Symptom: the code seems right, but the verdict is "Time Limit Exceeded."
Cause: you used a slow method (a double loop, slow input) on a problem with large input. As in the 3-5 measurement, even a double loop at input 8,000 already exceeds 2 seconds.
Fix: first switch to fast input; if that still fails, find a way to reduce the double loop. If the input range says "at most 1,000,000," that’s a signal to abandon the double loop from the start.

Wall 3. Runtime Error / Index Error

Symptom: you submit and get "Runtime Error."
Cause: input doesn’t always come like the examples. Blank lines, fewer numbers than expected — you run past the list’s range.
Fix: reread the problem’s input format and assume "when the input is minimal." The habit of running every example input on your own computer before submitting reduces compile errors and runtime errors together.

Wall 4. range’s End Is One Short

Symptom: sums and counts keep being off by one (3-6 measurement: 15 appears where 10 should be for n=5, or the reverse).
Cause: range(n) runs from 0 to n-1. To include the end, use range(1, n + 1).
Fix: every time you write a loop range, ask out loud, "what’s the first value and the last value?" Substituting one boundary value by hand is the vaccine for this type.

Wall 5. Burning Two Hours on One Problem

Symptom: one problem won’t solve, the day passes, and motivation breaks.
Cause: a normal part of the beginner period, but it needs management.
Fix: I recommend the 30–40 minute rule. If there’s no progress within that time, fold the problem and solve a different one. An amazing number of problems solve themselves when you come back a few days later. The purpose of training is not the count of correct answers but the muscle.


7. Summary

Today’s Concepts

Concept One-line description
Automated judging Checks with every input the server prepared — one miss and it’s wrong
Standard input/output The channel judging uses — honoring the format is half the battle
Time complexity The law of how much slower it gets as input grows
O(n²) Double loop — double the input, quadruple the time (verified by measurement)
Boundary values The first and last inputs — the spot where bugs hide

Today’s Syntax

Syntax What it does
a, b = map(int, input().split()) Receive two numbers on one line as integers
import sys; input = sys.stdin.readline Switch to fast input
time.time() Measure code’s running time
range(1, n + 1) From 1 to n (inclusive)

The Instinct That Matters More Than Commands

A problem statement’s traps are usually in three places. (a) The input range — a single line like "N is at most 1,000,000" decides the entire approach. (b) The output format — one per line or space-separated. (c) The exception conditions — a line like "print -1 if equal." Underline these three places before you start solving. Thirty seconds of underlining prevents thirty minutes of wrong answers.

Remember two more things. First, today’s sense of time complexity later becomes the ruler that measures "how many days would it take to brute-force this password." Four digits is 10,000 tries, but eight lowercase letters is about 200 billion — measuring the time of an attack is also the work of setting the standard for defense. Second, algorithm skill is not an explosion but a savings account. One or two problems a day becomes two hundred problems in half a year. Circle the start date on your calendar. A hundred days later, you’ll look at that circle and find you’ve become a different person.


Once every box is checked, Step 54 is complete.

ONE STEP FURTHER

Finished this lesson?

Check the completion criteria, then mark your progress.

Something wrong with this page or a link? Let us know.
Next