openenv-code-debugger / code_debug_env /tasks /medium /task_003_mutable_default.json
arnavzz
feat: initial OpenEnv code debugging environment
c14504c
{
"task_id": "task_003_mutable_default",
"difficulty": "medium",
"description": "Fix the bug in the add_item and build_shopping_lists functions. build_shopping_lists should return independent lists for each group of items. Currently all lists share the same underlying list due to a mutable default argument.",
"buggy_code": "def add_item(item, lst=[]):\n lst.append(item)\n return lst\n\n\ndef build_shopping_lists(items_per_list):\n result = []\n for items in items_per_list:\n current_list = add_item(items[0])\n for item in items[1:]:\n current_list = add_item(item)\n result.append(current_list)\n return result\n",
"solution": "def add_item(item, lst=None):\n if lst is None:\n lst = []\n lst.append(item)\n return lst\n\n\ndef build_shopping_lists(items_per_list):\n result = []\n for items in items_per_list:\n current_list = add_item(items[0])\n for item in items[1:]:\n current_list = add_item(item, current_list)\n result.append(current_list)\n return result\n",
"test_descriptions": [
"build_shopping_lists([['a', 'b']]) should return [['a', 'b']]",
"build_shopping_lists([['a', 'b'], ['c', 'd']]) should return [['a', 'b'], ['c', 'd']] (independent lists)",
"build_shopping_lists([['x']]) should return [['x']]",
"Calling build_shopping_lists twice should not carry state between calls"
],
"test_code": "try:\n r1 = build_shopping_lists([['a', 'b']])\n results.append({\"test_name\": \"single_list\", \"passed\": r1 == [['a', 'b']], \"expected\": \"[['a', 'b']]\", \"actual\": str(r1), \"error\": \"\"})\nexcept Exception as e:\n results.append({\"test_name\": \"single_list\", \"passed\": False, \"expected\": \"[['a', 'b']]\", \"actual\": \"\", \"error\": str(e)})\n\ntry:\n r2 = build_shopping_lists([['a', 'b'], ['c', 'd']])\n results.append({\"test_name\": \"independent_lists\", \"passed\": r2 == [['a', 'b'], ['c', 'd']], \"expected\": \"[['a', 'b'], ['c', 'd']]\", \"actual\": str(r2), \"error\": \"\"})\nexcept Exception as e:\n results.append({\"test_name\": \"independent_lists\", \"passed\": False, \"expected\": \"[['a', 'b'], ['c', 'd']]\", \"actual\": \"\", \"error\": str(e)})\n\ntry:\n r3 = build_shopping_lists([['x']])\n results.append({\"test_name\": \"single_item\", \"passed\": r3 == [['x']], \"expected\": \"[['x']]\", \"actual\": str(r3), \"error\": \"\"})\nexcept Exception as e:\n results.append({\"test_name\": \"single_item\", \"passed\": False, \"expected\": \"[['x']]\", \"actual\": \"\", \"error\": str(e)})\n\ntry:\n call1 = build_shopping_lists([['a']])\n call2 = build_shopping_lists([['b']])\n results.append({\"test_name\": \"no_state_leak\", \"passed\": call1 == [['a']] and call2 == [['b']], \"expected\": \"[['a']] and [['b']]\", \"actual\": f\"{call1} and {call2}\", \"error\": \"\"})\nexcept Exception as e:\n results.append({\"test_name\": \"no_state_leak\", \"passed\": False, \"expected\": \"[['a']] and [['b']]\", \"actual\": \"\", \"error\": str(e)})",
"max_steps": 7
}