Silver Challenge


#1
var toDoList = ["Take out garbage", "Pay bills", "Cross of finished items"]

var convenientWay:[String] = toDoList.reverse()
var toDoListCount = toDoList.count

// Reverse toDoList elements within a Loop
for var i = 0; i < toDoListCount; i++ {
    
    toDoList.insert(toDoList.removeLast(), atIndex: i)
    
}

print(toDoList)
print(convenientWay)

#2

Now that the C style for loop has been deprecated and will ultimately be removed in Swift 3, here’s its replacement:

for item in toDoList { toDoList.insert(item, atIndex: 0) }


#3

Didn’t know they have plans to remove that older style syntax. Thanks for the heads up


#4

How about:

import Cocoa var toDoList = ["Take out garbage", "Pay bills", "Cross off finished items"] toDoList = toDoList.reverse()

But, how would one know to do that (i.e., use ‘reverse()’), based on the documentation for Array?
Is the answer to this question that ‘Array’ conforms to ‘CollectionType’ (according to the first few lines of 'Array’s documentation), and one of the methods of ‘CollectionType’ is ‘reverse()’?


#5

[quote=“fminich”]How about:

import Cocoa var toDoList = ["Take out garbage", "Pay bills", "Cross off finished items"] toDoList = toDoList.reverse()

But, how would one know to do that (i.e., use ‘reverse()’), based on the documentation for Array?
Is the answer to this question that ‘Array’ conforms to ‘CollectionType’ (according to the first few lines of 'Array’s documentation), and one of the methods of ‘CollectionType’ is ‘reverse()’?[/quote]

Yeah… I didn’t know about the reverse method… I think I just stumbled on it in this forum. But seems pretty handy. Also, there isn’t one answer to any one question. Basically, there are multiple ways/solutions to doing the same thing.


#6

Hi, my solution is:

var secondList:[String] = [“Take out garbage”, “Pay bills”, “Cross off finished items”]

var reversedSecondList = String

for item in (secondList.count - 1).stride(through: 0, by: -1) {
reversedSecondList.append(secondList[item])
}