I'm taking a course with a hands-on question on Swift programming, that redirect me to Hackerrank
Write a function named printMessage that takes two parameters - a string message and an integer count. The message should print and repeat the message as specified in the count parameter.
Message:"Hello , How are You"
For instance take Count as 8
This should print Message:"Hello , How are You" 8 times consecutively.
The problem is when I submit my code always said Wrong answer, then I tried with custom input, and it throws me no response on STDOUT. Anyone knows what's wrong?
import Foundation
func printMessage(message: String, count: Int) {
for _ in 0..<count {
print(message)
}
}
let message: String = readLine()!
let count: Int = Int(readLine()!)!
printMessage(message: message, count: count)
I have run into this before, the way I solved it was saving my code somewhere and resetting to the boilerplate code. So just reset to the boilerplate and copy your function back in. On many swift hackerrank problems they have something like the following:
func myFunc(param: [Int]) -> [Int] {
/*
* Write your code here.
*/
}
// The following is an example of your function being written to stdout
let fileName = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: fileName, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: fileName)!
let result = myFunc(param: input)
fileHandle.write(result.map{ String($0) }.joined(separator: "\n").data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
The code after the function is what writes to stdout
Hackerrank in some swift exercises fires that message whenever you have a buffer overflow, instead of warning you about it