Telephone Numbers puzzle discussion

Hello,
I solved the puzzel in the sugestet way, but althow tryed another way.
And here I have a problem with my code but cant find wats wrong.
Maybe one of you can finde the bug…
would be nice to know…

[No full code please, instead show the lines where you suspect the bug]

Guess what ? I had an undefined value in some code part, which triggered an exception in my Javascript code. Fixing that allowed me to solve that puzzle.

Use Python3

Apparently the 4th test has an extra phone number compared to what’s announced : 11892011
Can someone confirm it, please ?

I only noticed it in C. Everything works fine in Python !

Most likely a bug in your code. The test cases are the same for all languages. The number you wrote is not part of the 4th test case. You can see them by clicking on the list icon in the tests section.

I used this python 3 code to make a trie, but I’m a little confused:

def make_trie(*words):
root = dict()
for word in words:
    current_dict = root
    for letter in word:
        current_dict = current_dict.setdefault(letter, {})
    current_dict['_end'] = '_end'
return root

I thought the current_dict = root line was unnecessary and I could just delete it and substitute all occurrences of current_dict with root and it would still do the same thing. I know this actually doesn’t work as I tried it and an empty dictionary was returned.

I also tried putting print statements in the second for loop to see how current_dict and root were updated. I thought that since they were set to be equal, they referred to the same dictionary and would be updated simultaneously, but that wasn’t the case.

What am I misunderstanding?

Hello,

I pass al tests but I’ve got only 44% when I submit :frowning:
I’m a looking for test cases to help me understand where I do wrong, can you help me ?

I failed on :

  • Nested numbers
  • Numbers with a different base
  • All the digits of a number are identical
  • All the numbers are identical

If you have idea of which test I could add to make my code failed, it would be great.

Swann

Mouahahahahahah !!! evil_laugh

I know now that no telephone number of previously listed tests start by ‘0’… And I removed my bad initialisation of my tree. Now all works good.

Man this is quite simple, just visualise the statement, in C++ all you need is a map<string,int> and just insert all the substrings of the telephone number into the map, and finally count the size of the map.

Basic trie data structure should do the trick. All tests past.

Hello,
Just to be sure, in the last test, i get “81680” as final answer, but “45512” is awaited…
45512 numbers for a 10000 long telephone number list seems to me very low. Is there an issue?

No, 45512 is right.

Damned! Ok thanxx

The way you solve the problem is not appropriated I think… First I did like you using List of String and comparing subString… And like you, it took me 10 minutes to solve the issue. Then I did it again using Tree… And now it is not the same story.
At the end, solution with tree is obviously faster even if it is more complex to write and understand (But for the last comment (complex and difficult to understand => it is also because I don’t usually use tree)…

Like you I failed on the last test obaining 45438 instead of 45512.
Finally get it right since I checked my tree and It appeared that I wrongly put the next index on some last nodes of numbers when they should get an ‘end’ tag.

Hello.

I have passed first 4 tests, but I got 28330 in the last one.
I have no clue what I`m doing wrong :confused:
Could someone please take a look at my code and point my error?

edit: The only thing that comes to my mind is a question…
if number has 3 digits → 112, and than we have longer number → 112 45 67
can we use 112 as a base?

let tree = [];
let roots = [];

const N = parseInt(readline());
for (let i = 0; i < N; i++) {
    let telephone = readline();
    if(telephone){
        let numbers = telephone.split('');
        let nodes = [...roots];
        let index = 0;
        let parent = false;
        while(nodes.length){
            let node = nodes.pop();
            if(node.number === numbers[index]){
                parent = node;
                index++;
                if(node.children.length && index < numbers.length ){
                    for(let c=0;c<node.children.length;c++){
                            {
                                if(node.children[c].number === numbers[index])
                                nodes.push(node.children[c]);
                            }
                    }
                }
            }
        }
        addNumberToTree(telephone, parent, index);
    }
}
function addNumberToTree(number, p, numberIndex){
    let numbers = number.split('');
    let parent = p;
    for(let i=numberIndex;i<numbers.length;i++){
        let node = {
            number:numbers[i],
            parent: parent,
            children: []
        };
        tree.push(node);
        if(!parent){
            roots.push(node);
        }
        if(parent){
            parent.children.push(node);
        }
        parent = node;
    }
}

console.log(tree.length);

Your question on base: You should be able to work out the answer based on test case 3 :wink:

And regarding your code, I did a quick test. Your code starts to give a wrong answer in the last test case when it comes to the 8th phone number 633. For the first 8 numbers, the answer should be 66 but your code gives 65. This may serve as a starting point of your debugging process. Hope this helps.

Hi.
Can anyone help to check my Kotlin solution?
It passed first 4 tests but failed in the last one with result 81680.

import java.util.*
import java.io.*
import java.math.*

data class Node(
    val value: Int? = null,
    var next: Node? = null
)

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
fun main(args : Array<String>) {
    val input = Scanner(System.`in`)
    val N = input.nextInt()
    var count = 0
    var head = Node()
    for (i in 0 until N) {
        val telephone = input.next()
        var node = head
        telephone.forEach {
            val value = it - '0'
            //System.err.println("${value} ${count} ${node}")
            if (node.next?.value != value) {
                count ++
                node.next = Node(value)
            } 
            node = node.next ?: Node()
        }
    }

    // Write an answer using println()
    // To debug: System.err.println("Debug messages...");


    // The number of elements (referencing a number) stored in the structure.
    println("${count}")
}

Your code starts to give the wrong answer from the 7th number (59278). For your reference:
First 7 numbers: correct count = 64 (instead of 65 per your code)
First 8 numbers: correct count = 66 (instead of 68 per your code)

I got the reason. My data structure is incorrect, should be tree instead of linked list