Skip to content

Instantly share code, notes, and snippets.

@fahied fahied/hackerRank.swift
Last active Jul 4, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.