Spaces:
Runtime error
Runtime error
File size: 21,414 Bytes
b3509ba |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"sys.path.append('..')\n",
"\n",
"from swarmai.challenges.python_challenges.PythonChallenge import PythonChallenge\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"test_challenge_config = Path('D:/00Repos/GPT-Swarm/swarmai/challenges/python_challenges/challenge2/pc2_config.yaml')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"challenge = PythonChallenge(test_challenge_config)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A password is considered strong if the below conditions are all met:\n",
"- It has at least 6 characters and at most 20 characters.\n",
"- It contains at least one lowercase letter, at least one uppercase letter, and at least one digit.\n",
"- It does not contain three repeating characters in a row (i.e., \"Baaabb0\" is weak, but \"Baaba0\" is strong).\n",
"\n",
"Given a string password, return the minimum number of steps required to make password strong. if password is already strong, return 0.\n",
"\n",
"In one step, you can:\n",
"- Insert one character to password,\n",
"- Delete one character from password, or\n",
"- Replace one character of password with another character.\n",
" \n",
"\n",
"Example 1:\n",
"Input: password = \"a\"\n",
"Output: 5\n",
"\n",
"Example 2:\n",
"Input: password = \"aA1\"\n",
"Output: 3\n",
"\n",
"Example 3:\n",
"Input: password = \"1337C0d3\"\n",
"Output: 0\n",
" \n",
"\n",
"Constraints:\n",
"1 <= password.length <= 50\n",
"password consists of letters, digits, dot '.' or exclamation mark '!'.\n",
"\n",
"Include only the following function in your answer enclosed in a code block.\n",
"```python\n",
"def strongPasswordChecker(s: str) -> int:\n",
" \"\"\"\n",
" :type s: str\n",
" :rtype: int\n",
" \"\"\"\n",
" pass\n",
"```\n"
]
}
],
"source": [
"probelm = challenge.get_problem()\n",
"print(probelm)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"test_solution_correct = (\n",
" \"Here is my solution:\\n\"\n",
" \"```python\\n\"\n",
" \"from typing import List\\n\"\n",
" \"def isIdealPermutation(A: List[int]) -> bool:\\n\"\n",
" \" for i in range(len(A)):\\n\"\n",
" \" if i - A[i] > 1 or i - A[i] < -1: return False\\n\"\n",
" \" return True\\n\"\n",
" \"```\\n\"\n",
")\n",
"\n",
"test_solution_incorrect = (\n",
" \"Here is my solution:\\n\"\n",
" \"```python\\n\"\n",
" \"from typing import List\\n\"\n",
" \"def isIdealPermutation(A: List[int]) -> bool:\\n\"\n",
" \" for i in range(len(A)):\\n\"\n",
" \" if i - A[i] > 1 or i - A[i] < -1: return False\\n\"\n",
" \" return False\\n\"\n",
" \"```\\n\"\n",
")\n",
"\n",
"test_solution_error = (\n",
" \"Here is my solution:\\n\"\n",
" \"```python\\n\"\n",
" \"def isIdealPermutation(A: List[int]) -> bool:\\n\"\n",
" \" for i in range(len(A)):\\n\"\n",
" \" if i - A[i] > 1 or i - A[i] < -1: return False\\n\"\n",
" \" return False\\n\"\n",
" \"```\\n\"\n",
")\n",
"\n",
"test_solution_error_internal = (\n",
" \"Here is my solution:\\n\"\n",
" \"```python\\n\"\n",
" \"from typing import List\\n\"\n",
" \"def isIdealPermutation(A: List[int]) -> bool:\\n\"\n",
" \" for i in range(len(A)):\\n\"\n",
" \" if i - A[i] > 1 or i - A[i] < -1: return 'a'/0\\n\"\n",
" \" return False\\n\"\n",
" \"```\\n\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"crappy_solution = \"```python\\ndef strongPasswordChecker(s: str) -> int:\\n # Initialize variables to keep track of password requirements\\n missing_lower = 1\\n missing_upper = 1\\n missing_digit = 1\\n repeating_chars = 0\\n \\n # Initialize variables to keep track of password modifications\\n insertions = 0\\n deletions = 0\\n replacements = 0\\n \\n # Loop through the password to check each character\\n i = 0\\n while i < len(s):\\n # Check for lowercase letter\\n if s[i].islower():\\n missing_lower = 0\\n # Check for uppercase letter\\n elif s[i].isupper():\\n missing_upper = 0\\n # Check for digit\\n elif s[i].isdigit():\\n missing_digit = 0\\n \\n # Check for repeating characters\\n j = i + 1\\n while j < len(s) and s[j] == s[i]:\\n j += 1\\n if j - i >= 3:\\n repeating_chars += j - i - 2\\n \\n print(f'{i}, {j}, {len(s)}')\\n i = j\\n \\n # Check for password length\\n missing_length = max(0, 6 - len(s))\\n if len(s) > 20:\\n deletions = len(s) - 20\\n \\n # Check for password requirements\\n missing_requirements = missing_lower + missing_upper + missing_digit\\n \\n # Check for password modifications\\n if missing_requirements == 0 and repeating_chars == 0:\\n return max(missing_length, deletions)\\n \\n # Case 1: Password too short\\n if len(s) < 6:\\n return missing_requirements + max(missing_length, deletions)\\n \\n # Case 2: Password too long\\n if len(s) > 20:\\n # Reduce repeating characters\\n k = 1\\n while k < 3:\\n print(k)\\n i = 0\\n while i < len(s) and deletions > 0:\\n # Check if character is part of repeating sequence\\n if i > 0 and s[i] == s[i-1]:\\n k += 1\\n else:\\n k = 1\\n \\n # Delete character if part of repeating sequence\\n if k == 3:\\n s = s[:i] + s[i+1:]\\n deletions -= 1\\n k = 2\\n else:\\n i += 1\\n \\n # Reduce repeating characters by replacing characters\\n if k == 2:\\n i = len(s) - 2\\n while i >= 0 and deletions > 0:\\n # Check if character is part of repeating sequence\\n if s[i] == s[i+1]:\\n replacements += 1\\n s = s[:i+1] + chr(ord('a') + (ord(s[i+1]) - ord('a') + 1) % 26) + s[i+2:]\\n deletions -= 1\\n \\n i -= 1\\n \\n # Add missing requirements and length\\n return missing_requirements + max(missing_length, deletions) + replacements\\n \\n # Case 3: Password meets requirements but has repeating characters\\n return max(repeating_chars, missing_requirements) + max(missing_length, deletions)\\n```\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def strongPasswordChecker(s: str) -> int:\n",
" # Initialize variables to keep track of password requirements\n",
" missing_lower = 1\n",
" missing_upper = 1\n",
" missing_digit = 1\n",
" repeating_chars = 0\n",
" \n",
" # Initialize variables to keep track of password modifications\n",
" insertions = 0\n",
" deletions = 0\n",
" replacements = 0\n",
" \n",
" # Loop through the password to check each character\n",
" i = 0\n",
" while i < len(s):\n",
" # Check for lowercase letter\n",
" if s[i].islower():\n",
" missing_lower = 0\n",
" # Check for uppercase letter\n",
" elif s[i].isupper():\n",
" missing_upper = 0\n",
" # Check for digit\n",
" elif s[i].isdigit():\n",
" missing_digit = 0\n",
" \n",
" # Check for repeating characters\n",
" j = i + 1\n",
" while j < len(s) and s[j] == s[i]:\n",
" j += 1\n",
" if j - i >= 3:\n",
" repeating_chars += j - i - 2\n",
" \n",
" i = j\n",
" \n",
" # Check for password length\n",
" missing_length = max(0, 6 - len(s))\n",
" if len(s) > 20:\n",
" deletions = len(s) - 20\n",
" \n",
" # Check for password requirements\n",
" missing_requirements = missing_lower + missing_upper + missing_digit\n",
" \n",
" # Check for password modifications\n",
" if missing_requirements == 0 and repeating_chars == 0:\n",
" return max(missing_length, deletions)\n",
" \n",
" # Case 1: Password too short\n",
" if len(s) < 6:\n",
" return missing_requirements + max(missing_length, deletions)\n",
" \n",
" # Case 2: Password too long\n",
" if len(s) > 20:\n",
" # Reduce repeating characters\n",
" k = 1\n",
" while k < 3:\n",
" i = 0\n",
" while i < len(s) and deletions > 0:\n",
" # Check if character is part of repeating sequence\n",
" if i > 0 and s[i] == s[i-1]:\n",
" k += 1\n",
" else:\n",
" k = 1\n",
" \n",
" # Delete character if part of repeating sequence\n",
" if k == 3:\n",
" s = s[:i] + s[i+1:]\n",
" deletions -= 1\n",
" k = 2\n",
" else:\n",
" i += 1\n",
" \n",
" # Reduce repeating characters by replacing characters\n",
" if k == 2:\n",
" i = len(s) - 2\n",
" while i >= 0 and deletions > 0:\n",
" # Check if character is part of repeating sequence\n",
" if s[i] == s[i+1]:\n",
" replacements += 1\n",
" s = s[:i+1] + chr(ord('a') + (ord(s[i+1]) - ord('a') + 1) % 26) + s[i+2:]\n",
" deletions -= 1\n",
" \n",
" i -= 1\n",
" \n",
" # Add missing requirements and length\n",
" return missing_requirements + max(missing_length, deletions) + replacements\n",
" \n",
" # Case 3: Password meets requirements but has repeating characters\n",
" return max(repeating_chars, missing_requirements) + max(missing_length, deletions)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```python\n",
"def strongPasswordChecker(s: str) -> int:\n",
" # Initialize variables to keep track of password requirements\n",
" missing_lower = 1\n",
" missing_upper = 1\n",
" missing_digit = 1\n",
" repeating_chars = 0\n",
" \n",
" # Initialize variables to keep track of password modifications\n",
" insertions = 0\n",
" deletions = 0\n",
" replacements = 0\n",
" \n",
" # Loop through the password to check each character\n",
" i = 0\n",
" while i < len(s):\n",
" # Check for lowercase letter\n",
" if s[i].islower():\n",
" missing_lower = 0\n",
" # Check for uppercase letter\n",
" elif s[i].isupper():\n",
" missing_upper = 0\n",
" # Check for digit\n",
" elif s[i].isdigit():\n",
" missing_digit = 0\n",
" \n",
" # Check for repeating characters\n",
" j = i + 1\n",
" while j < len(s) and s[j] == s[i]:\n",
" j += 1\n",
" if j - i >= 3:\n",
" repeating_chars += j - i - 2\n",
" \n",
" print(f'{i}, {j}, {len(s)}')\n",
" i = j\n",
" \n",
" # Check for password length\n",
" missing_length = max(0, 6 - len(s))\n",
" if len(s) > 20:\n",
" deletions = len(s) - 20\n",
" \n",
" # Check for password requirements\n",
" missing_requirements = missing_lower + missing_upper + missing_digit\n",
" \n",
" # Check for password modifications\n",
" if missing_requirements == 0 and repeating_chars == 0:\n",
" return max(missing_length, deletions)\n",
" \n",
" # Case 1: Password too short\n",
" if len(s) < 6:\n",
" return missing_requirements + max(missing_length, deletions)\n",
" \n",
" # Case 2: Password too long\n",
" if len(s) > 20:\n",
" # Reduce repeating characters\n",
" k = 1\n",
" while k < 3:\n",
" print(k)\n",
" i = 0\n",
" while i < len(s) and deletions > 0:\n",
" # Check if character is part of repeating sequence\n",
" if i > 0 and s[i] == s[i-1]:\n",
" k += 1\n",
" else:\n",
" k = 1\n",
" \n",
" # Delete character if part of repeating sequence\n",
" if k == 3:\n",
" s = s[:i] + s[i+1:]\n",
" deletions -= 1\n",
" k = 2\n",
" else:\n",
" i += 1\n",
" \n",
" # Reduce repeating characters by replacing characters\n",
" if k == 2:\n",
" i = len(s) - 2\n",
" while i >= 0 and deletions > 0:\n",
" # Check if character is part of repeating sequence\n",
" if s[i] == s[i+1]:\n",
" replacements += 1\n",
" s = s[:i+1] + chr(ord('a') + (ord(s[i+1]) - ord('a') + 1) % 26) + s[i+2:]\n",
" deletions -= 1\n",
" \n",
" i -= 1\n",
" \n",
" # Add missing requirements and length\n",
" return missing_requirements + max(missing_length, deletions) + replacements\n",
" \n",
" # Case 3: Password meets requirements but has repeating characters\n",
" return max(repeating_chars, missing_requirements) + max(missing_length, deletions)\n",
"```\n"
]
}
],
"source": [
"print(crappy_solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"challenge.evaluate_solution(crappy_solution)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.9,\n",
" \"Input: {'A': [2, 0, 1, 3]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [0, 2, 4, 1, 3]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [0, 1, 3, 5, 2, 4, 6]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [1, 5, 4, 3, 0, 2]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [0, 2, 1]}\\nResult: False\\nExpected: True\\nCorrect: False\\n\\nInput: {'A': [2, 0, 3, 5, 4, 1]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [4, 1, 6, 0, 5, 2, 3]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [2, 1, 3, 0]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\\nInput: {'A': [1, 2, 0, 4, 3, 5]}\\nResult: False\\nExpected: False\\nCorrect: True\\n\")"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"challenge1.evaluate_solution(test_solution_incorrect)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0,\n",
" \"Error during loading submitted code. Make sure you enclose your code in ```python\\n ```, include a function with the name isIdealPermutation, and have all the necessary imports.\\nError: name 'List' is not defined\")"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"challenge1.evaluate_solution(test_solution_error)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(0.0,\n",
" \"unsupported operand type(s) for /: 'str' and 'int'\\nInput: {'A': [1, 0]}\\nResult: False\\nExpected: True\\nCorrect: False\\n\\nInput: {'A': [0, 1, 2]}\\nResult: False\\nExpected: True\\nCorrect: False\\n\\nInput: {'A': [0]}\\nResult: False\\nExpected: True\\nCorrect: False\\n\")"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"challenge1.evaluate_solution(test_solution_error_internal)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution:\n",
"\n",
"We can use the following observation to solve this problem in O(n) complexity:\n",
"\n",
"- Any local inversion is a global inversion.\n",
"- If there is any global inversion that is not a local inversion, then the number of global inversions will be greater than the number of local inversions.\n",
"\n",
"Therefore, we only need to check if there is any global inversion that is not a local inversion. We can do this by keeping track of the maximum value seen so far and checking if there is any value after it that is less than it. If there is, then we have found a global inversion that is not a local inversion.\n",
"\n",
"Here's the Python code to implement this algorithm:\n",
"\n",
"```python\n",
"def isIdealPermutation(A: list) -> bool:\n",
" \"\"\"\n",
" Args:\n",
" - A (list[int]): a list of integers.\n",
" \n",
" Returns:\n",
" bool: true if the number of global inversions is equal to the number of local inversions\n",
" \"\"\"\n",
" max_val = -1\n",
" for i in range(len(A)-2):\n",
" max_val = max(max_val, A[i])\n",
" if max_val > A[i+2]:\n",
" return False\n",
" return True\n",
"```\n"
]
}
],
"source": [
"print('Solution:\\n\\nWe can use the following observation to solve this problem in O(n) complexity:\\n\\n- Any local inversion is a global inversion.\\n- If there is any global inversion that is not a local inversion, then the number of global inversions will be greater than the number of local inversions.\\n\\nTherefore, we only need to check if there is any global inversion that is not a local inversion. We can do this by keeping track of the maximum value seen so far and checking if there is any value after it that is less than it. If there is, then we have found a global inversion that is not a local inversion.\\n\\nHere\\'s the Python code to implement this algorithm:\\n\\n```python\\ndef isIdealPermutation(A: list) -> bool:\\n \"\"\"\\n Args:\\n - A (list[int]): a list of integers.\\n \\n Returns:\\n bool: true if the number of global inversions is equal to the number of local inversions\\n \"\"\"\\n max_val = -1\\n for i in range(len(A)-2):\\n max_val = max(max_val, A[i])\\n if max_val > A[i+2]:\\n return False\\n return True\\n```')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv_gptswarm",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|