No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Iteraciones y operaciones sobre conjuntos

19/33
Recursos

Aportes 18

Preguntas 1

Ordenar por:

Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.

Resumen

let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let primeNumbers: Set = [2, 3, 5, 7]

A union B = elementos que son o bien de A, o bien de B o de los dos

oddDigits.union(evenDigits).sorted()

A intersection B = elementos que son de A y B

oddDigits.intersection(evenDigits)
evenDigits.intersection(primeNumbers).sorted()
oddDigits.intersection(primeNumbers).sorted()

A - B = elementos que son de A pero no de B

oddDigits.subtracting(primeNumbers).sorted() // Todos los numeros impares QUE NO son primos.

Diferencia simetrica: A + B = (A-B) union (B - A)

oddDigits.symmetricDifference(primeNumbers).sorted() // Los numeros pares que no son primos y los numeros primos que no son pares.

Formas de comprobar si:

  • un conjunto es subconjunto de otro
  • un conjunto es superconjunto de otro
  • un conjunto es disjunto de otro
let houseAnimals: Set = ["🐶", "😹"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "😹"]
let cityAnimals: Set = ["🐁", "🕊"]

Un conjunto es subconjunto de otro

houseAnimals.isSubset(of: farmAnimals)

Un conjunto es superconjunto de otro

farmAnimals.isSuperset(of: houseAnimals)

A y B son disjuntos si su interseccion es vacia

farmAnimals.isDisjoint(with: cityAnimals)
let favouriteFoods:Set = ["arroz", "mole", "mandarina", "pizza", "galletas", "carne", "pollo", "manzana"]
let fruits: Set = ["platano", "manzana", "mandarina", "naranja", "guayaba", "papaya"]

fruits.isSubset(of: favouriteFoods) // false

fruits.union(favouriteFoods).sorted()
//["arroz", "carne", "galletas", "guayaba", "mandarina", "manzana", "mole", "naranja", "papaya", "pizza", "platano", "pollo"]
fruits.intersection(favouriteFoods).sorted()
//["mandarina", "manzana"]
fruits.subtracting(favouriteFoods).sorted()
//["guayaba", "naranja", "papaya", "platano"]
fruits.symmetricDifference(favouriteFoods).sorted()
//["arroz", "carne", "galletas", "guayaba", "mole", "naranja", "papaya", "pizza", "platano", "pollo"]

// Reto
let favouriteFood: Set = ["Pizza", "Tacos", "Pollo", "Manzana", "Platano", "Carne", "Pasta" ]
let favouriteFruits: Set = ["Manzana", "Pera", "Melon", "Uvas", "Platano", "Papaya", "Piña"]

favouriteFood.union(favouriteFruits).sorted()
favouriteFood.intersection(favouriteFruits).sorted()
favouriteFood.subtracting(favouriteFruits).sorted()
favouriteFood.symmetricDifference(favouriteFruits).sorted()
favouriteFood.isSubset(of: favouriteFruits)
favouriteFood.isSuperset(of: favouriteFruits)
favouriteFood.isDisjoint(with: favouriteFruits)
let frutas: Set = ["Manzana", "Pera", "Naranja", "Uva", "Sandia", "Mango"]
let comidas: Set = ["Hamburguesa", "Pizza", "Tacos", "Sandia", "Sushi", "Pollo", "Naranja"]

comidas.isSubset(of:frutas)

comidas.union(frutas).sorted()

comidas.intersection(frutas).sorted()

comidas.subtracting(frutas).sorted()

comidas.symmetricDifference(frutas).sorted()

comidas.isDisjoint(with: frutas)

