benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf b083d055-8c4f-5378-8b49-fd416cee1694

Problem

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

complete prompt

import os
import json
from collections import Counter


def task_func(json_files_path='./json_files/', key='name'):
    """
    Count the occurrence of a particular key in all json files in a specified directory 
    and return a dictionary with the values of the specified key and their counts.
    
    Parameters:
    - json_files_path (str): The path to the directory containing the JSON files. Default is './json_files/'.
    - key (str): The key in the JSON files whose values need to be counted. Default is 'name'.
    
    Returns:
    dict: A dictionary with values of the key as keys and their counts as values.
    
    Requirements:
    - os
    - json
    - collections.Counter
    
    Example:
    >>> import tempfile
    >>> import json
    >>> directory = tempfile.mkdtemp()
    >>> data = [{'product': 'apple', 'quantity': 5}, {'product': 'banana', 'quantity': 3}]
    >>> for i, d in enumerate(data):
    ...     with open(f"{directory}/{i}.json", 'w') as file:
    ...         json.dump(d, file)

    >>> task_func(json_files_path=directory, key='product')
    {'apple': 1, 'banana': 1}
    """

instruct prompt

Count the occurrence of a particular key in all json files in a specified directory and return a dictionary with the values of the specified key and their counts. >>> task_func(json_files_path=directory, key='product') {'apple': 1, 'banana': 1}
The function should output with:
    dict: A dictionary with values of the key as keys and their counts as values.
You should write self-contained code starting with:

Code

import os
import json
from collections import Counter
def task_func(json_files_path='./json_files/', key='name'):

code prompt

Code

import os
import json
from collections import Counter
def task_func(json_files_path='./json_files/', key='name'):

entry point

task_func

libs

  • json
  • collections
  • os

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