Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Helllo! im doing a c# programming challange and im woundering if i understood and solved the question right?

The question is:
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

Emergency 911
Alice 95 225 256
Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

The first line of input gives a single integer, 1≤t≤40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1≤n≤10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

For each test case, output “YES” if the list is consistent, or “NO” otherwise.

I solved it this way:
C#
List<string> phoneList = new List<string>();
         string input;

         while ((input = Console.ReadLine()) != null)
         {
           phoneList.Add(input);
         }

         for (int i = 0; i < phoneList.Count; i++)
         {
             for (int j = 0; j < phoneList.Count; j++)
             {

                 if (phoneList[i].Substring(0,3).Equals(phoneList[j].Substring(0,3)) && i != j)
                 {
                     Console.WriteLine("NO");
                     return;
                 }
             }
         }

         Console.WriteLine("YES");



Is it solved Correctly? and i tried to use the Distinct() method insteed but didint know how to make it work with the Substring() method. I really appreciate all Anwsers! Thank you!
Posted
Updated 9-Jun-15 5:02am
v2
Comments
CHill60 9-Jun-15 11:01am    
Try testing it with a variety of data (having worked out your expected results) - you can answer your own question then!
Andy Lanng 9-Jun-15 11:09am    
I like that you ask for general advise. Usually we don't touch homework as you can get caught out if we answer with code.

I will say this: If you can check for duplicates, then you will know that the whole list is invalid if there is one duplicate, or go on to test if there are none.

If all number are unique, then you need only compare a number with shorter numbers.

Start with the shortest and second shortest. You will have to check the numbers against the full length of the shorter of the pair (not just the first 3)

I can see this going up to (n^2)/2 algorithm.
Philippe Mori 9-Jun-15 13:27pm    
I don't think it is the correct approach. Except for numbers like emergency numbers and even then, I think there must be a delay before the actual call start... And more generally, you won't know which length are not valid as the rule vary from countries and for internationnal calls and for extension number.

Thus in the end, you have complex code that it is hard to proove correct and even if correct today, rules might changes in the future as number sometimes get longers...

Thus in practice, it generally don't worth the trouble as the benefice is small and such validation would only help for some very specific errors and won't help in most cases like one wrong digit. A lot of effort for almost nothing...
Member 11753034 9-Jun-15 17:38pm    
Its now only if my soultion is right, its if i interupented the question right, my first language is not english so i had a hard time understanding what i was supposed to do. Have i understood the question right? does prefix mean the firt 3 numbers?

A simpler solution is to sort all the string-based numbers.
At that point, any number which contains another will appear in the list immediately after it.
123456
911
91125426
95225256
So then, it's just one loop:
C#
if (thisString.StartsWith(previousString))
   {
   // It's a match.
   }
 
Share this answer
 
v2
If you are able to make an assumption that all emergency numbers will be a fixed number of digits, in most cases; 3 digits, then the problem is easier to solve. If so, then they can be ignored.

Otherwise you could process a second list of unique prefix numbers:
After your code:
C#
while ((input = Console.ReadLine()) != null)
{
    phoneList.Add(input);
}


C#
List<string> prefixes = new List<string>();
foreach(string number in phoneList)
{
    prefixes.Add(phoneList.SubString(0,2));
}</string></string>

Then check that list contains the phone number being validated.
 
Share this answer
 
Comments
Andy Lanng 9-Jun-15 11:50am    
why do the 2 digit prefixes have to be unique? Am I missing something?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900