case male
case female
}
let name: String
let surname: String
let age: Int
let gender: Gender
}
extension Person: Hashable {
func hash(into hasher: inout Hasher) {
// Выберите реализацию идеальной хеш-функции. Идеальной хеш-функцией называется такая функция, которая отображает ключ из статического множества (предложенного в примере ниже) во множество во множество целых чисел без коллизий.
}
}
let listOfPersons: [Person] = [
.init(name: "Paul", surname: "Brown", age: 23, gender: .male),
.init(name: "John", surname: "Williams", age: 5, gender: .male),
.init(name: "Donna", surname: "Anderson", age: 62, gender: .male),
.init(name: "Jessica", surname: "Richards", age: 17, gender: .male),
.init(name: "Martin", surname: "Scott", age: 33, gender: .male),
.init(name: "Scott", surname: "Martin", age: 33, gender: .female),
.init(name: "Eva", surname: "Peters", age: 17, gender: .female),
.init(name: "Emily", surname: "Gibson", age: 11, gender: .female),
.init(name: "Mia", surname: "Jordan", age: 97, gender: .female),
.init(name: "Sofia", surname: "Grant", age: 8, gender: .female),
.init(name: "Olivia", surname: "Davis", age: 52, gender: .female),
.init(name: "Mia", surname: "Jones", age: 83, gender: .female),
.init(name: "Mia", surname: "Jones", age: 83, gender: .female),
.init(name: "Olivia", surname: "Davis", age: 24, gender: .female)
]
// Вот варианты ответов
// 1
func hash(into hasher: inout Hasher) {
let combined = name.hashValue ^ surname.hashValue
hasher.combine(combined)
}
// 2
func hash2(into hasher: inout Hasher) {
hasher.combine(name)
hasher.combine(surname)
}
func hash3(into hasher: inout Hasher) {
hasher.combine(age)
hasher.combine(gender)
}
// 4
func hash4(into hasher: inout Hasher) {
let combined = name.unicodeScalars.map { Int($0.value) }.reduce(0, +) + age
hasher.combine(combined)
}
// 5
func hash5(into hasher: inout Hasher) {
hasher.combine(Int.random(in: 0...100))
}
// 6
func hash6(into hasher: inout Hasher) {
hasher.combine(name.count)
hasher.combine(surname.count)
hasher.combine(age)
}
// 7
func hash7(into hasher: inout Hasher) {
let combined = name.count + surname.count + age
hasher.combine(combined)
}
кто нибудь знает ответ?
оооо, два гендера...
Обсуждают сегодня