Last active
June 26, 2017 05:36
-
-
Save heanfig/60b5dcc9e9fdec214aae6b6c563b7430 to your computer and use it in GitHub Desktop.
30 days of code
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
/* Day 8: Dictionaries and Maps | |
# Task | |
# Given N names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be | |
# given an unknown number of names to query your phone book for; for each queried, print the associated entry from your phone book (in | |
# the form name = phoneNumber) or Not found if there is no entry for name. | |
# Note: Your phone book should be a Dictionary/Map/HashMap data structure. | |
# Input Format | |
# The first line contains an integer, N, denoting the number of entries in the phone book. | |
# Each of the N subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a | |
# friend's name, and the second value is an 8-digit phone number. | |
# After the N lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look | |
# up, and you must continue reading lines until there is no more input. | |
# Note: Names consist of lowercase English letters and are first names only. | |
# Constraints | |
# 1 <= N <= 10^5 | |
# 1 <= queries <= 10^5 | |
# Output Format | |
# On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise, print the full | |
# name and phoneNumber in the format name = phoneNumber. | |
# Enter your code here. Read input from STDIN. Print output to STDOUT | |
*/ | |
//Sample Input | |
3 | |
sam 99912222 | |
tom 11122222 | |
harry 12299933 | |
sam | |
edward | |
harry | |
//Sample Output | |
sam=99912222 | |
Not found | |
harry=12299933 | |
//My Solution | |
function processData(input) { | |
var r=input.split(/\n/),o=Number(r[0]),c=r.length-1,x=[]; | |
r.splice(0,o+1); | |
r.forEach((l)=>{ | |
var rx=new RegExp(l+"\\s(\\d+)"),o=input.match(rx); | |
console.log(o==null?"Not found":l+"="+o[1]); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment