Popis problému Celý popis zadania sa nacháza – Hackerrank. Riešenie Regulárny výraz potrebný na rozdelenie telefónneho čísla: \s|\- Vytvoril som riešenie v týchto programovacích jazykoch: Scala Java JavaScript Ruby Všetky riešenia sú dostupné aj na mojom GitHub profile. Scala
|
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
|
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
|
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 |