Spaces:
Sleeping
Sleeping
File size: 4,004 Bytes
f9adcbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
[
{
"code": "def safe_divide(a, b):\n \"\"\"Safely divide two numbers, handling zero division\"\"\"\n if b == 0:\n return None\n return a / b",
"language": "python",
"task": "fix",
"description": "Zero division protection with None return"
},
{
"code": "def calculate_average(numbers):\n \"\"\"Calculate average of a list, handling empty list\"\"\"\n if not numbers:\n return 0\n return sum(numbers) / len(numbers)",
"language": "python",
"task": "fix",
"description": "Safe average calculation with empty list check"
},
{
"code": "try:\n result = risky_operation()\nexcept Exception as e:\n logger.error(f'Operation failed: {e}')\n result = None",
"language": "python",
"task": "fix",
"description": "Safe error handling with logging"
},
{
"code": "def validate_input(value):\n \"\"\"Validate and sanitize user input\"\"\"\n if value is None:\n raise ValueError('Value cannot be None')\n if not isinstance(value, (int, float)):\n raise TypeError(f'Expected number, got {type(value).__name__}')\n return value",
"language": "python",
"task": "fix",
"description": "Input validation with type checking"
},
{
"code": "import os\nfrom pathlib import Path\n\nfile_path = Path('data/file.txt')\nif file_path.exists():\n with open(file_path, 'r') as f:\n content = f.read()\nelse:\n print(f'File not found: {file_path}')",
"language": "python",
"task": "fix",
"description": "Safe file reading with existence check"
},
{
"code": "def process_list(items):\n \"\"\"Process list items safely\"\"\"\n if not items:\n return []\n \n results = []\n for item in items:\n try:\n processed = transform(item)\n results.append(processed)\n except Exception as e:\n logger.warning(f'Failed to process {item}: {e}')\n continue\n \n return results",
"language": "python",
"task": "fix",
"description": "Robust list processing with error handling"
},
{
"code": "def factorial(n):\n \"\"\"Calculate factorial recursively\"\"\"\n if n == 0 or n == 1:\n return 1\n return n * factorial(n - 1)",
"language": "python",
"task": "boilerplate",
"description": "Recursive factorial implementation"
},
{
"code": "def fibonacci(n):\n \"\"\"Calculate nth Fibonacci number\"\"\"\n if n <= 1:\n return n\n return fibonacci(n-1) + fibonacci(n-2)",
"language": "python",
"task": "boilerplate",
"description": "Recursive Fibonacci implementation"
},
{
"code": "def binary_search(arr, target):\n \"\"\"Binary search in sorted array\"\"\"\n left, right = 0, len(arr) - 1\n \n while left <= right:\n mid = (left + right) // 2\n if arr[mid] == target:\n return mid\n elif arr[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n \n return -1",
"language": "python",
"task": "boilerplate",
"description": "Iterative binary search"
},
{
"code": "class Node:\n def __init__(self, value):\n self.value = value\n self.next = None\n\nclass LinkedList:\n def __init__(self):\n self.head = None\n \n def append(self, value):\n new_node = Node(value)\n if not self.head:\n self.head = new_node\n return\n \n current = self.head\n while current.next:\n current = current.next\n current.next = new_node",
"language": "python",
"task": "boilerplate",
"description": "Simple linked list implementation"
}
] |