-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextractPinCode.swift
More file actions
41 lines (33 loc) · 928 Bytes
/
extractPinCode.swift
File metadata and controls
41 lines (33 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// https://codefights.com/challenge/jopxFifZN3GPjG856
// 3000
let extractPinCode: [Double] -> [Int] = {
var r = [Int](), x = $0, k = x.count-1, i = 0, a = 7 - x[k-1]
while i < k-2 {
let n = Int(x[i]+a) / 2 * 3 + Int(x[i+1]+3-x[k]) / 2
i += 2
if n > 11 {
r.removeLast()
} else {
r += [n%11]
}
}
return r
}
/*
let extractPinCode: [Double] -> [Int] = {
var r = [Int](),
x = $0.enumerate().flatMap { $0 % 2 > 0 ? $1 : nil },
y = $0.enumerate().flatMap { $0 % 2 < 1 ? $1 : nil }
// last y should be max, last x should be min
// convert (lastX, lastY) to (1.5, 7.5)
for i in 0..<x.count-1 {
var n = (Int(y[i]+8-y.last!) - 1) / 2 * 3 + (Int(x[i]-x.last!) + 3) / 2
if n > 11 {
r.removeLast()
} else {
r += n < 10 ? [n] : [0]
}
}
return r
}
*/