/
disyuncion_excluyente.py
65 lines (54 loc) · 1.82 KB
/
disyuncion_excluyente.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# disyuncion_excluyente.py
# Disyunción excluyente.
# José A. Alonso Jiménez <https://jaalonso.github.io>
# Sevilla, 1-septiembre-2022
# ---------------------------------------------------------------------
# ---------------------------------------------------------------------
# La disyunción excluyente de dos fórmulas se verifica si una es
# verdadera y la otra es falsa. Su tabla de verdad es
# x | y | xor x y
# ------+-------+---------
# True | True | False
# True | False | True
# False | True | True
# False | False | False
#
# Definir la función
# xor : (bool, bool) -> bool
# tal que xor(x, y) es la disyunción excluyente de x e y. Por ejemplo,
# xor(True, True) == False
# xor(True, False) == True
# xor(False, True) == True
# xor(False, False) == False
# ---------------------------------------------------------------------
from typing import Any
from hypothesis import given
from hypothesis import strategies as st
# 1ª solución
def xor1(x: bool, y: bool) -> Any:
match x, y:
case True, True: return False
case True, False: return True
case False, True: return True
case False, False: return False
# 2ª solución
def xor2(x: bool, y: bool) -> bool:
if x:
return not y
return y
# 3ª solución
def xor3(x: bool, y: bool) -> bool:
return (x or y) and not(x and y)
# 4ª solución
def xor4(x: bool, y: bool) -> bool:
return (x and not y) or (y and not x)
# 5ª solución
def xor5(x: bool, y: bool) -> bool:
return x != y
# La propiedad de equivalencia es
@given(st.booleans(), st.booleans())
def test_equiv_xor(x: bool, y: bool) -> None:
assert xor1(x, y) == xor2(x, y) == xor3(x, y) == xor4(x, y) == xor5(x, y)
# La comprobación es
# src> poetry run pytest -q disyuncion_excluyente.py
# 1 passed in 0.11s