Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

other/password.py: alternative_password_generator ignores the requested length when the required characters exceed it

オープン 初心者向け
#15,633 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@raaj-2222 がすでに取り組んでいます。

2026年10月25日 から。

  • #15639 @raaj-2222 による — オープン
  • #15645 @saiyam-jain-dev による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
cli

調査の方向性

other/password.py を開いて alternative_password_generator を読んでください。バグは i -= len(chars_incl) / quotient = i // 3 / remainder = i % 3 のブロックにあり、i が負になったときに不正な動作をします。issue のスニペットで再現した上で、len(chars_incl) > i の場合に ValueError を発生させるか値をクランプするかを決め、main() がそのエラーを通知する必要があるかも確認してください。完了とは、両方の再現ケースが要求された長さを返し、有効な入力に対する既存の動作が変わらないことを意味します。

索引モデルが issue の本文から書いたものです。

説明

Repository commit

6e03a48579ca212a08b36be00378f887dc41a003

Python version (python --version)

Python 3.13.5

Dependencies version (pip freeze)

None needed — other/password.py only uses the standard library (secrets, random, string).

Expected behavior

alternative_password_generator(chars_incl, i) should return a password whose length matches the requested length i (the main() prompt calls it "the max length of your password", and the comment above the function says i = how many letters or characters the password length will be).

Actual behavior

When chars_incl is longer than the requested length i, the subtraction i -= len(chars_incl) makes i negative, and the generator silently returns a password longer than what was asked for:

>>> from other.password import alternative_password_generator
>>> len(alternative_password_generator("abcdefgh", 5))   # asked for 5
8
>>> len(alternative_password_generator("abcd", 3))      # asked for 3
5

In the second case i becomes -1, so quotient = i // 3 is -1 and remainder = i % 3 is 2 (Python's modulo of a negative number is positive), which adds one extra random letter on top of all the required characters.

A user who types a max length smaller than their required-characters string in main() gets a password that silently exceeds the max length they asked for. If the requested length is at least len(chars_incl) everything works as expected.

Steps to reproduce

python - <<'EOF'
from other.password import alternative_password_generator
print(len(alternative_password_generator("abcdefgh", 5)))  # expected 5, prints 8
print(len(alternative_password_generator("abcd", 3)))      # expected 3, prints 5
EOF

Suspected cause

The negative remainder path in alternative_password_generator:

i -= len(chars_incl)
quotient = i // 3
remainder = i % 3

When len(chars_incl) > i, quotient goes negative (the random() calls then contribute nothing because range() of a negative is empty) while remainder stays positive, so quotient + remainder adds extra characters. Guarding the input (if i < len(chars_incl): raise ValueError(...) or clamping) would keep the generator honest about the requested length.

主要言語
Python
スター
225k
フォーク
51.2k
平均マージ
1日 13時間
マージ済み PR(30日)
32

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TheAlgorithms/Python のほかの issue

TheAlgorithms/Python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。