Spaces:
Sleeping
Sleeping
File size: 9,179 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
[
{
"code": "def calculate_bmi(weight: float, height: float) -> float:\n \"\"\"Calculate Body Mass Index\"\"\"\n if height <= 0:\n raise ValueError(\"Height must be positive\")\n return weight / (height ** 2)",
"language": "python",
"task": "boilerplate",
"description": "BMI calculator with validation"
},
{
"code": "def celsius_to_fahrenheit(celsius: float) -> float:\n \"\"\"Convert Celsius to Fahrenheit\"\"\"\n return (celsius * 9/5) + 32",
"language": "python",
"task": "boilerplate",
"description": "Temperature conversion"
},
{
"code": "import random\n\ndef generate_password(length: int = 12) -> str:\n \"\"\"Generate secure random password\"\"\"\n import string\n chars = string.ascii_letters + string.digits + string.punctuation\n return ''.join(random.choice(chars) for _ in range(length))",
"language": "python",
"task": "boilerplate",
"description": "Password generator"
},
{
"code": "def is_palindrome(s: str) -> bool:\n \"\"\"Check if string is palindrome\"\"\"\n s = ''.join(c.lower() for c in s if c.isalnum())\n return s == s[::-1]",
"language": "python",
"task": "boilerplate",
"description": "Palindrome checker"
},
{
"code": "def count_words(text: str) -> int:\n \"\"\"Count words in a string\"\"\"\n return len(text.split())",
"language": "python",
"task": "boilerplate",
"description": "Word counter"
},
{
"code": "import os\n\ndef get_file_size(filepath: str) -> int:\n \"\"\"Get file size in bytes\"\"\"\n try:\n return os.path.getsize(filepath)\n except OSError:\n return -1",
"language": "python",
"task": "boilerplate",
"description": "File size utility"
},
{
"code": "def reverse_list(lst: list) -> list:\n \"\"\"Reverse a list without modifying original\"\"\"\n return lst[::-1]",
"language": "python",
"task": "boilerplate",
"description": "List reversal"
},
{
"code": "def find_max(numbers: list) -> float:\n \"\"\"Find maximum value in list\"\"\"\n if not numbers:\n return None\n return max(numbers)",
"language": "python",
"task": "boilerplate",
"description": "Find max value"
},
{
"code": "def find_min(numbers: list) -> float:\n \"\"\"Find minimum value in list\"\"\"\n if not numbers:\n return None\n return min(numbers)",
"language": "python",
"task": "boilerplate",
"description": "Find min value"
},
{
"code": "def average(numbers: list) -> float:\n \"\"\"Calculate average of list\"\"\"\n if not numbers:\n return 0.0\n return sum(numbers) / len(numbers)",
"language": "python",
"task": "boilerplate",
"description": "Calculate average"
},
{
"code": "def to_uppercase(text: str) -> str:\n \"\"\"Convert string to uppercase\"\"\"\n return text.upper()",
"language": "python",
"task": "boilerplate",
"description": "Uppercase converter"
},
{
"code": "def to_lowercase(text: str) -> str:\n \"\"\"Convert string to lowercase\"\"\"\n return text.lower()",
"language": "python",
"task": "boilerplate",
"description": "Lowercase converter"
},
{
"code": "def capitalize_words(text: str) -> str:\n \"\"\"Capitalize first letter of each word\"\"\"\n return text.title()",
"language": "python",
"task": "boilerplate",
"description": "Title case converter"
},
{
"code": "import json\n\ndef save_json(data: dict, filepath: str):\n \"\"\"Save dictionary to JSON file\"\"\"\n with open(filepath, 'w', encoding='utf-8') as f:\n json.dump(data, f, indent=4)",
"language": "python",
"task": "boilerplate",
"description": "JSON saver"
},
{
"code": "import csv\n\ndef read_csv(filepath: str) -> list:\n \"\"\"Read CSV file into list of dicts\"\"\"\n with open(filepath, 'r', newline='') as f:\n return list(csv.DictReader(f))",
"language": "python",
"task": "boilerplate",
"description": "CSV reader"
},
{
"code": "import time\n\ndef timer_decorator(func):\n \"\"\"Decorator to measure execution time\"\"\"\n def wrapper(*args, **kwargs):\n start = time.time()\n result = func(*args, **kwargs)\n end = time.time()\n print(f\"{func.__name__} took {end-start:.4f}s\")\n return result\n return wrapper",
"language": "python",
"task": "boilerplate",
"description": "Timer decorator"
},
{
"code": "def flatten_dict(d: dict, parent_key: str = '', sep: str = '_') -> dict:\n \"\"\"Flatten nested dictionary\"\"\"\n items = []\n for k, v in d.items():\n new_key = f\"{parent_key}{sep}{k}\" if parent_key else k\n if isinstance(v, dict):\n items.extend(flatten_dict(v, new_key, sep=sep).items())\n else:\n items.append((new_key, v))\n return dict(items)",
"language": "python",
"task": "boilerplate",
"description": "Dict flattener"
},
{
"code": "def safe_division(a: float, b: float) -> float:\n \"\"\"Divide with zero check\"\"\"\n if b == 0:\n return 0.0\n return a / b",
"language": "python",
"task": "boilerplate",
"description": "Safe division"
},
{
"code": "def get_extension(filename: str) -> str:\n \"\"\"Get file extension\"\"\"\n if '.' not in filename:\n return ''\n return filename.split('.')[-1]",
"language": "python",
"task": "boilerplate",
"description": "File extension extractor"
},
{
"code": "import re\n\ndef is_valid_email(email: str) -> bool:\n \"\"\"Validate email format\"\"\"\n pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'\n return bool(re.match(pattern, email))",
"language": "python",
"task": "boilerplate",
"description": "Email validator"
},
{
"code": "def clamp(n: float, min_val: float, max_val: float) -> float:\n \"\"\"Clamp number between min and max\"\"\"\n return max(min_val, min(n, max_val))",
"language": "python",
"task": "boilerplate",
"description": "Clamp function"
},
{
"code": "def lerp(start: float, end: float, t: float) -> float:\n \"\"\"Linear interpolation\"\"\"\n return start + t * (end - start)",
"language": "python",
"task": "boilerplate",
"description": "Linear interpolation"
},
{
"code": "def factorial_iterative(n: int) -> int:\n \"\"\"Calculate factorial iteratively\"\"\"\n result = 1\n for i in range(2, n + 1):\n result *= i\n return result",
"language": "python",
"task": "boilerplate",
"description": "Iterative factorial"
},
{
"code": "def fibonacci_iterative(n: int) -> int:\n \"\"\"Calculate Fibonacci iteratively\"\"\"\n if n <= 1:\n return n\n a, b = 0, 1\n for _ in range(2, n + 1):\n a, b = b, a + b\n return b",
"language": "python",
"task": "boilerplate",
"description": "Iterative Fibonacci"
},
{
"code": "def gcd(a: int, b: int) -> int:\n \"\"\"Calculate Greatest Common Divisor\"\"\"\n while b:\n a, b = b, a % b\n return a",
"language": "python",
"task": "boilerplate",
"description": "GCD calculator"
},
{
"code": "def lcm(a: int, b: int) -> int:\n \"\"\"Calculate Least Common Multiple\"\"\"\n if a == 0 or b == 0:\n return 0\n return abs(a * b) // gcd(a, b)",
"language": "python",
"task": "boilerplate",
"description": "LCM calculator"
},
{
"code": "def is_even(n: int) -> bool:\n \"\"\"Check if number is even\"\"\"\n return n % 2 == 0",
"language": "python",
"task": "boilerplate",
"description": "Even checker"
},
{
"code": "def is_odd(n: int) -> bool:\n \"\"\"Check if number is odd\"\"\"\n return n % 2 != 0",
"language": "python",
"task": "boilerplate",
"description": "Odd checker"
},
{
"code": "def square(n: float) -> float:\n \"\"\"Calculate square of number\"\"\"\n return n * n",
"language": "python",
"task": "boilerplate",
"description": "Square calculator"
},
{
"code": "def cube(n: float) -> float:\n \"\"\"Calculate cube of number\"\"\"\n return n * n * n",
"language": "python",
"task": "boilerplate",
"description": "Cube calculator"
}
] |