MIME Type puzzle discussion

I have a solution and it fails. However I do not understand why: I just post it here:

import sys
import math
import re

n = int(input())  # Number of elements which make up the association table.
q = int(input())  # Number Q of file names to be analyzed.

atab={}

for i in range(n):
    # ext: file extension
    # mt: MIME type.
    ext, mt = input().split()
    atab[ext.upper()]=mt
    print(ext + " -> " + mt, file=sys.stderr)
    
for i in range(q):
    fname = input()  # One file name per line.
    print("----------------------", file=sys.stderr)
    print("Filename = \"" + fname + "\"", file=sys.stderr)
    m = re.match(".*\\.([a-zA-Z0-9]+)", fname)
    if m:
        ext = m.group(1).upper()
        if len(ext) <= 10:
            print("Looking for " + ext, file=sys.stderr)
            if ext in atab:
                print(atab[ext])
            else:
                print("Extension is not registered!", file=sys.stderr)
                print("UNKNOWN")
        else:
            print("Extension is to long!", file=sys.stderr)
            print("UNKNOWN")
            
    else:
        print("No extension found!", file=sys.stderr)
        print("UNKNOWN")

This is the output on a failing testcase:

Standard Output Stream:

wav -> audio/x-wav
mp3 -> audio/mpeg
pdf -> application/pdf
----------------------
Filename = "a"
No extension found!
UNKNOWN
----------------------
Filename = "a.wav"
Looking for WAV
audio/x-wav
----------------------
Filename = "b.wav.tmp"
Looking for TMP
Extension is not registered!
UNKNOWN
----------------------
Filename = "test.vmp3"
Looking for VMP3
Extension is not registered!
UNKNOWN
----------------------
Filename = "pdf"
No extension found!
UNKNOWN
----------------------
Filename = ".pdf"
Looking for PDF
application/pdf
----------------------
Filename = "mp3"
No extension found!
UNKNOWN
----------------------
Filename = "report..pdf"
Looking for PDF
application/pdf
----------------------
Filename = "defaultwav"
No extension found!
UNKNOWN
----------------------
Filename = ".mp3."
Looking for MP3
audio/mpeg
----------------------
Filename = "final."
No extension found!
UNKNOWN

Failure
Found:

a

Expected:

U

That doesn’t look right.

I don’t get it. I wrote my code in C and I’m having trouble with the limit extension size, though I have tried limiting the extension to 10 chars or limiting the file name extension to 10 chars or not limiting anything.
It basically passes all the following tests and still stuck at 95%:
1)
2
2
htmlaaaaaaaab text/html
png image/png
index
.htmlaaaaaaaaaaaaaa
2)
2
2
htmlaaaaaa text/html
png image/png
index
.htmlaaaaaaaaaaaaaa
3)
2
2
htmlaaaaaaaab text/html
png image/png
index
.htmlaaaaaa
4)
2
2
htmlaaaaaaaa text/html
png image/png
index
.htmlaaaaaaaa

Does anyone have any other idea what to do? Or what combination to test?

LOL, I had to delete my debug logs to make my algorithm quick enough to pass the last test.

Many thanks for pointing out!

Changing my regular expression to “.*\.([a-zA-Z0-9]+$” solved the task.

Hello, nice puzzle, thanks.
I’m wondering if it’s normal to consider “.pdf” as a valid filename (3rd test)
I agree that’s a valid extension but an extension cannot exist by itself…it has to be after a filename (and a “.” ) since we are trying to determine MIMETYPE from a list of file.

Not a big deal though.

Bye

That’s an outdated DOSism. Not sure how Windows handles it nowadays, but “.pdf” is a perfectly valid (if hidden) Unix filename.

Hello,

Anyone have the right output for the 03 test ?
I have a bug but i don’t know what is wrong in my output.
thank.

Hello Everyone! I resolved in c++ without the use of map :slight_smile:

This is not a bug, a file can not just be called .extension (before yea i know… but … just… website don’t know ok!). you just need to look at if the filename is correct.

thanks a lot Wind_Blade.

You just saved my evening. I always forget about that “+” has so bad Performance.

To anyone who is having the same problem in Java: Use StringBuffer!

What are the the factors that decide the timeout process? on an IDE it gives the correct output on the Large Dataset test case but here it gives a timeout.
It is easy to solve splitting the code onto methods but i was just curious about it.

wav audio/x-wav
mp3 audio/mpeg
pdf application/pdf

a
a.wav
b.wav.tmp
test.vmp3
pdf
.pdf
mp3
report…pdf
defaultwav
.mp3.
final.

UNKNOWN
audio/x-wav
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
Can someone tell me plz where is a mistake in this output? For me it seems to be correct, but when checking it says:
“Failure
Found: U
Expected: a”
(mistake on 3d test)

Hi!
I can’t pass the last case Large dataset. Error message is “Failure. Process has timed out. This may mean that your solution is not optimized enough to handle some cases.” =,(((

Who can explain how to solve this case? Because the other 9 cases in puzzle I already passed.

Your code is too slow, you should look to optimize it. You might do unnecessary computations.

.pdf and report…pdf are valid extensions.
You can have a look at each test case by clicking on the small button on the top right of the test cases section.

Thank you for your advice! Finally done with this exercise.

How would that look like? Am new and I don’t know what I should change`?

you should find and remove the computations that are not necessary to find the solution.