Skip to content

Instantly share code, notes, and snippets.

@hipdev
Created March 24, 2024 16:15
Show Gist options
  • Save hipdev/829dc31871b0b9bfdb790a46c8419f50 to your computer and use it in GitHub Desktop.
Save hipdev/829dc31871b0b9bfdb790a46c8419f50 to your computer and use it in GitHub Desktop.
Without for
import random
def generar_numero_secreto():
while True:
numero_secreto = random.randint(100, 999) # Asegura un número de tres dígitos.
s = str(numero_secreto)
if s[0] != s[1] and s[1] != s[2] and s[0] != s[2]: # Verifica dígitos diferentes.
return numero_secreto
def evaluar_intento(intento, secreto):
intento_s = str(intento)
secreto_s = str(secreto)
# Inicializamos PIN y PON como 0.
pin = 0
pon = 0
# Verificamos cada dígito individualmente para PIN.
if intento_s[0] == secreto_s[0]:
pin += 1
if intento_s[1] == secreto_s[1]:
pin += 1
if intento_s[2] == secreto_s[2]:
pin += 1
# Verificamos cada dígito individualmente para PON.
# Un PON ocurre si el dígito está en el número pero en una posición diferente.
if intento_s[0] in secreto_s and intento_s[0] != secreto_s[0]:
pon += 1
if intento_s[1] in secreto_s and intento_s[1] != secreto_s[1]:
pon += 1
if intento_s[2] in secreto_s and intento_s[2] != secreto_s[2]:
pon += 1
# Ajustamos para evitar contar dígitos duplicados como PON si ya se contaron como PIN.
if intento_s[0] in secreto_s and intento_s[0] == secreto_s[1] or intento_s[0] == secreto_s[2]:
pon -= 1
if intento_s[1] in secreto_s and intento_s[1] == secreto_s[0] or intento_s[1] == secreto_s[2]:
pon -= 1
if intento_s[2] in secreto_s and intento_s[2] == secreto_s[0] or intento_s[2] == secreto_s[1]:
pon -= 1
return pin, pon
# Generamos el número secreto (esto normalmente estaría oculto al jugador).
numero_secreto = generar_numero_secreto()
print(f"DEBUG: Número secreto generado: {numero_secreto}") # Esta línea es solo para propósitos de demostración.
# Simulamos un intento del jugador.
intento_jugador = int(input("Intenta adivinar el número de 3 dígitos con dígitos diferentes: "))
# Evaluamos el intento.
pin, pon = evaluar_intento(intento_jugador, numero_secreto)
print(f"Resultado: {pin} PIN(s), {pon} PON(s)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment