Skip to content

Instantly share code, notes, and snippets.

@byaruhaf
Created June 22, 2020 06:07
Show Gist options
  • Save byaruhaf/6b7f80f0f0de3d727375269665b1605a to your computer and use it in GitHub Desktop.
Save byaruhaf/6b7f80f0f0de3d727375269665b1605a to your computer and use it in GitHub Desktop.
class Person {
var id: Int
var items: [String: Float]
init(id: Int, items: [String: Float]) {
self.id = id
self.items = items
}
}
var person1 = Person(id: 1, items: [:])
var person2 = Person(id: 2, items: [:])
var compatibilityItems = ["Cats", "Dogs", "Chamois"]
var currentPerson: Person?
currentPerson = person1
currentPerson?.items.updateValue(1, forKey: "Cats")
currentPerson?.items.updateValue(1, forKey: "Dogs")
currentPerson?.items.updateValue(1, forKey: "Chamois")
print(person1.items)
currentPerson = person2
currentPerson?.items.updateValue(1, forKey: "Cats")
currentPerson?.items.updateValue(1, forKey: "Dogs")
currentPerson?.items.updateValue(1, forKey: "Chamois")
print(person2.items)
// If diff 0.0 is 100% and 5.0 is 0%, calculate match percentage
var percentagesForAllItems: [Double] = []
for (key, person1Rating) in person1.items {
let person2Rating = person2.items[key] ?? 0
let difference = abs(person1Rating - person2Rating)/5.0
percentagesForAllItems.append(Double(difference))
}
print(percentagesForAllItems)
let sumOfAllPercentages = percentagesForAllItems.reduce(0, +)
let matchPercentage = sumOfAllPercentages/Double(compatibilityItems.count)
// print(matchPercentage, "%")
let matchString = 100 - (matchPercentage * 100).rounded()
print("\(matchString)%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment