Menu Close

Tres iguales

Definir la función

   tresIguales :: Int -> Int -> Int -> Bool

tal que (tresIguales x y z) se verifica si los elementos x, y y z son iguales. Por ejemplo,

   tresIguales 4 4 4  ==  True
   tresIguales 4 3 4  ==  False

Soluciones

A continuación se muestran las soluciones en Haskell y las soluciones en Python.


Soluciones en Haskell

tresIguales :: Int -> Int -> Int -> Bool
tresIguales x y z = x == y && y == z

El código se encuentra en GitHub.


Soluciones en Python

from hypothesis import given, strategies as st
 
# 1ª definición
def tresIguales1(x: int, y: int, z: int) -> bool:
    return x == y and y == z
 
# 2ª definición
def tresIguales2(x: int, y: int, z: int) -> bool:
    return x == y == z
 
# La propiedad de equivalencia es
@given(st.integers(), st.integers(), st.integers())
def test_equiv_tresIguales(x, y, z):
    assert tresIguales1(x, y, z) == tresIguales2(x, y, z)
 
# La comprobación es
#    src> poetry run pytest -q tres_iguales.py
#    1 passed in 0.16s

El código se encuentra en GitHub.

Comentarios

  • La conjunción de x e y se calcula
    • en Haskell, con x && y y
    • en Python, con x and y.
  • En Python, x == y == z es equivalente a x == y and y == z.
Posted in Haskell y Python

Escribe tu solución

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.