MIME Type puzzle discussion

Please note that sometimes printing stuff to the error stream may cause a timeout too. You may comment out those lines and try again.

Also, what do you use to store the association table? Do you use an array, a dictionary or something else?

i removed all the comment to be sure i let only the essential.
i used a map to store the extensions and the files.
I have one big issue when i try to use a int/boolean that change when the thing is not found to print unknown i have this message:
Local variable j defined in an enclosing scope must be final or effectively finalJava its weird…

The 3rd test case is must be removed. I can open files with two dots before the extension, at least on macOS

Oops, I misunderstood the test case

I have case in Python 3. with :

file_ext: wav
Output: audio/x-wav

But recive:

Failure

Found:
’‘audio/x-wav…’'
Expected:
’‘UNKNOWN’'

What the heck?

What’s the full filename? Is it something like “b.wav.tmp”?

HashMaps is the way to go. that’s what i used.

I feel like the .replaceAll("[^a-zA-Z]", "").toLowerCase(); is not necessary.

Some of the mime types can contain…

  • uppercase letters like cSs so .toLowerCase() causes inaccurate output.

  • numbers so .replaceAll("[^a-zA-Z]", "") removes all the numbers.

It should work if you remove that part.

Hey friends, what are your thoughts on this code? All tests pass, but I’m unsure if it complies with the rules. I apologize for any mistakes in my English.

[Mod edit: Please do not post full code on the forum]

The actual extension is not wav, its tmp (Full file b.wav.tmp), so it should be UNKNOWN. Had the same problem.

TIP: If splitting, check for the last item.

You might be throwing away input which doesn’t have any dots like “a”, instead of printing “UNKNOWN”

I realize that this case consideration is only to the MIME types array (or list, dictionary, etc), this value must to be saved with no modifications, and the extensions array must be always converted to lower case.

In my solution the extensions array and the MIME types array are parallel arrays.

When is time to validate the file names, iterating the extensions array with a for loop I check if the extension in the file name (converted to lower case) is equal to one item of the extensions array, if true, the item from the MIME types array that corresponds to the iteration (parallel arrays) is added to the response.

And all the tests passed.

In other words, it doesn’t matter if in the input there are file names with rare extensions like .cSs the output must to find the correct extension and return the MIME type exactly like was previously defined (e.g, “.tif” must to show “image/TIFF”, “.cSs” must to show “text/css”).

I am not a native of the English language, I hope this helps.

Hi Guys,
I did my program with Python and like most of people, all my tests has been ok except two tests. (5th Detection of same prefix extensions and 10th Large dataset.)
Here is my code (I’m a beginner please be kind with me :sweat_smile:):
[Mod edit: avoid posting codes on the forum]
I spend lot of times but don’t find any solutions.
Can someone help me please? (sorry for my English)

Try using a dictionary. It should work faster than a list in this puzzle.

it works ! thanks

Can someone help me? It passes all tests except “Large dataset”. How should I optimize this?

const N = parseInt(readline()); // Number of elements which make up the association table.
const Q = parseInt(readline()); // Number Q of file names to be analyzed.
var ext = [];
var mt = [];
for (let i = 0; i < N; i++) {
var inputs = readline().split(’ ');
const EXT = inputs[0]; // file extension
const MT = inputs[1]; // MIME type.
ext[i] = EXT;
mt[i] = MT;
}
for (let i = 0; i < Q; i++) {
var a = 0;
const FNAME = readline(); // One file name per line.
for(let j = 0; j < N; j++){
if(FNAME.lastIndexOf(“.”) >= 0 && FNAME.slice(FNAME.lastIndexOf(“.”)+1, FNAME.length).toUpperCase() == ext[j].toUpperCase()){
console.log(mt[j]);
a++;
break;
}
}
if(a == 0){
console.log(“UNKNOWN”);
}
}

Try using a dictionary instead of checking the extensions one by one.

Hello

I’m blocked for the large dataset in C++.
I used the map and the break (to stop when the extension is found), but I keep getting the message that the process execution time has been exceeded.

Is there any more tips ?

Do you iterate through the keys one by one to look for a match? If so, that’s probably too slow and also unnecessary.

It is just bugged.
It won’t accept any answer, even if it is supposed to be the good one

Probably your code is bugged. If you want, you may send me your code in private message and I can have a look later today.