Skip to content

appleweiping/cs50p

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CS50P — Introduction to Programming with Python

Complete solutions to Harvard's CS50P — every problem set implemented and verified, with pytest suites passing — an independent, from-scratch build that is part of a csdiy.wiki full-catalog effort.

status language tests license

Overview

CS50P (CS50's Introduction to Programming with Python) is Harvard's introduction to programming, taught by David J. Malan. It covers functions and variables, conditionals, loops, exceptions, libraries, unit tests, file I/O, regular expressions, and object-oriented programming, ending with an open-ended final project.

This repository implements all 43 problems across the 9 problem sets plus a substantive final project. Every solution runs end-to-end; the problems that ship with (or require) test_*.py are covered by real pytest suites, and every program was executed on sample inputs with the output captured to results/.

Results (measured on Windows, CPU-only, Python 3.11.2)

All 46 unit tests pass across 10 pytest suites (full log: results/pytest.txt):

Suite Tests Result
pset5/test_twttr 4 passed
pset5/test_bank 3 passed
pset5/test_plates 5 passed
pset5/test_fuel 5 passed
pset7/numb3rs 3 passed
pset7/working 5 passed
pset7/um 4 passed
pset8/jar 4 passed
pset8/seasons 4 passed
project 9 passed
Total 46 46 passed

Selected real program runs (full log: results/sample_runs.txt):

$ echo "Hello :) Goodbye :(" | python pset0/faces/faces.py
Hello 🙂 Goodbye 🙁

$ python pset4/bitcoin/bitcoin.py 2.5          # live price from CoinGecko
$158,167.5000

$ python pset6/pizza/pizza.py regular.csv
+-----------------+---------+---------+
| Regular Pizza   | Small   | Large   |
+=================+=========+=========+
| Cheese          | $13     | $26     |
+-----------------+---------+---------+
| Mushroom        | $14     | $28     |
+-----------------+---------+---------+

$ echo "2001-01-01" | python pset8/seasons/seasons.py
Five hundred twenty-five thousand, six hundred minutes

The pset8/shirtificate program renders a real A4 PDF certificate; a generated sample is saved at results/shirtificate_sample.pdf.

Implemented problem sets

  • Pset 0 — Functions, Variables: indoor, playback, faces, einstein, tip
  • Pset 1 — Conditionals: deep, bank, extensions, interpreter, meal
  • Pset 2 — Loops: camel, coke, twttr, plates, nutrition
  • Pset 3 — Exceptions: fuel, taqueria, grocery, outdated
  • Pset 4 — Libraries: emojize, figlet, adieu, game, professor, bitcoin
  • Pset 5 — Unit Tests: test_twttr, test_bank, test_plates, test_fuel
  • Pset 6 — File I/O: lines, pizza, scourgify, shirt
  • Pset 7 — Regular Expressions: numb3rs, watch, working, um, response
  • Pset 8 — OOP: seasons, jar, shirtificate
  • Final Project: a command-line task tracker (project/)

Project structure

cs50p/
├── pset0/  indoor playback faces einstein tip
├── pset1/  deep bank extensions interpreter meal
├── pset2/  camel coke twttr plates nutrition
├── pset3/  fuel taqueria grocery outdated
├── pset4/  emojize figlet adieu game professor bitcoin
├── pset5/  test_twttr test_bank test_plates test_fuel   (each: <prog>.py + test_<prog>.py)
├── pset6/  lines pizza scourgify shirt
├── pset7/  numb3rs watch working um response            (numb3rs/working/um ship pytest suites)
├── pset8/  seasons jar shirtificate                     (seasons/jar ship pytest suites)
├── project/  project.py  test_project.py  requirements.txt
├── results/  pytest.txt  sample_runs.txt  shirtificate_sample.pdf
├── requirements.txt
└── LICENSE

How to run

# Python 3.11. Install the dependencies the solutions actually import:
python -m pip install -r requirements.txt
# (this repo was built against the shared csdiy env:
#   D:\Project\_csdiy\.venv-ml\Scripts\python.exe)

# Run any program (each lives in its own folder):
python pset0/tip/tip.py
python pset4/bitcoin/bitcoin.py 2.5
python pset6/pizza/pizza.py pset6/pizza/regular.csv

# Run a test suite (cd into the problem's folder so imports resolve):
cd pset8/jar && pytest
cd project && pytest

Some programs need a data file that is provided in the folder (e.g. pset6/pizza/regular.csv, pset8/shirtificate/shirtificate.png, pset6/shirt/shirt.png).

Verification

  • Unit tests: pytest was run in every folder that contains a test_*.py; all 46 tests pass. Full transcript in results/pytest.txt.
  • Real runs: every program was executed on representative inputs (including error paths — bad command-line args, invalid dates, out-of-range fuel fractions) and the outputs captured to results/sample_runs.txt.
  • Live API: bitcoin.py was run against the CoinGecko public API and returned a real USD-denominated total.
  • Binary artifacts: shirtificate.py produced a valid %PDF-1.4 file (65 KB), and shirt.py produced a fitted JPEG overlay — both verified by re-opening with Pillow.

Final project — Task Tracker

A small but complete command-line to-do manager (project/project.py) that exercises most of the course's ideas at once: argparse sub-commands, JSON file persistence, exception handling, regex/datetime date validation, and priority-based sorting.

python project/project.py add "Submit CS50P project" --priority high --due 2026-12-31
python project/project.py list --pending
python project/project.py done 1
python project/project.py remove 2

It has a dedicated pytest suite (test_project.py, 9 tests) covering task creation, due-date validation, priority/status sorting, completion, and removal.

Tech stack

Python 3.11 · pytest · emoji · inflect · pyfiglet · requests · tabulate · Pillow · fpdf2 · validators (standard library: re, csv, json, argparse, datetime, sys).

Key ideas / what I learned

  • Writing pure, testable helper functions (separating I/O from logic) so pytest can drive them directly — the whole point of Pset 5.
  • Robust input validation with try/except and re-prompting (fuel, outdated, professor).
  • Regular expressions with re.fullmatch / re.findall and word boundaries for IPv4 validation, YouTube URL extraction, and word counting.
  • Object-oriented design with @property-guarded invariants (the Jar class).
  • Using third-party libraries idiomatically: tabulate for tables, fpdf2 for PDFs, Pillow for image compositing, inflect for number-to-words.

Credits & license

Based on the problem sets of CS50's Introduction to Programming with Python (CS50P) by David J. Malan and Harvard University. This repository is an independent educational reimplementation; all course materials, specifications, and provided assets belong to their original authors. Original code here is released under the MIT License.

About

Solutions to Harvard CS50P (Introduction to Programming with Python) — all problem sets with their pytest test suites passing

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages