Question on Gold Challenge


#1

The hint on the Gold Challenge states “you will need to change the return type form [Int] to an array of associated type of the of the CollectionType protocol.” I am having trouble understanding how this will still meet the requirements of the findAll(::slight_smile: function. Here is what I did. What am I missing?

[code]func findAll<T: Equatable, C: CollectionType where C.Generator.Element == T>(items: C, _ itemToFind: T) -> [Int] {
var result = Int

for (index, item) in items.enumerate() {
    if item == itemToFind {
        result.append(index)
    }
}

return result

}[/code]

I am really enjoying the book. Your hard work creating this is appreciated!


#2

Maybe he wants us to turn the output into a generic CollectionType, but every thing I have tried gives a raft of errors.


#3

This gold challenge is driving me crazy:

Here is what I have thus far:

func findAll<C:Collection, T:Equatable>(_ items: C, _ value: T) -> [Int] where C.Iterator.Element == T {
    var results = [Int]()

    for (index, element) in items.enumerated() {
        if element == value {
            results.append(index)
        }
    }
    return results
}
var results = [Int]()
results = findAll([5,3,7,3,9], 3)

I am totally confused when they say:

You will need to change the return type from [Int] to an array of an associated type of the Collection protocol.

So I change the return type to a generic Sequence like so:

func findAll<C:Collection, T:Equatable, R:Sequence>(_ items: C, _ value: T) -> [R] where C.Iterator.Element == T {
    var results = [R]()

    for (index, element) in items.enumerated() {
        if element == value {
            results.append(index)
        }
    }
    return results
}
var results = [Int]()
results = findAll([5,3,7,3,9], 3)

Gives the following error:

cannot invoke append with an argument list of type (int)

Anybody else get this to work?


#4

I think something got garbled in the dining process. The hint just looks wrong to me. First, because how can you return the locations in all possible Equatable Collections? And second because it works just fine without changing the return type.


#5

dining = editing - see how things can get garbled? :slight_smile: