Computer Screen Screengrab

Solving the MIME Type Detection Puzzle

Approach:

  1. Parse the input to retrieve the association table and the file names.
  2. Store the association table in a data structure for easy lookup.
  3. Iterate through the file names, extracting their extensions.
  4. Check if the extension exists in the association table.
  5. If found, print the corresponding MIME type; otherwise, print “UNKNOWN”.
<?php
// Number of elements in the association table
fscanf(STDIN, "%d", $N);

// Number of file names to be analyzed
fscanf(STDIN, "%d", $Q);

// Associative array to store file extensions and MIME types
$mimeTypes = [];

// Parsing the association table
for ($i = 0; $i < $N; $i++)
{
    fscanf(STDIN, "%s %s", $EXT, $MT);
    $mimeTypes[strtolower($EXT)] = $MT; // Store in lowercase for case-insensitive lookup
}

// Parsing and analyzing file names
for ($i = 0; $i < $Q; $i++)
{
    $FNAME = stream_get_line(STDIN, 256 + 1, "\n");

    // Extracting file extension
    $fileParts = explode('.', $FNAME);
    $extension = end($fileParts);

    // Checking if the extension exists in the association table
    if (array_key_exists(strtolower($extension), $mimeTypes)) {
        echo $mimeTypes[strtolower($extension)] . "\n"; // Print MIME type
    } else {
        echo "UNKNOWN\n"; // Print "UNKNOWN" if MIME type not found
    }
}
?>

By following this approach and using the provided example code, you can efficiently solve the MIME Type Detection puzzle on Codingame.

To explore the puzzle further and try solving it yourself, visit the MIME Type Detection Puzzle on Codingame.

Further Reading:

Agile principles are a set of values and practices for software development that prioritize flexibility, collaboration, responsiveness to change, and customer-focused, enabling teams to deliver high-quality products that meet evolving requirements efficiently.

Here is the improved version of code:

<?php
// Function to fetch MIME type using API
function getMimeType($fileName) {
    // API endpoint URL
    $apiUrl = "https://api.example.com/detect-mime";

    // Prepare data for API request
    $postData = array('file' => base64_encode(file_get_contents($fileName)));

    // Initialize cURL session
    $curl = curl_init();

    // Set cURL options
    curl_setopt_array($curl, array(
        CURLOPT_URL => $apiUrl,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $postData
    ));

    // Execute cURL request
    $response = curl_exec($curl);

    // Check for errors
    if (curl_errno($curl)) {
        return "UNKNOWN"; // Return "UNKNOWN" if API request fails
    }

    // Close cURL session
    curl_close($curl);

    // Decode API response
    $responseData = json_decode($response, true);

    // Return MIME type from API response
    return $responseData['mime_type'] ?? "UNKNOWN";
}

// Agile: Extract method to handle API response
function handleApiResponse($curl, $fileName) {
    $response = curl_exec($curl);
    if (curl_errno($curl)) {
        return "UNKNOWN"; // Return "UNKNOWN" if API request fails
    }
    curl_close($curl);
    $responseData = json_decode($response, true);
    return $responseData['mime_type'] ?? "UNKNOWN";
}

// Agile: Extract method to perform API request
function performApiRequest($apiUrl, $postData) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $apiUrl,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $postData
    ));
    return $curl;
}

// Common Use Case: MIME type detection for a file
function detectMimeType($fileName) {
    // API endpoint URL
    $apiUrl = "https://api.example.com/detect-mime";

    // Prepare data for API request
    $postData = array('file' => base64_encode(file_get_contents($fileName)));

    // Perform API request
    $curl = performApiRequest($apiUrl, $postData);

    // Handle API response
    return handleApiResponse($curl, $fileName);
}

// Number of elements in the association table
fscanf(STDIN, "%d", $N);

// Number of file names to be analyzed
fscanf(STDIN, "%d", $Q);

// Associative array to store file extensions and MIME types
$mimeTypes = [];

// Parsing the association table
for ($i = 0; $i < $N; $i++) {
    fscanf(STDIN, "%s %s", $EXT, $MT);
    $mimeTypes[strtolower($EXT)] = $MT; // Store in lowercase for case-insensitive lookup
}

// Parsing and analyzing file names
for ($i = 0; $i < $Q; $i++) {
    $FNAME = stream_get_line(STDIN, 256 + 1, "\n");

    // Extracting file extension
    $fileParts = explode('.', $FNAME);
    $extension = end($fileParts);

    // Check if extension exists in association table
    if (array_key_exists(strtolower($extension), $mimeTypes)) {
        echo $mimeTypes[strtolower($extension)] . "\n"; // Print MIME type from association table
    } else {
        // If not found, fetch MIME type using API
        echo detectMimeType($FNAME) . "\n";
    }
}
?>

Discover more from Kvnbbg.fr

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *