Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission for mod 2 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,33 @@ var animalArray: [String] = ["Lion", "Zebra", "Elephant", "Turtle"]
var optionalArray: [Int]?

// [2] Is there a logical error with accessing index `4` in a 4-element array?
print("4th element of animalArray: \(animalArray[4])")
print("4th element of animalArray: \(animalArray[3])")

// [3] The operation below is quite dangerous. How can we make it safer?
let unwrappedArray = optionalArray!
print(unwrappedArray)
if let unwrappedArray = optionalArray {
print(unwrappedArray)
} else {
print("optionalArray is nil")
}


// [4] Initialize optionalArray
/* BEGIN CODE */

optionalArray = []
/* END CODE */

// [5, 6, 7] Add elements to optionalArray
/* BEGIN CODE */

optionalArray?.append(10)
optionalArray?.append(contentsOf: [6, 7, 8, 9])
optionalArray?.insert(55, at: 2)
/* END CODE */

// [8] Print the 5th element of optionalArray
/* BEGIN CODE */

if let optionalArray = optionalArray, optionalArray.count >= 5 {
print("5th element of optionalArray: \(optionalArray[4])")
} else {
print("optionalArray doesn't have a 5th element")
}
/* END CODE */
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ print("Alphabetical: \(cities)\n")
// [1] Use the .sort(by:) function to sort the array from shortest name to
// longest. Check the docs for reference material if needed
/* BEGIN CODE */

cities.sort(by: { $0.count < $1.count })
/* END CODE */
print("Length (.sort): \(cities)")

Expand All @@ -264,10 +264,21 @@ for i in 0 to (arr_length - 1):
*/
func selectionSort(_ input: [String]) -> [String] {
var arr: [String] = input
/* BEGIN CODE */

/* END CODE */
for i in 0..<arr.count {
var minIndex = i
for j in (i + 1)..<arr.count {
if arr[j].count < arr[minIndex].count {
minIndex = j
}
}
if i != minIndex {
let temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
}
}
return arr
}

cities2 = selectionSort(cities2)
print("Length (sel): \(cities2)")
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ var numArr: [Int] = Array(repeating: 0, count: 5)

// [1] Rewrite this code to use a loop
/* BEGIN CODE */
numArr[0] = 10
numArr[1] = 20
numArr[2] = 30
numArr[3] = 40
numArr[4] = 50
for i in 0..<numArr.count {
numArr[i] = (i + 1) * 10
}
/* END CODE */
print("numArr: \(numArr)")
// <-- [RUN HERE] The output should be unchanged
Expand All @@ -161,7 +159,9 @@ var messages: [String: String] = [
// Reference the docs for the desired output format.
print("\nMessage Data:")
/* BEGIN CODE */

for (id, message) in messages {
print("\(id): \(message)")
}
/* END CODE */
// <-- [RUN HERE] Does this seem like the order of a normal conversation?
// Perhaps we're missing some data about which messages
Expand All @@ -184,27 +184,50 @@ print("Goodbye!")
// Refer to the docs for tips & tricks.
print("\n(WHILE) MESSAGES: ")
/* BEGIN CODE */

var index = 0
while index < messageOrder.count {
let id = messageOrder[index]
if let message = messages[id] {
print(message)
}
index += 1
}
/* END CODE */
// <-- [RUN HERE] Check if your output matches the example.

// [4] Do the same thing, but with a repeat-while loop
print("\n(REPEAT-WHILE) MESSAGES: ")
/* BEGIN CODE */

index = 0
repeat {
let id = messageOrder[index]
if let message = messages[id] {
print(message)
}
index += 1
} while index < messageOrder.count
/* END CODE */
// <-- [RUN HERE] Check if your output matches the example.

// [5] Do the same thing, but with a for loop using default iterator
print("\n(FOR ITERATOR) MESSAGES: ")
/* BEGIN CODE */

for id in messageOrder {
if let message = messages[id] {
print(message)
}
}
/* END CODE */
// <-- [RUN HERE] Check if your output matches the example.

// [6] Do the same thing, but with a for loop using a range
print("\n(FOR RANGE) MESSAGES: ")
/* BEGIN CODE */

for i in 0..<messageOrder.count {
let id = messageOrder[i]
if let message = messages[id] {
print(message)
}
}
/* END CODE */
// <-- [RUN HERE] Check if your output matches the example.
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,60 @@ if !mySet.contains("Zebra") {

// [2] Declare an optional String-Set, but don't initialize it
/* BEGIN CODE */

var mySet: Set<String> = ["Zebra"]
if !mySet.contains("Zebra") {
print("--> FAILURE: mySet does not contain Zebra")
} else if !mySet.isSubset(of: animalSet) {
print("--> FAILURE: mySet is not a subset of animalSet")
} else {
print("SUCCESS: mySet contains Zebra and is a subset of animalSet")
}
/* END CODE */

var optionalSet: Set<String>?
// [3] Insert "Giraffe" into optionalSet.
/* BEGIN CODE */

optionalSet?.insert("Giraffe")
/* END CODE */

// [4] Check if optionalSet contains "Giraffe"
/* BEGIN CODE */

if let set = optionalSet, set.contains("Giraffe") {
print("optionalSet contains Giraffe")
} else {
print("optionalSet does not contain Giraffe")
}
/* END CODE */
// <-- [RUN HERE] optionalSet should NOT contain Giraffe at this point

// [5] Initialize optionalSet with "Cow", "Elephant"
// (can be done in one line)
/* BEGIN CODE */

optionalSet = ["Cow", "Elephant"]
/* END CODE */

// [6] Insert "Giraffe" into optionalSet again
/* BEGIN CODE */

optionalSet?.insert("Giraffe")
/* END CODE */

// [7] Check if optionalSet contains "Giraffe"
/* BEGIN CODE */

if let set = optionalSet, set.contains("Giraffe") {
print("optionalSet contains Giraffe")
} else {
print("optionalSet does not contain Giraffe")
}
print("optionalSet size: \(optionalSet?.count ?? 0)")
/* END CODE */
// <-- [RUN HERE] optionalSet SHOULD contain Giraffe at this point

print("optionalSet size: \(optionalSet?.count)")

// [8] Insert "Giraffe" into optionalSet again
/* BEGIN CODE */
optionalSet?.insert("Giraffe")

print("optionalSet size: \(optionalSet?.count ?? 0)")
/* END CODE */

print("optionalSet size: \(optionalSet?.count)")
Expand Down