var applesDict: [String: Int] = ["Adam": 3,
"Beth": 5,
"Cal": 3,
"Dan": 5,
"Eve": 4]
a. Set eveAppleCount
equal to the number of apples that Eve has
let eveAppleCount =
Solution
let eveAppleCount = applesDict["Eve"] ?? 0
print(eveAppleCount) // 4
b. Change the number of apples that Adam has to 4
// Your code here
Solution
applesDict["Adam"] = 4
print(applesDict["Adam"] ?? 0) // 4
c. Set calAndDanAppleCount
equal to the sum of Cal
and Dan
let calAndDanAppleCount =
// Expected output: 8
Solution
let calAndDanAppleCount = (applesDict["Cal"] ?? 0) + (applesDict["Dan"] ?? 0)
print(calAndDanAppleCount) // 8
d. Set all the values in applesDict to 0
// Your code here
Solution
applesDict.forEach { applesDict[$0.key] = 0 }
print(applesDict) // ["Cal": 0, "Dan": 0, "Beth": 0, "Adam": 0, "Eve": 0]
var citiesDict: [String: String] = ["Afghanistan": "Kabul",
"Russia": "Moscow",
"Iceland": "Reykjavik"]
a. Set russiaCapital
equal to Russia's capital using citiesDict
let russiaCapital = /* Your code here (Replace "" with your solution)*/ ""
// Your code here
Solution
let russiaCapital = citiesDict["Russia"] ?? ""
print(russiaCapital) // Moscow
b. Add a new key value pair "Jamaica" and its capital "Kingston"
// Your code here
Solution
citiesDict["Jamaica"] = "Kingston"
print(citiesDict)
// ["Russia": "Moscow", "Jamaica": "Kingston", "Iceland": "Reykjavik", "Afghanistan": "Kabul"]
c. Add a new key value pair "Indonesia" and its capital "Jakarta"
// Your code here
Solution
citiesDict["Indonesia"] = "Jakarta"
print(citiesDict)
// ["Russia": "Moscow", "Jamaica": "Kingston", "Indonesia": "Jakarta", "Iceland": "Reykjavik", "Afghanistan": "Kabul"]
a. Create a dictionary that represents the table below listing an authors name and their comprehensibility score.
/*
| Author Name | Score |
| :--: | :--: |
| Mark Twain | 8.9 |
| Nathaniel Hawthorne | 5.1 |
| John Steinbeck | 2.3 |
| C.S. Lewis | 9.9 |
| Jon Krakauer | 6.1 |
*/
var authorScores = [String: Double] ()
/*
Expected outputs:
["Mark Twain": 8.9, "John Steinbeck": 2.3, "Jon Krakauer": 6.1, " C.S. Lewis": 9.9, "Nathaniel Hawthorne": 5.1]
*/
// Your code here
Solution
authorsScores = ["Mark Twain": 8.9, "Nathaniel Hawthorne": 5.1, "John Steinbeck": 2.3, " C.S. Lewis": 9.9, "Jon Krakauer": 6.1]
print(authorsScores)
// ["Mark Twain": 8.9, "John Steinbeck": 2.3, "Jon Krakauer": 6.1, " C.S. Lewis": 9.9, "Nathaniel Hawthorne": 5.1]
b. Add an additional author named “Erik Larson” with an assigned score of 9.2.
Solution
authorsScores["Erik Larson"] = 9.2
print(authorsScores)
// ["Mark Twain": 8.9, "John Steinbeck": 2.3, "Erik Larson": 9.2, "Jon Krakauer": 6.1, " C.S. Lewis": 9.9, "Nathaniel Hawthorne": 5.1]
You are given an array of dictionaries. Each dictionary in the array describes the score of a person. Find the person with the best score and print his full name.
var peopleWithScores: [[String: String]] = [
[
"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"
]
]
var highestScoringName = ""
// Expected output: "Garry Mckenzie"
// Your code here
Solution
var highestScoringName = ""
var highestScore = 0
for scoreDict in peopleWithScores {
if let scoreStr = scoreDict["score"],
let score = Int(scoreStr),
let firstName = scoreDict["firstName"],
let lastName = scoreDict["lastName"] {
if score > highestScore {
highestScore = score
highestScoringName = firstName + " " + lastName
}
}
}
print(highestScoringName) // Garry Mckenzie
Write code below such that cubeDict maps the numbers between 1 and 20 inclusive to their cubes. A number's cube is that number multiplied by itself twice:
2 ^ 3 = 2 * 2 * 2 = 8
var cubeDict = [Int: Int]()
/*
Sample output:
cubeDict.count // 20
cubeDict[2] // 8
cubeDict[3] // 27
cubeDict[20] // 8000
cubeDict[14] // 2744
*/
// Your code here
Solution
var cubeDict = [Int: Int]()
for num in 1...20 {
cubeDict[num] = num * num * num
}
print(cubeDict.count) // 20
print(cubeDict)
// [10: 1000, 3: 27, 7: 343, 12: 1728, 9: 729, 5: 125, 17: 4913, 18: 5832, 19: 6859, 20: 8000, 13: 2197, 8: 512, 6: 216, 1: 1, 4: 64, 16: 4096, 11: 1331, 15: 3375, 14: 2744, 2: 8]
Find the most common letter in the string below. Use a dictionary to generate your solution that maps a character to the number of times it appears in the string. Ignore whitespaces and capitalization.
let myString = "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or knowledge. We've tended to forget that no computer will ever ask a new question."
var frequencyDict = [Character: Int]()
var mostFrequentChar: Character = "?"
// Excpected output: "e"
// Your code here
Solution
let myString = "We're flooding people with information. We need to feed it through a processor. A human must turn information into intelligence or knowledge. We've tended to forget that no computer will ever ask a new question."
var frequencyDict = [Character: Int]()
var mostFrequentChar: Character = "?"
for char in myString {
if !char.isWhitespace && !char.isUppercase {
if let count = frequencyDict[char] {
frequencyDict[char] = count + 1
} else {
frequencyDict[char] = 1
}
}
}
let maxElement = frequencyDict.max { $0.value < $1.value }
mostFrequentChar = maxElement?.key ?? " "
print(mostFrequentChar) // e