Skip to content

feat(caesar): add automated frequency analysis auto-solve using chi-squared statistics - #4

Merged
Aakash02A merged 2 commits into
Aakash02A:mainfrom
txltedxgod:feat/caesar-chi-squared-auto-solve
Nov 9, 2026
Merged

Aakash02A merged 2 commits into
Aakash02A:mainfrom
txltedxgod:feat/caesar-chi-squared-auto-solve

Conversation

@txltedxgod

Copy link
Copy Markdown
Contributor

Summary

Implements automated frequency-analysis cracking for the Caesar cipher using Chi-squared ($\chi^2$) goodness-of-fit statistics against standard English letter frequencies, addressing the task in #2.

Key Changes

  • Added auto_solve_caesar(ciphertext: str) -> list[tuple[int, str, float]]:
    • Iterates over all 25 possible shifts.
    • Computes letter distribution and evaluates Chi-squared statistic:
      $$\chi^2 = \sum \frac{(O_i - E_i)^2}{E_i}$$
    • Returns candidate plaintexts ranked by lowest $\chi^2$ error (highest probability).
  • Added interactive frequency_auto_solve_demo() displaying top 5 predictions with candidate snippets and $\chi^2$ confidence scores.
  • Added option 5. Auto-Solve via Chi-Squared Analysis (Auto-Crack) to caesar_menu().
  • Added automated unit test test_caesar_auto_solve() in tests/test_level_01_classical.py verifying accurate plaintext recovery without knowing the shift.

Resolves task in #2.

@Aakash02A

Copy link
Copy Markdown
Owner

Thanks for the contribution! Please handle non-English characters safely in the Caesar solver, add tests for Unicode and input with no English letters, and remove pr_body.md if it isn’t needed. Then please update the PR and check that CI passes. I’ve verified the tests locally.

@txltedxgod

Copy link
Copy Markdown
Contributor Author

Updated! Addressed all points:

  1. Handled non-English & Unicode characters safely by preserving characters outside the ASCII English alphabet ('A' <= ch <= 'Z' / 'a' <= ch <= 'z') during shifting.
  2. �uto_solve_caesar() now calculates letter counts strictly across English alphabetic letters (A-Z) and safely returns an empty list if no English letters are found.
  3. Added tests for inputs with no English letters (empty, digits/symbols, Cyrillic, Japanese) as well as Unicode preservation during cipher rounds.
  4. Removed pr_body.md.
    All unit tests passing locally!

@Aakash02A
Aakash02A merged commit 54e4e1a into Aakash02A:main Nov 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants