import Foundation import Glibc //Exercise 1 //1 /*var a = 5; var b = 10; var sum = a + b; print(sum);*/ //2 //var string1 = "Gdansk University of Technology"; //var string2 = string1.replacingOccurrences(of: "n", with: "⭐") //3 /*var fullname = "Juris Lavrinovics"; var reverse = String(fullname.reversed()); print(reverse)*/ //CONTROL FLOW //peredacha danych po seti (primer o opcionalnosci) /*var data: String? var error: String? //esli bude nil to siuda ne popadajem if let errorUnwraped = error{ print("Error type: " + errorUnwraped) }else { if let dataUnwraped = data{ print("Data is: " + dataUnwraped) }else{ print("ni4ego ne prishlo") } } /////////////2 sposob if error == nil{ print("Peredacha danych uspeshna") if data != nil{ print("Priniali: " + data!) //razvernuli dannye }else{ print("Ni4ego ne preshlo") } }else{ print("Peredacha danych s oshybkoi: " + error!) } */ /* var optionalName: String? = "Robert Ostrowski" var greeting = "Hello!" greeting = "Hello, " + (optionalName ?? "Juris") print(greeting) */ /*if let name = optionalName { greeting = "Hello, \( name )" } print ( greeting ) */ /* for i in 1..<5 { print (i, terminator:" ") } print("") for j in (1..<5).reversed(){ print(j, terminator:" ") } print("") */ //Exercise 2 /* //1 for _ in 1...11{ print("I will pass this course with best mark, because Swift is great!") } //2 var N: Int? = 5 if let NUnwraped = N{ for i in 1...NUnwraped{ print(i*i, terminator:" ") } } print() //3 var M = 5 for _ in 1...M{ for _ in 1...M{ print("@", terminator: "") } print() } */ //arrays //mutable arrays /*var arrayOfInt = [Int]() var arrayOfInt2 = Array<Int>() for i in 1...10{ arrayOfInt.append(i) } print ( "Array have \( arrayOfInt . count ) elements." ) print(arrayOfInt) arrayOfInt = [] print ( "Array have \( arrayOfInt . count ) elements." ) print(arrayOfInt) var students = ["Hommer", "Lisa", "Bart"] if students . isEmpty { print ( "No students on board😞" ) }else{ print ( "Students are: " + "\( students )" ) } //add new item at the end of array students.append("Marge") //multiple addings to array students += [ "Apu" , "Barney" , "Nelson" ] */ //arays starting from 0 //let size = students.count //Exercise 3 //Print the maximum value in array: /*var numbers = [5, 10, 20, 15, 80, 13] let numMax = numbers.reduce(Int.min, { max($0, $1) }) let numMin = numbers.reduce(Int.max, { min($0, $1) }) print("Max: \(numMax) \n Min: \(numMin)")*/ //Print the numbers from array in reversed order: //var nums = [5, 10, 20, 15, 80, 13] //var size = nums.count //Print the numbers from array in reversed order: /*for i in (0..<size).reversed(){ print(nums[i], terminator:" ") }*/ //Create a new array containing unique numbers from allNumbers and then /*var allNumbers = [10, 20, 10, 11, 13, 20, 10, 30] var unique = Array(Set(allNumbers)) print(unique) extension Array where Element: Hashable { func removingDuplicates() -> [Element] { var addedDict = [Element: Bool]() return filter { addedDict.updateValue(true, forKey: $0) == nil } } mutating func removeDuplicates() { self = self.removingDuplicates() } } let numb = [10, 20, 10, 11, 13, 20, 10, 30] let unique = numb.removingDuplicates() print(unique) */ //sets /* var musicTypes: Set = [ "Rock" , "Classic" , "Hip hop" ] musicTypes.insert("Jazz") let oddDigits: Set = [1,3,5,7,9] let evenDigits: Set = [0,2,4,6,8] let singleDigitPrimeNumbers: Set = [2,3,5,7] //print(oddDigits.union(evenDigits).sorted()) //print(singleDigitPrimeNumbers.intersection(oddDigits).sorted()) var number = 10 var j = 0 var divisors = Set<Int>() for i in 1...number{ if number % i == 0{ divisors.insert(i) } } print(divisors) */ //dictionaries /* var airports = [ "GDN" : "Gdansk" , "NYO" : "Stockholm Skavsta" ] print(airports) //add the value airports [ "SFO" ] = "San Francisco Airport" print(airports) //change the value airports [ "SFO" ] = "San Francisco" //airports [ "SFO" ] = nil print(airports) for (airportCode, airportName) in airports { print ( " \( airportCode ): \( airportName )" ) } for airportCode in airports.keys { print ( "Airport code: \( airportCode )" ) } for airportName in airports . values { print ( "Airport name: \( airportName )" ) } */ //Exercise 1 //You are given an array of dictionaries. Each dictionary in the array contains 2 keys //flightNumber and destination . Create a new array of strings called f lightNumbers //that will contain only flightNumber value from each dictionary. /* var lightNumbers = Array<String>() var flights: [[ String : String ]] = [ [ "flightNumber" : "AA8025" , "destination" : "Copenhagen" ], [ "flightNumber" : "BA1442" , "destination" : "New York" ], [ "flightNumber" : "BD6741" , "destination" : "Barcelona" ] ] var i = 0 while i < flights.count { let tmp = Array(flights[i].values)[0] lightNumbers.append(tmp) i += 1 } print(lightNumbers) */ //2 //You are given an array of strings (names). Create new array of dictionaries which //contains 2 keys f irstName , which will be name from names array and l astName //which will be hardcoded string value. var names = [ "Hommer" , "Lisa" , "Bart" ] var fullName: [[ String : String ]] = [ [ "lastName": "Simpson", "firstName": "Hommer" ], [ "lastName": "Simpson", "firstName": "Lisa" ], [ "lastName": "Simpson", "firstName": "Bart" ] ] print(fullName)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more