Last active
July 4, 2017 06:14
-
-
Save fahied/8321b120cf21fff65af243189a317613 to your computer and use it in GitHub Desktop.
HackerRank Read Input with 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
//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