Bronze, Silver & Gold Challenges

My Gold approach:

var bucketList = ["Climb Mt. Everest"]
var newItems = [
    "Fly hot air balloon to Fiji",
    "Watch the Lord of the Rings trilogy in one day",
    "Go on a walkabout",
    "Scuba dive in the Great Blue Hole",
    "Find a triple rainbow"
]

for item in newItems {
    bucketList.append(item)
}
bucketList.remove(at: 2)
bucketList
print(bucketList.count)
print(bucketList[0...2])
bucketList[2] += " in Australia"

var found: Int?
found = bucketList.index(of: "Fly hot air balloon to Fiji")

if let fijiIndex = found {
    print("The Fiji entry is at Array index position \(fijiIndex).")
    print("The array element that is two positions after the Fiji entry is: \(bucketList[fijiIndex + 2])")
}

Iā€™m a bit confused about your silver challenge using the for-in loop.

The way I interpreted it (obviously wrong):

  • set an empty array containing string values to the variable ā€œreversedArrayā€
  • for elements in ā€œtoDoListā€ take the empty ā€œreversedArrayā€ and insert items (from toDoList) starting at 0 index and so on
  • reversedArray (the way I see itā€¦again wrong) prints the exact same string as toDoList

Basically, could you explain how the function reversedArray.insert(item, at: 0) actually reverses the list?

Not a problem! I think the part youā€™re getting confused on is the insert method. Letā€™s walk through the following example:

let numbers = [1,2,3]

for number in numbers {
    print(number)
}

So when you run that, it will print out 1,2,3. So the reversing magic is happening with the insert method. Consider the following:

let numbers = [1,2,3]
var reversedNumbers = [Int]()

for number in numbers {
    reversedNumbers.insert(number, at: 0)
    print(reversedNumbers)
    
}

When this runs youā€™ll see:
[1]
[2,1]
[3,2,1]

So when you use reversedNumbers.insert(number, at: 0) it means: "Iā€™m going to insert this number at location 0, and the element that was 0, and everything else will be shifted to the right.

Let me know if that helps, or if you need further clarification!

Thanks a lot I understand now.

Iterating through the for-in loop,

1st time- insert 1 at index 0 into the empty reversedNumbers array
2nd time- insert 2 at index 0 (shifting 1 to the right)
3rd time- insert 3 at index 0 (shifting 2 and 1 to the right)

1 Like

Here is my solution to the Gold Challenge:

var bucketList = ["Take out garbage", "Fly hot air balloon to Fiji", "Pay bills", "Cross off finished items"]

if let foundIndex = bucketList.index(of: "Fly hot air balloon to Fiji"),
  let twoPositionLaterIndex = bucketList.index(foundIndex, offsetBy: 2, limitedBy: bucketList.endIndex.advanced(by: -1)) {
  print(bucketList[twoPositionLaterIndex])
}

For instanceļ¼ŒThere are many elements in an arrayļ¼ŒHow do we know where it is in the first placeļ¼ŒLetting the program find out for itself is the best way

var bucketList = ["Climb Hill",
              "Watch the lord of the Rings trilogy in one day",
              "Go on a walkabout",
              "Scuba dive in great Blue Hole",
              "find a triple rainbow",
              "fly hot air balloon to Fiji",
              ];

let index = bucketList.index(of :ā€œfly hot air balloon to Fijiā€);
print(index!);

Hereā€™s what I came up withā€¦

//Bronze
var toDoList = ["Take out garbage", "Pay Bills", "Cross off finished items"]
var reverseToDoList = [String]()
for i in toDoList {
    reverseToDoList.insert(i, at: 0)
}
print(reverseToDoList)

//Silver
toDoList.reverse()
print(toDoList)

//Gold
let findIndex = bucketList.firstIndex(of: "Fly hot air balloon to Fiji")
print(findIndex!)
let indexPlusTwo = findIndex! + 2
print(bucketList[indexPlusTwo])
1 Like

Here is my solution using nil coalescing operator:

var unwrappedIndex = bucketList.firstIndex(of: "Fly hot air balloon to Fiji") ?? 0
var nextIndex = unwrappedIndex + 2
let stringAtLocation = bucketList[nextIndex]

var todoList = ["Go to hyborian kingdom", "Meet Valeria", "Meet Conan", "Go to Atlantis", "Go to Stygia", "Meet Soloman", "Meet Kull"]

Bronze -

if todoList.count > 0
{
    print("not empty")
}
else
{
    print("empty")
}

Silver

for i in stride(from: toDoList.count - 1, through: 0, by: -1)

{

    print(toDoList[i])

}

Gold

let i: Int? = toDoList.firstIndex(of: "Meet Valeria")

if let tour = i

{

    let newcode = toDoList.index(tour, offsetBy: 2)

    toDoList[newcode]

}

@averagedude2992 - beautiful solution to reversing the array :clap:

//Silver
for i in toDolist.reverse(){
print(i)
}