GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Hello-Python/Hello-Python/Basic/sets_utils.py at main · mrtngrsbch/Hello-Python

Expand file tree

/

sets_utils.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
92 lines (71 loc) · 2.44 KB
/

sets_utils.py

Copy path

File metadata and controls

92 lines (71 loc) · 2.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Utilidades puras para trabajar con conjuntos (Clase 06 — Sets).
- Enfoque funcional: NO mutamos los sets de entrada; devolvemos nuevos sets.
- Validamos tipos para dar feedback claro al alumno.
Ejemplos rápidos:
>>> new_set_from_iterable([1, 2, 2, 3])
{1, 2, 3}
>>> add({1, 2}, 3)
{1, 2, 3}
>>> discard({1, 2}, 5)
{1, 2}
>>> union({1, 2}, {2, 3})
{1, 2, 3}
"""
from typing import Any, Iterable, Set
def _ensure_set(s: Any) -> Set[Any]:
if not isinstance(s, set):
raise ValueError("Se esperaba un set como primer argumento")
return s # type: ignore[return-value]
def new_set_from_iterable(it: Iterable[Any]) -> Set[Any]:
"""Crea un set a partir de un iterable, eliminando duplicados."""
try:
return set(it)
except TypeError as e:
# Elementos no hashables, por ejemplo listas dentro de it
raise ValueError("Los elementos deben ser hashables para formar un set") from e
def add(s: Any, item: Any) -> Set[Any]:
"""Devuelve un nuevo set con item añadido (sin mutar s)."""
base = _ensure_set(s)
nuevo = set(base)
nuevo.add(item)
return nuevo
def remove(s: Any, item: Any) -> Set[Any]:
"""Devuelve un nuevo set sin item. Lanza KeyError si no existe (comportamiento estricto)."""
base = _ensure_set(s)
if item not in base:
raise KeyError(item)
nuevo = set(base)
nuevo.remove(item)
return nuevo
def discard(s: Any, item: Any) -> Set[Any]:
"""Devuelve un nuevo set sin item. Si no existe, no lanza error (modo seguro)."""
base = _ensure_set(s)
nuevo = set(base)
if item in nuevo:
nuevo.remove(item)
return nuevo
def contains(s: Any, item: Any) -> bool:
"""True si item pertenece al set s."""
base = _ensure_set(s)
return item in base
def union(a: Any, b: Any) -> Set[Any]:
"""Unión de dos sets (a ∪ b)."""
sa = _ensure_set(a)
sb = _ensure_set(b)
return sa.union(sb)
def intersection(a: Any, b: Any) -> Set[Any]:
"""Intersección de dos sets (a ∩ b)."""
sa = _ensure_set(a)
sb = _ensure_set(b)
return sa.intersection(sb)
def difference(a: Any, b: Any) -> Set[Any]:
"""Diferencia de sets (a − b)."""
sa = _ensure_set(a)
sb = _ensure_set(b)
return sa.difference(sb)
def symmetric_difference(a: Any, b: Any) -> Set[Any]:
"""Diferencia simétrica (elementos en uno u otro, pero no en ambos)."""
sa = _ensure_set(a)
sb = _ensure_set(b)
return sa.symmetric_difference(sb)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.