forked from lunchScreen/Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubarray Division 1.swift
50 lines (38 loc) · 1.56 KB
/
Subarray Division 1.swift
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
42
43
44
45
46
47
48
49
50
import Foundation
/*
* Complete the 'birthday' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY s
* 2. INTEGER d
* 3. INTEGER m
*/
func birthday(s: [Int], d: Int, m: Int) -> Int {
var count = 0
for index in 0...s.count - m {
if s[index..<index+m].reduce(0, +) == d { count += 1 }
}
return count
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
guard let sTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let s: [Int] = sTemp.split(separator: " ").map {
if let sItem = Int($0) {
return sItem
} else { fatalError("Bad input") }
}
guard s.count == n else { fatalError("Bad input") }
guard let firstMultipleInputTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let firstMultipleInput = firstMultipleInputTemp.split(separator: " ").map{ String($0) }
guard let d = Int(firstMultipleInput[0])
else { fatalError("Bad input") }
guard let m = Int(firstMultipleInput[1])
else { fatalError("Bad input") }
let result = birthday(s: s, d: d, m: m)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)