Friday, 2 December 2016

Read phone number from a from a phone book by name

Input Format
The first line contains an integer, , denoting the number of entries in the phone book. 
Each of the  subsequent lines describes an entry in the form of  space-separated values on a single line. The first value is a friend's name, and the second value is an -digit phone number.
After the  lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.
Sample Input
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
Sample Output
sam=99912222
Not found
harry=12299933

Dictionary<string, string> di = new Dictionary<string, string>();
            int length = Convert.ToInt32(Console.ReadLine());

            while (di.Count < length)
            {
                string s = Console.ReadLine();
                string[] arr = s.Split(' ');
                di.Add(arr[0], arr[1]);
            }
            string name = Console.ReadLine();
            List<string> names = new List<string>();
            while (!string.IsNullOrWhiteSpace(name))
            {
                names.Add(name);
                name = Console.ReadLine();
            }

            foreach(string s in names)
            {
                if (di.ContainsKey(s.ToString()))
                {
                    Console.WriteLine("{0}={1}",s.ToString(), di[s.ToString()]);
                }
                else
                {
                    Console.WriteLine("Not found");
                }
            }


                Console.ReadLine();

1 comment:

  1. Wouldn't you want an experienced developer to use a class (OOP) and some intent revealing code with SOLID principleS showing, too? Wouldn't take much longer and would reveal a lot in the developer's attitude.

    ReplyDelete

Note: only a member of this blog may post a comment.