|
1 | | -# Mime Type |
| 1 | +# MIME Type |
2 | 2 |
|
3 | | -This is a solution to the "Mime Type" problem on [Codingame](https://www.codingame.com/training/easy/mime-type). |
| 3 | +## Description |
4 | 4 |
|
5 | | -## Problem Description |
| 5 | +The task involves creating a program to determine the MIME type of files based on their names. This involves associating file extensions with MIME types. The program reads an association table with N elements and a list of Q file names to be analyzed. For each file name, the program identifies the extension by finding the substring after the last dot character. If the extension is found in the association table (case insensitive), it prints the corresponding MIME type. If the MIME type cannot be determined or the file has no extension, it outputs UNKNOWN. The program takes the number of elements in the table, the number of file names, the extension-MIME associations, and the file names as input, and outputs the corresponding MIME types or UNKNOWN. |
6 | 6 |
|
7 | | -You are given a list of file names and their corresponding MIME types. You need to read a list of file names and determine the corresponding MIME type for each file name. If the MIME type cannot be determined, the program should output `UNKNOWN`. |
| 7 | +## Solution Overview |
8 | 8 |
|
9 | | -The MIME type is determined by the file extension. The table of file extensions and their corresponding MIME types is given. |
| 9 | +To solve this problem, we need to map file extensions to their respective MIME types, and then analyze the file names to determine the MIME type by extracting the extension from the file name. If the extension matches one in our table (case insensitive), we return the MIME type; otherwise, we return `UNKNOWN`. |
10 | 10 |
|
11 | | -| Extension | MIME Type | |
12 | | -|-----------|-----------| |
13 | | -|html |text/html | |
14 | | -|htm |text/html | |
15 | | -|png |image/png | |
16 | | -|jpeg |image/jpeg | |
17 | | -|jpg |image/jpeg | |
18 | | -|gif |image/gif | |
19 | | -|bmp |image/bmp | |
20 | | -|txt |text/plain| |
21 | | -|pdf |application/pdf| |
| 11 | +### Explanation: |
22 | 12 |
|
23 | | -The file name may have multiple periods (.) in it, but the extension is always the substring that follows the last period. |
| 13 | +- We use a dictionary `mime_table` to store the file extensions and their corresponding MIME types, converting extensions to lowercase to handle case insensitivity. |
| 14 | +- For each file name, the program finds the last occurrence of a `.` in the string, and extracts the file extension if valid. |
| 15 | +- The MIME type is printed if the extension is found in the dictionary, otherwise `UNKNOWN` is printed. |
24 | 16 |
|
25 | | -## Solution |
| 17 | +### Edge Cases Handled: |
26 | 18 |
|
27 | | -The solution reads the the number of MIME types, number of file names and the file names. It stores the MIME types in a map with the extension as the key. Then it reads a list of file names and determines the corresponding MIME type by looking up the extension in the map. If the extension is not found, it outputs `UNKNOWN`. |
| 19 | +- Files with no extension or a dot at the end. |
| 20 | +- Extensions in different cases (e.g., `.TXT`, `.txt`, `.tXt` all map to the same MIME type). |
| 21 | +- Files whose extensions aren't in the provided table result in `UNKNOWN`. |
28 | 22 |
|
29 | 23 | ## Example Input/Output |
30 | 24 |
|
31 | | -Input: |
| 25 | +**Input** |
32 | 26 |
|
33 | 27 | ``` |
34 | | -4 |
35 | | -2 |
| 28 | +3 |
| 29 | +3 |
36 | 30 | html text/html |
37 | 31 | png image/png |
38 | 32 | gif image/gif |
39 | | -txt text/plain |
40 | | -file.html |
41 | | -file.txt |
| 33 | +animated.gif |
| 34 | +portrait.png |
| 35 | +index.html |
42 | 36 | ``` |
43 | 37 |
|
44 | | -Output: |
| 38 | +**Output** |
45 | 39 |
|
46 | 40 | ``` |
| 41 | +image/gif |
| 42 | +image/png |
47 | 43 | text/html |
48 | | -text/plain |
49 | 44 | ``` |
50 | 45 |
|
51 | | -## Example Code |
| 46 | +## Code Example |
52 | 47 |
|
53 | 48 | ```python |
54 | | -# Read the number of elements in the association table |
55 | | -n = int(input()) |
56 | | - |
57 | | -# Read the number of file names to be analyzed |
58 | | -q = int(input()) |
59 | | - |
60 | | -# Create a dictionary to store the association table |
61 | | -mime_types = {} |
62 | | - |
63 | | -# Read the association table |
64 | | -for _ in range(n): |
65 | | - ext, mime = input().split() |
66 | | - mime_types[ext.lower()] = mime |
67 | | - |
68 | | -# Process each file name |
69 | | -for _ in range(q): |
70 | | - file_name = input().lower() |
71 | | - |
72 | | - # Find the extension of the file |
73 | | - if '.' in file_name: |
74 | | - extension = file_name.split('.')[-1] |
75 | | - if extension in mime_types: |
76 | | - print(mime_types[extension]) |
| 49 | +# Number of elements which make up the association table. |
| 50 | +N = int(input()) |
| 51 | + |
| 52 | +# Number of file names to be analyzed. |
| 53 | +Q = int(input()) |
| 54 | + |
| 55 | +mime_table = {} |
| 56 | + |
| 57 | +# Reading the MIME type associations. |
| 58 | +for _ in range(N): |
| 59 | + ext, mime_type = input().split() |
| 60 | + mime_table[ext.lower()] = mime_type # Store extensions as lowercase for case-insensitivity. |
| 61 | + |
| 62 | +# Processing each file name. |
| 63 | +for _ in range(Q): |
| 64 | + fname = input() |
| 65 | + |
| 66 | + # Find the position of the last '.' in the file name. |
| 67 | + last_dot_index = fname.rfind('.') |
| 68 | + |
| 69 | + # If there's a '.' and it isn't the last character, extract the extension. |
| 70 | + if last_dot_index != -1 and last_dot_index < len(fname) - 1: |
| 71 | + file_ext = fname[last_dot_index + 1:].lower() # Get the file extension and convert to lowercase. |
| 72 | + |
| 73 | + # Check if the extension exists in the mime_table. |
| 74 | + if file_ext in mime_table: |
| 75 | + print(mime_table[file_ext]) # Output the MIME type. |
77 | 76 | else: |
78 | | - print("UNKNOWN") |
| 77 | + print('UNKNOWN') # Output UNKNOWN if extension not found. |
79 | 78 | else: |
80 | | - print("UNKNOWN") |
| 79 | + print('UNKNOWN') # Output UNKNOWN if no extension or '.' at the end. |
81 | 80 |
|
82 | 81 | ``` |
0 commit comments