Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

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

Aberta Para iniciantes
#15,633 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

@raaj-2222 já está trabalhando nisso.

Desde 25/10/2026.

  • #15639 de @raaj-2222 — aberto
  • #15645 de @saiyam-jain-dev — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
85/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
cli

Direção de pesquisa

Abra other/password.py e leia alternative_password_generator; o bug está no bloco i -= len(chars_incl) / quotient = i // 3 / remainder = i % 3, que se comporta mal quando i fica negativo. Reproduza com os trechos da issue, depois decida entre lançar um ValueError ou limitar quando len(chars_incl) > i, e verifique se main() precisa expor esse erro. Concluído significa que ambos os casos de reprodução retornam o comprimento solicitado e o comportamento existente para entradas válidas permanece inalterado.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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.

Linguagem predominante
Python
Estrelas
225k
Forks
51.2k
Merge médio
1d 8h
PRs com merge (30d)
40

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de TheAlgorithms/Python

Todas as issues de TheAlgorithms/Python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.