Last active
June 13, 2022 09:36
-
-
Save verebes1/e09cb29a50755191ba44e9d3c915ba04 to your computer and use it in GitHub Desktop.
HackerRank Input Reading Arrays of Ints or Doubles Stdin swift
This file contains 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
//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