{ "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 }