-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
63 lines (46 loc) · 1.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
import Foundation
// Questions Review: Sets
// Questions Two
// Create a new array scoresThatAppearOnce that has all the elements from scores that appear exactly once.
// It should be in the same order as the original.
let scores = [1, 77, 83, 32, 77, 77, 83, 32, 99]
var scoresThatAppearOnce = [Int]()
// Your code here
var visitedScores: Set<Int> = []
for (_, score) in scores.enumerated() {
if !visitedScores.contains(score) { // haven't seen this score
visitedScores.insert(score)
scoresThatAppearOnce.append(score)
} else { // visited this value before, so it's not unique
if let foundIndex = scoresThatAppearOnce.firstIndex(of: score) {
scoresThatAppearOnce.remove(at: foundIndex)
}
}
}
// runtime efficiency of the algorithm
// contains on a Set in constant time O(1)
// contains on an Array is linear time O(n)
assert(scoresThatAppearOnce == [1, 99], "Was expecting [1, 99], but got \(scoresThatAppearOnce)")
// Question Five
// Determine if a String is a pangram. A pangram is a string that contains every letter of
// the alphabet at least once.
// e.g "The quick brown fox jumps over the lazy dog" is a pangram
// e.g "The quick brown fox jumped over the lazy dog" is NOT a pangram
let strOne = "The quick brown fox jumps over the lazy dog"
let strTwo = "The quick brown fox jumped over the lazy dog"
let strThree = "Sphinx of black quartz, judge my vow"
var strOneIsPangram: Bool = false
var strTwoIsPangram: Bool = false
var strThreeIsPangram: Bool = false
// Your code here
let alphabets: Set<Character> = Set("abcdefghijklmnopqrstuvwxyz")
var trimmedStr = ""
for char in strOne.lowercased() {
if alphabets.contains(char) {
trimmedStr += String(char)
}
}
strOneIsPangram = Set(trimmedStr) == alphabets
assert(strOneIsPangram == true, "Was expecting true, but got \(strOneIsPangram)")
//assert(strTwoIsPangram == false, "Was expecting false, but got \(strTwoIsPangram)")
//assert(strThreeIsPangram == true, "Was expecting true, but got \(strThreeIsPangram)")