File size: 7,136 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
[
    {
        "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 factorial(n: int) -> int:\n    \"\"\"Calculate factorial of n\"\"\"\n    if n == 0:\n        return 1\n    return n * factorial(n - 1)",
        "language": "python",
        "task": "boilerplate",
        "description": "Recursive factorial with type hints"
    },
    {
        "code": "def binary_search(arr, target):\n    \"\"\"Binary search implementation\"\"\"\n    left, right = 0, len(arr) - 1\n    \n    while left <= right:\n        mid = (left + right) // 2\n        \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": "Binary search algorithm"
    },
    {
        "code": "class Stack:\n    \"\"\"Simple stack implementation\"\"\"\n    \n    def __init__(self):\n        self.items = []\n    \n    def push(self, item):\n        \"\"\"Add item to stack\"\"\"\n        self.items.append(item)\n    \n    def pop(self):\n        \"\"\"Remove and return top item\"\"\"\n        if not self.is_empty():\n            return self.items.pop()\n        return None\n    \n    def is_empty(self):\n        \"\"\"Check if stack is empty\"\"\"\n        return len(self.items) == 0",
        "language": "python",
        "task": "boilerplate",
        "description": "Stack data structure with docstrings"
    },
    {
        "code": "def quicksort(arr):\n    \"\"\"QuickSort implementation\"\"\"\n    if len(arr) <= 1:\n        return arr\n    \n    pivot = arr[len(arr) // 2]\n    left = [x for x in arr if x < pivot]\n    middle = [x for x in arr if x == pivot]\n    right = [x for x in arr if x > pivot]\n    \n    return quicksort(left) + middle + quicksort(right)",
        "language": "python",
        "task": "boilerplate",
        "description": "QuickSort with list comprehensions"
    },
    {
        "code": "def is_prime(n: int) -> bool:\n    \"\"\"Check if number is prime\"\"\"\n    if n < 2:\n        return False\n    \n    for i in range(2, int(n ** 0.5) + 1):\n        if n % i == 0:\n            return False\n    \n    return True",
        "language": "python",
        "task": "boilerplate",
        "description": "Prime number checker with type hints"
    },
    {
        "code": "def merge_dicts(dict1, dict2):\n    \"\"\"Merge two dictionaries\"\"\"\n    result = dict1.copy()\n    result.update(dict2)\n    return result",
        "language": "python",
        "task": "boilerplate",
        "description": "Dictionary merge utility"
    },
    {
        "code": "def flatten_list(nested_list):\n    \"\"\"Flatten a nested list\"\"\"\n    result = []\n    \n    for item in nested_list:\n        if isinstance(item, list):\n            result.extend(flatten_list(item))\n        else:\n            result.append(item)\n    \n    return result",
        "language": "python",
        "task": "boilerplate",
        "description": "Recursive list flattening"
    },
    {
        "code": "class LinkedListNode:\n    \"\"\"Node for linked list\"\"\"\n    \n    def __init__(self, data):\n        self.data = data\n        self.next = None\n\nclass LinkedList:\n    \"\"\"Simple linked list implementation\"\"\"\n    \n    def __init__(self):\n        self.head = None\n    \n    def append(self, data):\n        \"\"\"Add node to end of list\"\"\"\n        new_node = LinkedListNode(data)\n        \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        \n        current.next = new_node",
        "language": "python",
        "task": "boilerplate",
        "description": "Linked list with proper structure"
    },
    {
        "code": "def levenshtein_distance(s1: str, s2: str) -> int:\n    \"\"\"Calculate Levenshtein distance between two strings\"\"\"\n    if len(s1) < len(s2):\n        return levenshtein_distance(s2, s1)\n    \n    if len(s2) == 0:\n        return len(s1)\n    \n    previous_row = range(len(s2) + 1)\n    \n    for i, c1 in enumerate(s1):\n        current_row = [i + 1]\n        \n        for j, c2 in enumerate(s2):\n            insertions = previous_row[j + 1] + 1\n            deletions = current_row[j] + 1\n            substitutions = previous_row[j] + (c1 != c2)\n            current_row.append(min(insertions, deletions, substitutions))\n        \n        previous_row = current_row\n    \n    return previous_row[-1]",
        "language": "python",
        "task": "boilerplate",
        "description": "Levenshtein distance algorithm with type hints"
    },
    {
        "code": "def memoize(func):\n    \"\"\"Memoization decorator\"\"\"\n    cache = {}\n    \n    def wrapper(*args):\n        if args not in cache:\n            cache[args] = func(*args)\n        return cache[args]\n    \n    return wrapper",
        "language": "python",
        "task": "boilerplate",
        "description": "Memoization decorator pattern"
    },
    {
        "code": "import json\n\ndef read_json_file(filepath: str) -> dict:\n    \"\"\"Read and parse JSON file\"\"\"\n    try:\n        with open(filepath, 'r', encoding='utf-8') as f:\n            return json.load(f)\n    except FileNotFoundError:\n        print(f\"File not found: {filepath}\")\n        return {}\n    except json.JSONDecodeError:\n        print(f\"Invalid JSON in file: {filepath}\")\n        return {}",
        "language": "python",
        "task": "boilerplate",
        "description": "JSON file reader with error handling"
    },
    {
        "code": "def chunk_list(lst, chunk_size):\n    \"\"\"Split list into chunks of specified size\"\"\"\n    return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]",
        "language": "python",
        "task": "boilerplate",
        "description": "List chunking utility"
    },
    {
        "code": "from datetime import datetime\n\ndef format_timestamp(timestamp: float) -> str:\n    \"\"\"Format Unix timestamp to readable string\"\"\"\n    dt = datetime.fromtimestamp(timestamp)\n    return dt.strftime('%Y-%m-%d %H:%M:%S')",
        "language": "python",
        "task": "boilerplate",
        "description": "Timestamp formatting utility"
    },
    {
        "code": "def remove_duplicates(lst):\n    \"\"\"Remove duplicates while preserving order\"\"\"\n    seen = set()\n    result = []\n    \n    for item in lst:\n        if item not in seen:\n            seen.add(item)\n            result.append(item)\n    \n    return result",
        "language": "python",
        "task": "boilerplate",
        "description": "Duplicate removal preserving order"
    }
]