{"id":25694,"date":"2021-12-04T09:19:14","date_gmt":"2021-12-04T03:49:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=25694"},"modified":"2021-12-04T09:19:14","modified_gmt":"2021-12-04T03:49:14","slug":"python-code-for-printing-all-possible-subsequences-subsets","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-code-for-printing-all-possible-subsequences-subsets\/","title":{"rendered":"Python Code for Printing all Possible subsequences\/subsets."},"content":{"rendered":"
Let us look at a highly exciting subject called Printing all possible subsequences\/subsets of a given string.<\/p>\n
There are two options for each element in the specified string:<\/p>\n
Include the first element in the subsequence and then determine the subsequence for the other elements.
\nAlternatively, leave out the first element and determine the subsequence for the remaining elements.
\nThe same logic is used in each recursive call until we reach the last index of the provided array.<\/p>\n
In that scenario, we just print the created subsequence and then return to identify the next subsequence.<\/p>\n
Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Let us look at a highly exciting subject called Printing all possible subsequences\/subsets of a given string. There are two options for each element in the specified string: Include the first element in the subsequence and then determine the subsequence for the other elements. Alternatively, leave out the first element and determine the subsequence for …<\/p>\n\n
# Create a recursive function say possible_subsequences() which accepts the\r\n# given string, result string, and index pointing for the given string as\r\n# the arguments and returns all the subsequences or subsets of a given string.\r\n\r\n\r\ndef possible_subsequences(gvn_str, rslt, k):\r\n # Inside the possible_subsequences() function, check if the index argument(k)\r\n # is equal to the given length of the string or not using the if conditional\r\n # statement.\r\n if (k == len(gvn_str)):\r\n # If it is true, then check if the length of the result string is not equal\r\n # to 0 using the if conditional statement.\r\n if (len(rslt) != 0):\r\n # If it is true, then print the result string.\r\n print(rslt)\r\n else:\r\n # Else, Pass the given string, result string, and k+1 for the function itself\r\n # (recursive logic). It excludes the first character.\r\n possible_subsequences(gvn_str, rslt, k+1)\r\n\r\n # Add the given string at the index k to the result string and store it in\r\n # the same variable.It includes the first character.\r\n rslt += gvn_str[k]\r\n # Pass the given string, result string, and k+1 for the function itself\r\n # (recursive logic).\r\n possible_subsequences(gvn_str, rslt, k+1)\r\n # Return.\r\n return\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngvn_str = \"pqr\"\r\nprint(\"The all possible subsequences of a given string are :\")\r\n# Pass the given string, empty string and 1st index(0) to the recursive function\r\n# possible_subsequences().\r\npossible_subsequences(gvn_str, \"\", 0)\r\n# Print the first character of the given string.\r\nprint(gvn_str[0])\r\n<\/pre>\n
The all possible subsequences of a given string are :\r\nr\r\nq\r\nqr\r\np\r\npr\r\npq\r\npqr\r\np<\/pre>\n
Method #2: Using Recursive Function (User Input)<\/h3>\n
\n
# Create a recursive function say possible_subsequences() which accepts the\r\n# given string, result string, and index pointing for the given string as\r\n# the arguments and returns all the subsequences or subsets of a given string.\r\n\r\n\r\ndef possible_subsequences(gvn_str, rslt, k):\r\n # Inside the possible_subsequences() function, check if the index argument(k)\r\n # is equal to the given length of the string or not using the if conditional\r\n # statement.\r\n if (k == len(gvn_str)):\r\n # If it is true, then check if the length of the result string is not equal\r\n # to 0 using the if conditional statement.\r\n if (len(rslt) != 0):\r\n # If it is true, then print the result string.\r\n print(rslt)\r\n else:\r\n # Else, Pass the given string, result string, and k+1 for the function itself\r\n # (recursive logic). It excludes the first character.\r\n possible_subsequences(gvn_str, rslt, k+1)\r\n\r\n # Add the given string at the index k to the result string and store it in\r\n # the same variable.It includes the first character.\r\n rslt += gvn_str[k]\r\n # Pass the given string, result string, and k+1 for the function itself\r\n # (recursive logic).\r\n possible_subsequences(gvn_str, rslt, k+1)\r\n # Return.\r\n return\r\n\r\n\r\n# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\nprint(\"The all possible subsequences of a given string are :\")\r\n# Pass the given string, empty string and 1st index(0) to the recursive function\r\n# possible_subsequences().\r\npossible_subsequences(gvn_str, \"\", 0)\r\n# Print the first character of the given string.\r\nprint(gvn_str[0])\r\n<\/pre>\n
Enter some random string = xyz\r\nThe all possible subsequences of a given string are :\r\nz\r\ny\r\nyz\r\nx\r\nxz\r\nxy\r\nxyz\r\nx<\/pre>\n