Chapter 25, Silver challenge

Hello my friends!
Did i finish silver challenge correctly ?

extension Stack: Equatable where Element: Equatable {
  static func == (lhs: Stack<Element>, rhs: Stack<Element>) -> Bool {
    return lhs.items.count == rhs.items.count && lhs.items.enumerated().first(where: { $1 != rhs.items[$0] }) == nil
  }
}

let myTasks = Stack(items: ["Zelim", "Zurab"])
let yourTasks = Stack(items: ["Zelim"])

myTasks == yourTasks // false

class Pancake { }
let breakfast = Stack(items: [Pancake(), Pancake()])
let lunch = Stack(items: [Pancake()])
breakfast == lunch // operator function '==' requires that 'Pancake' conform to 'Equatable'


Hello @mgmzlm ,

It seems to be right, even if I didn’t have to implement the == operator. In fact, after writing the first line of your code then the end brace, the compiling error disappears. Then it simply works… :upside_down_face:
Bye

1 Like

Here’s my solution

extension Stack where Element: Equatable {
    static func == (lhs: Stack<Element>, rhs: Stack<Element>) -> Bool {
        return (lhs.items == rhs.items)
    }
}
1 Like