r<\/em><\/strong> length subsequences of elements from the input iterable.<\/p>\n\n\n\nCombinations are emitted in lexicographic sorted order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.<\/p>\n\n\n\n
Sample Code<\/strong><\/p>\n\n\n\n>>> from itertools import combinations\n>>> \n>>> print list(combinations('12345',2))\n[('1', '2'), ('1', '3'), ('1', '4'), ('1', '5'), ('2', '3'), ('2', '4'), ('2', '5'), ('3', '4'), ('3', '5'), ('4', '5')]\n>>> \n>>> A = [1,1,3,3,3]\n>>> print list(combinations(A,4))\n[(1, 1, 3, 3), (1, 1, 3, 3), (1, 1, 3, 3), (1, 3, 3, 3), (1, 3, 3, 3)]\n<\/code><\/pre>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nYou are given a string S<\/em><\/strong>.
Your task is to print all possible combinations, up to size k<\/em><\/strong>, of the string in lexicographic sorted order.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nA single line containing the string S<\/em><\/strong> and integer value k<\/em><\/strong> separated by a space.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 0 < k<\/em> < len<\/em>(S<\/em>)<\/strong><\/li>
- The string contains only UPPERCASE<\/em> characters.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the different combinations of string S<\/em><\/strong> on separate lines.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\nHACK 2<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nA\nC\nH\nK\nAC\nAH\nAK\nCH\nCK\nHK<\/code><\/pre>\n\n\n\n<\/span>Solution – Itertools.combinations() in Python<\/strong><\/span><\/h2>\n\n\n\n# Enter your code here. Read input from STDIN. Print output to STDOUT\nfrom itertools import combinations\n\ns , n = input().split()\nfor i in range(1, int(n)+1):\n for j in combinations(sorted(s), i):\n print (''.join(j))\n<\/pre>\n\n\n\n