I'm working on some HackerRank Problem Solving challenges. The language I am using right now is Swift. The objective is to calculate the diagonal difference of a square array with n columns and rows. The logic for the exercise is quite simple, and I've worked it out without any hiccups, the problem, however, is a Runtime Error, which occurs on running the program, ~ no response on stdout
. Here is a screenshot of the error:
And here is my code (the diagonalDifference()
function):
func diagonalDifference(arr: [[Int]], n: Int) -> Int {
// Write your code here
var primarySum = 0
var secondarySum = 0
for i in 0..<n {
primarySum += arr[i][i]
secondarySum += arr[i][n - (i - 1)]
}
var diagDifference = abs(primarySum - secondarySum)
return diagDifference
}
One other thing I've noticed in my time doing these challenges is that when I look up solutions in Swift in the discussions tab, most people don't include n
as a parameter in the function. However, when I don't includen
as a parameter, I get a runtime error.
Here is the rest of the code for reference:
import Foundation
/*
* Complete the 'diagonalDifference' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
func diagonalDifference(arr: [[Int]], n: Int) -> Int {
// Write your code here
var primarySum = 0
var secondarySum = 0
for i in 0..<n {
primarySum += arr[i][i]
secondarySum += arr[i][n - (i - 1)]
}
var diagDifference = abs(primarySum - secondarySum)
return diagDifference
}
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") }
var arr = [[Int]]()
for _ in 1...n {
guard let arrRowTemp = readLine()?.replacingOccurrences(of: "\\s+$", with: "", options: .regularExpression) else { fatalError("Bad input") }
let arrRow: [Int] = arrRowTemp.split(separator: " ").map {
if let arrItem = Int($0) {
return arrItem
} else { fatalError("Bad input") }
}
guard arrRow.count == n else { fatalError("Bad input") }
arr.append(arrRow)
}
guard arr.count == n else { fatalError("Bad input") }
let result = diagonalDifference(arr: arr, n: n)
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
Can someone tell me what I should do about this, my function should return diagDifference
. I'm somewhat comfortable with Swift, but there are still some parts of the code which I'm not totally clear about, such as:
fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
My understanding of this is that the code writes a file using the call of the diagonalDifference()
function. However, when I looked this up in the Swift documentation, I found that the write()
method was deprecated.
Not considering this question specifically, if you could suggest any improvements to my code or logic in general, please do so.
You have an array out of bounds error on this line. Think about what happens when i
is 0
:
secondarySum += arr[i][n - (i - 1)]
Change it to:
secondarySum += arr[i][n - i - 1]