benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 6af46385-9895-5c13-bc54-7240fb8638f6

Problem

Answer published by the source. Consult the official source to check your work against its answer.

complete prompt

import xmltodict
import json

def task_func(s, save_json, json_file_path):
    """ 
    Converts an XML string into a dictionary representation and optionally saves it as a JSON file.

    This function is useful for easily accessing data stored in XML format and saving it for future use.

    Parameters:
    s (str): The XML string to be converted.
    save_json (bool): Whether to save the parsed XML as a JSON file.
    json_file_path (str): The file path to save the JSON file. Required if save_json is True.

    Returns:
    dict: A dictionary representation of the XML string.

    Raises:
    ValueError: If the input XML string is empty or contains only whitespace.

    Requirements:
    - xmltodict
    - json

    Examples:
    Convert a simple XML string to a dictionary.
    >>> result = task_func('<person><name>John</name><age>30</age></person>')
    >>> result['person']['name'] + ', ' + result['person']['age']
    'John, 30'

    Convert an XML string with nested elements.
    >>> result = task_func('<school><class><student>Emma</student></class></school>')
    >>> result['school']['class']['student']
    'Emma'

    Save the parsed XML as a JSON file.
    >>> task_func('<data><item>1</item><item>2</item></data>', save_json=True, json_file_path='data.json')
    # A JSON file 'data.json' will be created with the parsed XML data.
    """

instruct prompt

Converts an XML string into a dictionary representation and optionally saves it as a JSON file. This function is useful for easily accessing data stored in XML format and saving it for future use. Convert an XML string with nested elements. >>> result = task_func('<school><class><student>Emma</student></class></school>') >>> result['school']['class']['student'] 'Emma' Save the parsed XML as a JSON file. >>> task_func('<data><item>1</item><item>2</item></data>', save_json=True, json_file_path='data.json') # A JSON file 'data.json' will be created with the parsed XML data.
The function should raise the exception for: ValueError: If the input XML string is empty or contains only whitespace.
The function should output with:
    dict: A dictionary representation of the XML string.
You should write self-contained code starting with:

Code

import xmltodict
import json
def task_func(s, save_json, json_file_path):

code prompt

Code

import xmltodict
import json
def task_func(s, save_json, json_file_path):

entry point

task_func

libs

  • xmltodict
  • json

Discussion

Discussion

No discussion posts on this page yet. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

See answer Answer published by the source

Artifacts

Code, notes and reproducible work shared by participants. Files are served from a separate origin.

No artifacts on this page yet. Share reproducible code or notes in a contribution. Share a minimal failing example, an algorithm with its complexity, or a reproducible command and result. Use the posting template.

Source and history

Official source

initial import