Last active
May 21, 2019 15:47
-
-
Save edmund-h/2581f1f862182b3d68a7a18c21bd62b4 to your computer and use it in GitHub Desktop.
Swift Setup for the HackerRank Environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func multiLineInputSetup() { | |
//Use this for multiple line inputs. The problem for which I generated this had the first term in its sequence as the number of lines of input. If this isn't available, you could just build a while loop that switches on whether a string can be instantiated with readLine() | |
let fileName = ProcessInfo.processInfo.environment["OUTPUT_PATH"]! | |
FileManager.default.createFile(atPath: fileName, contents: nil, attributes: nil) | |
let fileHandle = FileHandle(forWritingAtPath: fileName)! | |
guard let s = readLine(), let count = Int(s) else { fatalError("Bad input") } | |
var output = "" | |
for _ in 1...count { | |
guard let s = readLine() else {continue} | |
let result = run(s) | |
if let result = result { output.append(result) } | |
} | |
fileHandle.write(output.data(using: .utf8)!) | |
} | |
func singleLineInputSetup() { | |
//Alternatively if there's just a single line of input, use this setup | |
let fileName = ProcessInfo.processInfo.environment["OUTPUT_PATH"]! | |
FileManager.default.createFile(atPath: fileName, contents: nil, attributes: nil) | |
let fileHandle = FileHandle(forWritingAtPath: fileName)! | |
guard let s = readLine() else { fatalError("Bad input") } | |
let output = run(input: s) | |
fileHandle.write(output.data(using: .utf8)!) | |
} | |
func run(input: String)-> String { | |
//Be sure to copy this function in either case and have any output that needs to be written to STDOUT returned as a String | |
var output = "" | |
//Do the thing here | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment