Skip to content

Instantly share code, notes, and snippets.

@verebes1
Last active June 13, 2022 09:36
Show Gist options
  • Save verebes1/e09cb29a50755191ba44e9d3c915ba04 to your computer and use it in GitHub Desktop.
Save verebes1/e09cb29a50755191ba44e9d3c915ba04 to your computer and use it in GitHub Desktop.
HackerRank Input Reading Arrays of Ints or Doubles Stdin swift
//Reading an Int
let number = Int(readLine()!)!
//Reading a Double
let decimalNumber = Double(readLine()!)!
//Reading a string
let text = readLine()!
//reading an array of Ints
guard let intTemp = readLine() else { fatalError("Bad input") }
let intArray: [Int] = intTemp.split(separator: " ").map {
if let intItem = Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
return intItem
} else { fatalError("Bad input") }
}
//reading an array of Doubles
guard let doubleTemp = readLine() else { fatalError("Bad input") }
let doubleArray: [Double] = doubleTemp.split(separator: " ").map {
if let doubleItem = Double($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
return doubleItem
} else { fatalError("Bad input") }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment