Skip to content

Instantly share code, notes, and snippets.

@fahied
Last active July 4, 2017 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/8321b120cf21fff65af243189a317613 to your computer and use it in GitHub Desktop.
Save fahied/8321b120cf21fff65af243189a317613 to your computer and use it in GitHub Desktop.
HackerRank Read Input with Swift
//Read String array separated by new line character
func readInput () -> [String]{
let n: Int = Int(readLine()!)!
var strs = [String]()
(0...n-1).map { _ in
strs.append(readLine()!.lowercased())
}
return strs
}
//Read Int array
func readInput () -> [Int]{
let number: Int = Int(readLine()!)!
let input = readLine()!
let numbers = input.toIntArray()
return numbers
}
extension String {
func toIntArray () -> [Int]{
let numberCharacters = self.characters.split(separator: " ")
let numbers = numberCharacters.map { Int(String($0))! }
return numbers
}
}
//Source
//http://keitaito.com/blog/2017/01/27/how-to-read-standard-input-in-swift.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment