MIME TYPE

Опубликовано: 21 Май 2026
на канале: One Person Studio
493
5

I must use a case-insensitive technique to extract a string into its component parts, compare them, and find similar strings. Additionally, I need to build and use an associative array and browse a sizable list of elements.

This puzzle requires me to match file names with their MIME type.

EXPLANATION OF CODE:
1) The user's input is retrieved in the code's first two lines. The first line specifically obtains an integer value called nbAssociations, which stands for the quantity of connected file extensions and MIME types. Another integer value named nbFiles, which denotes the number of files whose MIME types will be determined, is retrieved in the second line.
2) The next few lines of code create a dictionary called assoc that associates each file extension with its corresponding MIME type. The for loop iterates nbAssociations times and assigns the user input to the ext and mime variables. Each file extension is converted to lowercase using the lower() method and is then used as a key in the assoc dictionary, with its corresponding MIME type as the value.
3) The MIME type of each input file is identified using the second for loop. The user input is assigned to the name variable during the loop's nbFiles iterations. After changing the name's case to lowercase using the lower() function, the file name and extension are separated using the split() method with a period as input. The code fetches the file's extension using the name[-1] if it exists (since the extension is the last element in the name list). The function sets ext to None if the file doesn't have an extension.
4) The code then displays the MIME type of the currently open input file. The method fetches the MIME type and outputs it if the file contains an extension and there is a matching MIME type in the assoc dictionary. The function prints "UNKNOWN" if no corresponding MIME type is present.
#codingame #python #programming #coding #mimetypes #strings #hashmap #loops #conditions #easy #puzzle