frutas.isDisjoint(with: comidas)```

Hola chicos y chicas que estén cursando ese módulo en pleno 2022! Recuerden que al hacer el sorted de una “colección de datos” el tipo de impresiotn cambia:

Al hacer un sorted de la intersección:

evenDigits.intersection(primeNumbers).sorted()
Se imprime => [2]

Al no hacerlo

evenDigits.intersection(primeNumbers).sorted()
Se imprime => {2}

// operaciones:
var oddNUmbers: Set = [1,3,5,7,9]
var evenNumbers: Set = [2,4,6,8,10]
var primeNumbers: Set = [2,3,5,7]

//Union:
oddNUmbers.union(evenNumbers).sorted()

//Interseccion:
oddNUmbers.intersection(primeNumbers).sorted()

//substraccT: elementos de A pero no de B:
// A.substract(B)
oddNUmbers.subtract(primeNumbers)

// SymetricDifference:
// A + B = A - B union B - A
oddNUmbers.symmetricDifference(primeNumbers)

var houseAnimals:Set = ["🐶","😼"]
var farmAnimals:Set = ["🐮","🐔","🦆","🐑","🐶","😼"]
var cityAnimals:Set = ["🕊","🕷","🐀"]

// isSubset
// si es subconjunto
houseAnimals.isSubset(of: farmAnimals)

// isSuperSet
// es un conjunto que contiene :
farmAnimals.isSuperset(of: houseAnimals)

// tienen elementos en común?
farmAnimals.isDisjoint(with: houseAnimals)
houseAnimals.isDisjoint(with: farmAnimals)
houseAnimals.isDisjoint(with: cityAnimals)
cityAnimals.isDisjoint(with: houseAnimals)

Ejercicio 😃

let aula1 : Set<String> = ["Jose","Carlos","Miguel","Maria","Enriquez","Rosa","Eungenia","Hernan"]
let cursoProgramacion : Set<String> = ["Jose","Carlos","Miguel","Maria","Enriquez"]
let cursoEnglish : Set<String> = ["Jose","Miguel","Carlos","Rosa","Eungenia","Hernan"]
let cursoMecanica : Set<String> = ["Rosa","Eungenia","Hernan"]


let subconjunto = cursoEnglish.isSubset(of: aula1)
let subconjunto2 = aula1.isSuperset(of: cursoProgramacion)
let subconjuntoDisjunto = cursoMecanica.isDisjoint(with: cursoProgramacion)

//reto
let foodILike:Set = ["banana", "asian food", "chicken", "orange", "hamburguer"]
let fruits:Set = ["banana", "orange", "pinapple", "strawberry", "apple", "blueberry"]

foodILike.isSubset(of: fruits)
foodILike.isSuperset(of: fruits)
foodILike.isDisjoint(with: fruits)
//Ejercicio
let myFavoriteFood: Set = ["Guacamole", "Arroz", "Café", "Zanahoria", "Zukini", "Tomate", "Berenjena", "Lenteja", "Garbanzo", "Pollo", "Hamburguesa", "Panini", "Manzana", "Fresa", "Queso"]
let frutas: Set = ["Curuva", "Guanabana", "Fresa", "Mora", "Mandarina", "Naranja", "Manzana", "Banano"]

myFavoriteFood.isSubset(of: frutas) //Subconjunto
myFavoriteFood.isSuperset(of: frutas) //Superconjunto
myFavoriteFood.intersection(frutas) //Intersección
myFavoriteFood.union(frutas) //Unión
myFavoriteFood.subtracting(frutas) //Diferencia
myFavoriteFood.symmetricDifference(frutas) //Diferencia simétrica

for item in myFavoriteFood.symmetricDifference(frutas)
{
    print(item)
}
/*OUTPUT: Garbanzo Tomate Pollo Panini Naranja Mora Zukini Café Lenteja Guacamole Berenjena Arroz Hamburguesa Queso Zanahoria Guanabana Curuva Banano Mandarina */

// Reto.
// Create a Set of Foods you like and another with fruits or vegetables


let favouriteFood: Set = ["puré", "papás fritas", "pollo", "tallarines"]
let favouriteFruits: Set = ["naranja", "platano", "pera", "piña", "mango"]

// union
favouriteFruits.union(favouriteFruits).sorted()

// Intesection
favouriteFood.intersection(favouriteFruits).sorted()

// Subtracting
favouriteFood.subtracting(favouriteFruits).sorted()

// symmetricDifference
favouriteFood.symmetricDifference(favouriteFruits).sorted()

// isSubset
favouriteFood.isSubset(of: favouriteFruits)

// isSuperSet
favouriteFood.isSuperset(of: favouriteFruits)

// isDisjoint
favouriteFood.isDisjoint(with: favouriteFruits)

Me diverti con estos ejercicios:

let oddDigits: Set = [1,3,5,7,9]
    let evenDigits: Set = [0,2,4,6,8]
    let primeDigits: Set = [2,3,5,7]
    
    // oddDigits union evenDigits
    print("oddDigits union evenDigits: \(oddDigits.union(evenDigits).sorted())")
    // oddDigits intersection primeDigits
    print("oddDigits intersection primeDigits: \(oddDigits.intersection(primeDigits).sorted())")
    // evenDigits symmetric diference primeDigits
    print("evenDigits symmetric diference primeDigits: \(evenDigits.symmetricDifference(primeDigits).sorted())")
    // oddDigits subtracting primeDigits
    print("OddDigits subtracting primeDigits: \(oddDigits.subtracting(primeDigits))")```