1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[7377],{7377:function(e,n,t){t.r(n),n.default='## Exercise: Generate Parentheses\n### Instructions\nWrite a function `generate_parentheses(n)` that generates all combinations of well-formed parentheses for a given integer `n`. Each combination should be a string of parentheses characters, where each opening parenthesis is followed by a closing parenthesis. The function should return a list of all valid combinations.\n\nAssume that `n` will always be a positive integer.\n\n### Examples\n```\n>>> generate_parentheses(1)\nOutput: ["()"]\n\n>>> generate_parentheses(2)\nOutput: ["(())", "()()"]\n\n>>> generate_parentheses(3)\nOutput: ["((()))", "(()())", "(())()", "()(())", "()()()"]\n```\n***\ndef generate_parentheses(n):\n def backtrack(s, left, right):\n if len(s) == 2 * n:\n result.append(s)\n return\n if left < n:\n backtrack(s + \'(\', left + 1, right)\n if right < left:\n backtrack(s + \')\', left, right + 1)\n\n result = []\n backtrack(\'\', 0, 0)\n return result\n***\ndef generate_parentheses(n):\n pass\n***\ndef test_case_1():\n return [\n [1], \n ["()"]\n ]\n\ndef test_case_2():\n return [\n [2], \n ["(())", "()()"]\n ]\n\ndef test_case_3():\n return [\n [3], \n ["((()))", "(()())", "(())()", "()(())", "()()()"]\n ]\n\ndef test_case_4():\n return [\n [4], \n ["(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"]\n ]\n\ntest_cases = [test_case_1, test_case_2, test_case_3, test_case_4]'}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.