benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 079e8999-89e6-520f-9f51-bb4c3c59c21b

Problem

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

complete prompt

import heapq
import random

def task_func(obj_list, attr, top_n=5, seed=None):
    """
Find the top N values of the specified attribute in a list of objects.
Return the top N values as well a a randomly sampled value of all attributes.

Parameters:
obj_list (list): The list of objects.
attr (str): The attribute to find the top N values.
top_n (int, optional): The number of top values to retrieve. Defaults to 5.
seed (float, optional): The seed used for randomly choosing an attribute.

Returns:
list[int]: The top N values as a list of integers. Empty list if there are no attributes.
float: A randomly chosen value of all attributes, None if there are no attributes.

Requirements:
- heapq
- random
    
Example:
    >>> # Sample data class used in the example
    >>> class Object:
    ...     def __init__(self, value):
    ...         self.value = value
    ...
    >>> random.seed(1)
    >>> obj_list = [Object(random.randint(1, 100)) for _ in range(33)]
    >>> top_values, random_value = task_func(obj_list, 'value', 5, seed=1)
    >>> print(top_values)
    [99, 98, 98, 98, 93]
    >>> print(random_value)
    58

    >>> class Object:
    ...     def __init__(self, value):
    ...         self.test = value
    ...
    >>> random.seed(2)
    >>> obj_list = [Object(random.randint(1, 12)) for _ in range(13)]
    >>> top_values, random_value = task_func(obj_list, 'test', 2, 12)
    >>> print(top_values)
    [12, 11]
    >>> print(random_value)
    5
"""

instruct prompt

Find the top N values of the specified attribute in a list of objects. Return the top N values as well a a randomly sampled value of all attributes. >>> class Object: ...     def __init__(self, value): ...         self.test = value ... >>> random.seed(2) >>> obj_list = [Object(random.randint(1, 12)) for _ in range(13)] >>> top_values, random_value = task_func(obj_list, 'test', 2, 12) >>> print(top_values) [12, 11] >>> print(random_value) 5
The function should output with:
    list[int]: The top N values as a list of integers. Empty list if there are no attributes.
    float: A randomly chosen value of all attributes, None if there are no attributes.
You should write self-contained code starting with:

Code

import heapq
import random
def task_func(obj_list, attr, top_n=5, seed=None):

code prompt

Code

import heapq
import random
def task_func(obj_list, attr, top_n=5, seed=None):

entry point

task_func

libs

  • random
  • heapq

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