benchmarks.wiki / Public workspace

BigCodeBench / BigCodeBench v0.1.0_hf 1485d79e-ef57-5d08-9c0c-c6a0b4a0159e

Problem

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

complete prompt

import re
import pandas as pd


def task_func(df: pd.DataFrame, column_name: str, pattern: str) -> pd.DataFrame:
    """
    Reverse the order of words in a specific column of a pandas DataFrame where the words
    match a user-specified regular expression pattern, using a nested helper function.
    Words are considered to be whitespace-separated strings. This function maintains the
    original order of non-matching words.

    Parameters:
    - df (pd.DataFrame): The pandas DataFrame.
    - column_name (str): The name of the column to be modified.
    - pattern (str), the regular expression pattern to match words against.

    Returns:
    - pd.DataFrame: A new pandas DataFrame with the specified column's words reordered
    if they match the pattern, maintaining the original order of words that do not match,
    and returning a copy of the unaltered DataFrame if the pattern is empty.

    Requirements:
    - pandas
    - re

    Example:
    >>> df = pd.DataFrame({'A': ['apple orange', 'red yellow green'], 'B': [1, 2]})
    >>> pattern = r'\b(?:apple|yellow)\b'
    >>> reversed_df = task_func(df, 'A', pattern)
    >>> reversed_df
                      A  B
    0      apple orange  1
    1  red yellow green  2
    >>> df = pd.DataFrame({'A': ['yellow car red', 'green apple yellow'], 'B': [3, 4]})
    >>> pattern = r'\b(?:car|apple|yellow)\b'
    >>> reversed_df = task_func(df, 'A', pattern)
    >>> reversed_df
                        A  B
    0      yellow car red  3
    1  green apple yellow  4
    """

instruct prompt

Reverse the order of words in a specific column of a pandas DataFrame where the words match a user-specified regular expression pattern, using a nested helper function. Words are considered to be whitespace-separated strings. This function maintains the original order of non-matching words.
The function should output with:
    pd.DataFrame: A new pandas DataFrame with the specified column's words reordered
    if they match the pattern, maintaining the original order of words that do not match,
    and returning a copy of the unaltered DataFrame if the pattern is empty.
You should write self-contained code starting with:

Code

import re
import pandas as pd
def task_func(df: pd.DataFrame, column_name: str, pattern: str) -> pd.DataFrame:

code prompt

Code

import re
import pandas as pd
def task_func(df: pd.DataFrame, column_name: str, pattern: str) -> pd.DataFrame:

entry point

task_func

libs

  • pandas
  • re

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