Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.91 KB

File metadata and controls

60 lines (42 loc) · 1.91 KB

Algorithms Basics (Python)

This module introduces common algorithmic patterns over lists.

Learning Metadata

  • Difficulty: Intermediate.
  • Estimated Time: 30-45 minutes.
  • Prerequisites: 01-foundations/arrays-and-vectors, 01-foundations/functions.
  • Cross-Language Lens: Compare hand-written loops with library helpers and see when explicit iteration stays clearer for beginners.

Quick Run

python example/main.py

Topics Covered

  • Linear search for the first matching index.
  • Counting occurrences of a target value.
  • One-pass minimum and maximum scanning.
  • Combining multiple statistics in a single loop.

Common Pitfalls

  • Forgetting to handle empty lists before min/max logic.
  • Initializing minimum or maximum with invalid defaults.
  • Doing multiple passes when one pass is enough.

Cross-Language Notes

  • Compared with the C++ baseline, the same traversal and aggregation ideas are expressed with different defaults for loops, collections, and helper functions.
  • Relative to Python, the statically typed tracks make intermediate state and accumulator types more explicit.
  • The useful comparison is algorithm shape staying constant while the surrounding syntax and safety rails change.

Exercise Focus

  • exercises/01.py: linear search and first index.
  • exercises/02.py: minimum, maximum, and even-count in one pass.

Exercise Specs

  1. exercises/01.py
  • Input: integer n, then n values, then a target.
  • Output: first index of target or -1.
  • Edge cases: n <= 0; target not present.
  1. exercises/02.py
  • Input: integer n, then n values.
  • Output: minimum, maximum, and even count.
  • Edge cases: all odd numbers (even count 0); all equal values.

Checkpoint

  • I can implement linear search confidently.
  • I can compute multiple statistics in one pass.
  • I can reason about edge cases before coding.
  • I completed exercises/01.py.
  • I completed exercises/02.py.