Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Splitting pattern:
\s|\-
I created solution in:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import scala.io.Source object SplitPhoneNumber extends App { private[this] val SPLIT_PATTERN = " |\\-" val numbers = Source.stdin.getLines().drop(1) numbers.foreach(num => { val splits = num.split(SPLIT_PATTERN) println(s"CountryCode=${splits(0)},LocalAreaCode=${splits(1)},Number=${splits(2)}") }) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.*; public class SplitPhoneNumbers { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { String[] splits = stdin.nextLine().split(" |\\-"); System.out.println("CountryCode=" + splits[0] + ",LocalAreaCode=" + splits[1] + ",Number=" + splits[2]); } stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
'use strict'; const processData = input => { let numbers = input.split('\n').slice(1); for(let i = 0; i < numbers.length; i++) { let splits = numbers[i].split(/ |\-/); console.log("CountryCode=" + splits[0] + ",LocalAreaCode=" + splits[1] + ",Number=" + splits[2]); } }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Ruby
1 2 3 4 5 |
tests = gets.chomp.to_i tests.times do (country_code, local_area, number) = gets.chomp.split(/ |\-/) puts "CountryCode=#{country_code},LocalAreaCode=#{local_area},Number=#{number}" end |