Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

exercise 9 Operator switch

import Foundation
import Glibc
 
typealias Operation = (operandOne: Float, operandTwo: Float, operation: Character)
let exampleOne : Operation = (3.1, 33, "+")
switch exampleOne {
    case (_, _, "+"):
        let resoult = exampleOne.0 + exampleOne.1
        print(resoult)
    case (_, _, "-"):
        let resoult = exampleOne.0 - exampleOne.1
        print(resoult)
    case (_, _, "*"):
        let resoult = exampleOne.0 * exampleOne.1
        print(resoult)
    case (_, _, "/"):
        let resoult = exampleOne.0 / exampleOne.1
        print(resoult)
    default:
        print("Ошибка")
}

Session 9

import Foundation
import Glibc
 var people: [[String:Any]] = [
[
"firstName": "Calvin",
"lastName": "Newton",
"score": 13
],
[
"firstName": "Garry",
"lastName": "Mckenzie",
"score": 23
],
[
"firstName": "Leah",
"lastName": "Rivera",
"score": 10
],
[
"firstName": "Sonja",
"lastName": "Moreno",
"score": 3
],
[
"firstName": "Noel",
"lastName": "Bowen",
"score": 16
]
]

func  bestScore(_ arrOfPeople: [[String:Any]]) -> String {
    var personName = ""
    var personScore = 0
    for person in arrOfPeople {
    let score = person["score"] as! Int
        if  score > personScore {
           personScore = score 
           let firstName = person["firstName"] as! String
           let lastName = person["lastName"] as! String
           personName = "\(firstName) \(lastName)"
        }
    }
    
  return personName  
}
print(bestScore(people))

/*
var topPerson = people[0]
var bestScore = topPerson["score"] as! Int
    for person in people {
    if let score = person["score"] as? Int {
       if bestScore < score {
         bestScore = score
          topPerson = person
        }
    }
}
    if let first = topPerson["firstName"] as? String,
    let second = topPerson["lastName"] as? String {
    print("\(first) \(second)")
}

*/

//Tuples
var person = ("ahmed", "ali", "test")
print(person.0)
print(person.2)

var person2 = (firstName: "ahmed", age: 20 )
print(person2.firstName)
print(person2.age)

var (a, b, c) = (1, 2, 3)
print(a)

func dividmod(_ a: Int, _ b: Int) -> (Int, Int) {
     return (a / b , a % b)
}
print(dividmod(7, 3))

Session8 Tasks

import Foundation
import Glibc
var code = ["a" : "b",
"b" : "c",
"c" : "d",
"d" : "e",
"e" : "f",
"f" : "g",
"g" : "h",
"h" : "i",
"i" : "j",
"j" : "k",
"k" : "l",
"l" : "m",
"m" : "n",
"n" : "o",
"o" : "p",
"p" : "q",
"q" : "r",
"r" : "s",
"s" : "t",
"t" : "u",
"u" : "v",
"v" : "w",
"w" : "x",
"x" : "y",
"y" : "z",
"z" : "a"]

//Task 1
func encode(dictionary: [String: String], message: String) -> String {
var encodeMessage = ""
     for char in message.characters {
     if char == " " {
        encodeMessage += "\(char)"
     } else {
         encodeMessage += (dictionary["\(char)"]!) 
     }
     
    }
return encodeMessage
}
print(encode(dictionary: code, message: "hello world"))
/*
var message = "hello world"
var encodedMessage = ""
for char in message.characters {
    var character = "\(char)"
    if let encodedChar = code[character] {
    // letter
        encodedMessage += encodedChar
    } else {
    // space
        encodedMessage += character
    }
}
print(encodedMessage)
*/

//Task 2
func decode(dictionary: [String: String], message: String) -> String {
var decodeMessage = ""
var decode: [String : String] = [:]

    for (key, value) in dictionary {
        decode[value] = key
    }
        for char in message.characters {
            if char == " " {
               decodeMessage += "\(char)"
            } else {
              decodeMessage += (decode["\(char)"]!) 
        }
     
    }
return decodeMessage
}
print(decode (dictionary: code, message: "uijt nfttbhf jt ibse up sfbe"))

/*
var encodedMessage = "uijt nfttbhf jt ibse up sfbe"
var decoder: [String:String] = [:]
// reverse the code
    for (key, value) in code {
        decoder[value] = key
    }
var decodedMessage = ""
    for char in encodedMessage.characters {
        var character = "\(char)"
        if let encodedChar = decoder[character] {
    // letter
           decodedMessage += encodedChar
    } else {
    // space
            decodedMessage += character
    }
}
print(decodedMessage)
*/

//Task 3
var people: [[String:String]] = [
[
"firstName": "Calvin",
"lastName": "Newton"
],
[
"firstName": "Garry",
"lastName": "Mckenzie"
],
[
"firstName": "Leah",
"lastName": "Rivera"
],
[
"firstName": "Sonja",
"lastName": "Moreno"
],
[
"firstName": "Noel",
"lastName": "Bowen"
]
]
func firstNames(_ nameOfPeople: [[String : String]]) -> [String] {
var firstNames: [String] = [] 
    for person in nameOfPeople {
        firstNames += [(person["firstName"]!)]
    }
return firstNames
}
print(firstNames(people))

//Task 4
func fullNames (_ nameOfPeople: [[String : String]]) -> [String] {
var fullNames : [String] = [] 
    for person in nameOfPeople {
        fullNames  += [person["firstName"]! + " " + person["lastName"]!]
       // fullNames.append((person["firstName"]!) + " " + (person["lastName"]!))
    }
return fullNames 
}
print(fullNames(people))

testtesttest

import Foundation
import Glibc
 
let player = ["rock", "paper", "scissors", "lizard", "spock"]
 
srandom(UInt32(NSDate().timeIntervalSince1970))
for count in 1...3 {
    print(count)
}
 
print(player[random() % player.count]);