Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở Phù hợp với người mới
#15,633 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@raaj-2222 đang làm issue này rồi.

Từ ngày 25/10/2026.

  • #15639 của @raaj-2222 — đang mở
  • #15645 của @saiyam-jain-dev — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
cli

Hướng nghiên cứu

Mở other/password.py và đọc alternative_password_generator; lỗi nằm trong khối i -= len(chars_incl) / quotient = i // 3 / remainder = i % 3, hoạt động sai khi i trở thành số âm. Tái hiện bằng các đoạn mã trong issue, sau đó quyết định giữa việc ném ValueError hay giới hạn khi len(chars_incl) > i, và kiểm tra xem main() có cần hiển thị lỗi đó không. Hoàn thành nghĩa là cả hai trường hợp tá hiện đều trả về độ dài được yêu cầu và hành vi hiện tại với đầu vào hợp lệ không thay đổi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
225k
Fork
51.2k
Merge trung bình
1 ngày 13 giờ
Pull request đã merge (30 ngày)
32

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của TheAlgorithms/Python

Tất cả issue của TheAlgorithms/Python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.