«   2024/12   »
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
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

swift 연산자 모음 예제들 본문

IOS/예제모음

swift 연산자 모음 예제들

행복한 수지아빠 2017. 7. 17. 12:34

출처 : http://codeman77.tistory.com/82


import UIKit;


// 홀수 집합

let oddNumberSet : Set = [ 13579 ];

// 짝수 집합

let evenNumberSet : Set = [ 02468 ];

// 소수 집합

let primeNumberSet : Set = [ 2357 ];


// 연산 결과를 저장할 집합

var resultNumberSet : Set = Set<Int>();


// 교집합(Intersection) :  집합의 공통된 요소

print("홀수짝수 교집합");

resultNumberSet = oddNumberSet.intersection(evenNumberSet);

// []

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


print("홀수소수 교집합");

resultNumberSet = oddNumberSet.intersection(primeNumberSet);

// [ 3, 5, 7 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


// 차집합(Subtract) : 한쪽 집합에만 있는 요소

print("홀수짝수 차집합");

resultNumberSet = oddNumberSet.subtracting(evenNumberSet);

// [ 1, 3, 5, 7, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


print("홀수소수 차집합");

resultNumberSet = oddNumberSet.subtracting(primeNumberSet);

// [ 1, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


// 합집합(Union) : 양쪽 집합에 있는 모든 요소

print("홀수짝수 합집합");

resultNumberSet = oddNumberSet.union(evenNumberSet);

// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


print("홀수소수 합집합");

resultNumberSet = oddNumberSet.union(primeNumberSet);

// [ 1, 2, 3, 5, 7, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


// 대칭차(Symmetric Difference) :  집합의 상대 여집합의 합집합에서 교집합을  

print("홀수짝수 대칭차");

resultNumberSet = oddNumberSet.symmetricDifference(evenNumberSet);

// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


print("홀수소수 대칭차");

resultNumberSet = oddNumberSet.symmetricDifference(primeNumberSet);

// [ 1, 2, 9 ]

for i in resultNumberSet.sorted(){

    print(i);

}

resultNumberSet.removeAll();

print();


실행 결과

실행 결과



출처: http://codeman77.tistory.com/82 [☆]

출처: http://codeman77.tistory.com/82 [☆]