Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.69 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import Cocoa
  4.  
  5.  
  6. //Functions
  7. //1
  8. func minValue(first:Int,second:Int) -> Int{
  9.     if(first >= second){
  10.         return second;
  11.     }
  12.     else{
  13.         return first;
  14.     }
  15. }
  16.  
  17. print(minValue(first: 7,second: 6));
  18.  
  19. //2
  20. func lastDigit(first:Int)->Int{
  21.     return first%10;
  22. }
  23.  
  24. print(lastDigit(first: 123));
  25.  
  26. //3
  27. func divides(first:Int,second:Int) -> Bool{
  28.     if(first%second == 0){
  29.         return true;
  30.     }
  31.     else{
  32.         return false;
  33.     }
  34. }
  35.  
  36. func countDivisors(number:Int)->Int{
  37.     var divisorsNumber=0;
  38.     for i in 1...number{
  39.         if(divides(first: number,second: i)){
  40.             divisorsNumber=divisorsNumber+1;
  41.         }
  42.     }
  43.     return divisorsNumber;
  44. }
  45.  
  46. func isPrime(number:Int)->Bool{
  47.     if(countDivisors(number: number) == 2){
  48.         return true;
  49.     }
  50.     else{
  51.         return false;
  52.     }
  53. }
  54.  
  55. //Closures
  56. //1
  57. var closure: () -> ()={
  58.     print("I will pass this course with the best mark because Swift is great!");
  59. }
  60. func smartBart(n:Int,f:()->())->(){
  61.     for _ in 1...n{
  62.         f()
  63.     }
  64. }
  65. smartBart(n: 8, f: closure);
  66. //2
  67. let numbers = [10,16,18,30,38,40,44,50];
  68. print(numbers.filter{$0%4==0});
  69. //3
  70. print(numbers.reduce(0){if($1>$0){
  71.     return $1
  72.     }
  73.     else{
  74.     return $0
  75.     }})
  76. //4
  77. var strings = ["Gdansk","University","of","Technology"];
  78. print(strings.reduce(""){$0+$1+" "});
  79. //5
  80. let newNumbers = [1,2,3,4,5,6];
  81. print(newNumbers.filter{$0%2 == 1}.map{$0*$0}.reduce(0){$0+$1});
  82.  
  83. //Tuples
  84. //1
  85. func minmax(first:Int,second:Int)->(Int,Int){
  86.     return (first,second);
  87. }
  88.  
  89. var minmaxTuple = minmax(first: 4,second: 6);
  90. //2
  91. var stringsArray = ["gdansk","gdansk","gdansk","gdansk","university","university","university","of","technology","technology"];
  92. var counts: [String:Int] = [:];
  93. for string in stringsArray{
  94.     counts[string] = (counts[string] ?? 0) + 1
  95. }
  96. var countedStrings: [(String,Int)] = [];
  97.  
  98. for (key,value) in counts {
  99.     countedStrings.append((key,value));
  100. }
  101. print(countedStrings);
  102.  
  103. //Enums
  104. //1
  105. enum Day:Int{
  106.     case Monday=1;
  107.     case Tuesday=2;
  108.     case Wednesday=3;
  109.     case Thursday=4;
  110.     case Friday=5;
  111.     case Saturday=6;
  112.     case Sunday=7;
  113.  
  114.     func returnEmote() -> String{
  115.         switch self{
  116.         case.Monday:
  117.             return ":)"
  118.         case.Tuesday:
  119.             return ":/"
  120.         case.Wednesday:
  121.             return ":("
  122.         case.Thursday:
  123.             return ":D"
  124.         case.Friday:
  125.             return "XD"
  126.         case.Saturday:
  127.             return ":P"
  128.         case.Sunday:
  129.             return ":X"
  130.         }
  131.     }
  132. }
  133.  
  134. let day = Day.Monday;
  135. print(day);
  136. print(day.rawValue);
  137. print(day.returnEmote());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement