Skip to content

Instantly share code, notes, and snippets.

@JaDenis
Created July 26, 2018 11:25
Show Gist options
  • Save JaDenis/d1a6d00ce0e1c51825128e5c78f86e53 to your computer and use it in GitHub Desktop.
Save JaDenis/d1a6d00ce0e1c51825128e5c78f86e53 to your computer and use it in GitHub Desktop.
lol
func calcWinningPair(odds: [[String: Any]]) -> [Float : [[String : Any]]] {
if !odds.isEmpty {
var handicapMap: [Float: Any] = [:]
var absSpecialValues: [Float] = []
// print(odds)
odds.forEach {
let specialValue = $0["specialValue"] as! String
if let floatSpecialValue = Float(specialValue) {
let absSpecValue = abs(floatSpecialValue)
absSpecialValues.append(absSpecValue)
absSpecialValues = absSpecialValues.removeDuplicates()
}
}
// print("absSpecialValues: \(absSpecialValues)")
// 1). составить мапу
for specialValue in absSpecialValues {
var sortedBySpecialValueArray: [[String: Any]] = []
odds.forEach {
if let floatSpecialValue = Float($0["specialValue"] as! String) {
let absSpecValue = abs(floatSpecialValue)
if absSpecValue == specialValue {
sortedBySpecialValueArray.append($0)
}
}
}
handicapMap[specialValue] = sortedBySpecialValueArray
}
// for sv in absSpecialValues {
// print("=====================")
// print("\(sv): \(handicapMap[sv]!)")
// print("=====================")
// }
// 2). перебрать мапу так, что остануться только пары
// - заглянуть в каждый value
// - выбрать там пару
// - записать в handicapMap
for specialValue in absSpecialValues {
let odds = handicapMap[specialValue]! as! [[String: Any]]
// из odds -> составить мапу по одинаковым specialValue - получим пары
var svMap: [Float: Any] = [:]
var svArray: [Float] = []
// собираем массив всех specialValue
odds.forEach {
let specialValue = $0["specialValue"] as! String
if let floatSpecialValue = Float(specialValue) {
svArray.append(floatSpecialValue)
}
}
svArray = svArray.removeDuplicates()
// print(svArray)
// составляем мапу [sv : odds]
for sv in svArray {
var pair: [[String: Any]] = []
odds.forEach {
let specialValue = $0["specialValue"] as! String
if let floatSpecialValue = Float(specialValue) {
if sv == floatSpecialValue {
pair.append($0)
}
}
}
let pairSortedByOutcome = pair.sorted {
if let outCome1 = Float($0["outCome"] as! String),
let outCome2 = Float($1["outCome"] as! String) {
return outCome1 < outCome2
} else {
return $0["outCome"] as! String > $1["outCome"] as! String
}
}
svMap[sv] = pairSortedByOutcome
}
// for sv in svArray {
// print("=====================")
// print("\(sv): \(svMap[sv]!)")
// print("=====================")
// }
// сначала сортируем по diff
var diffMap: [Float: Any] = [:]
for sv in svArray {
if let pair = svMap[sv] as? [[String: Any]] {
var coeffDiff: Float = 0.0
var coeffArray: [Float] = []
for odd in pair {
if let coeff = odd["coefficient"] as? String {
if let floatCoeff = Float(coeff) {
coeffArray.append(floatCoeff)
}
} else {
// добавляем коэффициент 0.0, если с бэкенда пришел <null>
coeffArray.append(0.0)
}
}
if coeffArray.count > 1 {
coeffDiff = abs(coeffArray[0] - coeffArray[1])
// составляем мапу диффов
diffMap[sv] = coeffDiff
} else {
print("пизда")
}
}
}
// print("======= diffMap ========")
// print(diffMap)
// потом сортируем по diff18
// составить мапу sv: diff18
var diff18Map: [Float: Any] = [:]
for sv in svArray {
if let pair = handicapMap[specialValue] as? [[String: Any]] {
var coeffArray: [Float] = []
for odd in pair {
if let coeff = odd["coefficient"] as? String {
if let floatCoeff = Float(coeff) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if coeffArray.count > 1 {
let diff18_1 = abs(coeffArray[0] - 1.8)
let diff18_2 = abs(coeffArray[1] - 1.8)
let diff18 = (diff18_1 + diff18_2) / 2.0
diff18Map[sv] = diff18
}
}
}
// print("======= diff18Map ========")
// print(diff18Map)
// сортировать массив svArray по diff
let svArraySortedByDiff = svArray.sorted {
let diff_1 = diffMap[$0] as! Float
let diff_2 = diffMap[$1] as! Float
return diff_1 < diff_2
}
// print(svArraySortedByDiff)
// сортировать массив svArray по diff18
let svArraySortedByDiff18 = svArray.sorted {
let diff18_1 = diff18Map[$0] as! Float
let diff18_2 = diff18Map[$1] as! Float
return diff18_1 < diff18_2
}
// print(svArraySortedByDiff18)
// взять первый итем - это sv выигравшей пары
let svForWinningPair = svArraySortedByDiff18.first!
// перезаписываем в handicapMap выигравшую пару
// записать ее с этим abs(sv) в handicapMap
handicapMap[abs(svForWinningPair)] = svMap[svForWinningPair]
}
// 3). сортируем финальную мапу
// - собираем handicapDiffMap
// - собираем handicapDiff18Map
// -
// for specialValue in absSpecialValues {
// print("=====================")
// print("\(specialValue): \(handicapMap[specialValue]!)")
// print("=====================")
// }
var handicapDiffMap: [Float: Any] = [:]
for specialValue in absSpecialValues {
if let pair = handicapMap[specialValue] as? [[String: Any]] {
var coeffDiff: Float = 0.0
var coeffArray: [Float] = []
for odd in pair {
if let coeff = odd["coefficient"] as? String {
if let floatCoeff = Float(coeff) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if coeffArray.count > 1 {
coeffDiff = abs(coeffArray[0] - coeffArray[1])
handicapDiffMap[specialValue] = coeffDiff
}
}
}
// print("======= diffMap ========")
// print(handicapDiffMap)
var handicapDiff18Map: [Float: Any] = [:]
for specialValue in absSpecialValues {
if let pair = handicapMap[specialValue] as? [[String: Any]] {
var coeffArray: [Float] = []
for odd in pair {
if let coeff = odd["coefficient"] as? String {
if let floatCoeff = Float(coeff) {
coeffArray.append(floatCoeff)
}
} else {
coeffArray.append(0.0)
}
}
if coeffArray.count > 1 {
let diff18_1 = abs(coeffArray[0] - 1.8)
let diff18_2 = abs(coeffArray[1] - 1.8)
let diff18 = (diff18_1 + diff18_2) / 2
handicapDiff18Map[specialValue] = diff18
}
}
}
// print("======= diff18Map ========")
// print(handicapDiff18Map)
// сортируем массив absSpecialValue по diff
let absSpecialValuesSortedByDiff = absSpecialValues.sorted {
let diff_1 = handicapDiffMap[$0] as! Float
let diff_2 = handicapDiffMap[$1] as! Float
return diff_1 < diff_2
}
// print(absSpecialValuesSortedByDiff)
// сортируем массив absSpecialValue по diff18
let absSpecialValuesSortedByDiff18 = absSpecialValues.sorted {
let diff18_1 = handicapDiff18Map[$0] as! Float
let diff18_2 = handicapDiff18Map[$1] as! Float
return diff18_1 < diff18_2
}
// print(absSpecialValuesSortedByDiff18)
// взять первый итем - это sv выигравшей пары
let svForWinningPair = absSpecialValuesSortedByDiff18.first!
let winningPair = handicapMap[svForWinningPair] as! [[String: Any]]
let handicap = [svForWinningPair : winningPair]
return handicap
} else {
return [-1.0: [["coefficient": "-",
"outCome": 1],
["coefficient": "-",
"outCome": 2]]]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment