-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
103 lines (79 loc) · 2.98 KB
/
Contents.swift
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import UIKit
// Question review from Closures lab
// Question Four
// Write a function called sortedNamesByLastName(in:) that takes in an array of tuples
// of type (String, String) and returns an array of tuples sorted by last name.
// Your function here
// input of our function: unsorted last name array of tuples
// output of our function: sorted by last name array of tuples
func sortedNamesByLastName(in names: [(String, String)]) -> [(String, String)] {
let result = names.sorted { name1, name2 in
// < means ascending, or a....z
// > means descending, or z....a
name1.1 < name2.1
}
return result
}
// Uncomment out the following lines to check your solution
let firstAndLastTuples = [
("Johann S.", "Bach"),
("Claudio", "Monteverdi"),
("Duke", "Ellington"),
("W. A.", "Mozart"),
("Nicolai","Rimsky-Korsakov"),
("Scott","Joplin"),
("Josquin","Des Prez")
]
let expectedOutputFour = [
("Johann S.", "Bach"),
("Josquin","Des Prez"),
("Duke", "Ellington"),
("Scott","Joplin"),
("Claudio", "Monteverdi"),
("W. A.", "Mozart"),
("Nicolai","Rimsky-Korsakov")
]
print(sortedNamesByLastName(in: firstAndLastTuples))
let outputFour = sortedNamesByLastName(in: firstAndLastTuples)
assert(outputFour.elementsEqual(expectedOutputFour, by: { $0 == $1 }), "Expected output to be \(expectedOutputFour), but found \(outputFour)")
let arr = [4, 5, 1, -7, -99, 56, 23]
// return an array where elements are less than 5
// output [-99, -7, 1, 4]
// using sorted() which does not take any arguments
print(arr.filter { $0 < 5 }.sorted())
let filteredArr = arr.filter { $0 < 5 }
print(filteredArr) // [4, 1, -7, -99]
let sortedArr = filteredArr.sorted
// sorted using closure which takes 2 arguments
let names = ["Alex", "Tiffany", "Antonio", "Genesis"]
// > descending z...->a and < ascending from a....->z
let sortedNamesUsingClosure = names.sorted { $0 > $1 }
print(sortedNamesUsingClosure)
// given an array of names return all the names uppercased
// input: ["Alex", "Tiffany", "Antonio", "Genesis"]
// output: ["ALEX", "TIFFANY", "ANTONIO", "GENESIS"]
// use map, filter or reduce to solve exercise
let sortedNamesUsingMap = names.map { $0.uppercased()}
print(sortedNamesUsingMap)
// re-creating the built-in map function
// map function will take 2 paramters,
// first parameter is an array on ints
// second parameter is a closure
func customMap(arr: [Int], closure: (Int) -> Int) -> [Int] {
var transformedArr = [Int]()
for num in arr {
// perform transformation based on closure and append result in
// transformedArr
transformedArr.append(closure(num))
}
return transformedArr
}
// using customMap function above take in an array of numbers and return
// the squared values of each of those elements
// input: [1, 2, 3, 4, 5, 6, 7]
// output: [1, 4, 9, 16, 25, 36, 49]
let numberArray = [1, 2, 3, 4, 5, 6, 7]
let squaredNumberArray = customMap(arr: numberArray) { number in
number * number
}
print(squaredNumberArray)