<\/span><\/h2>\n\n\n\nConsider the following:<\/p>\n\n\n\n
A string, s<\/em><\/strong>, of length n<\/em><\/strong> where s = c0c1. . . . cn-1<\/strong>.<\/p>\n\n\n\nAn integer, k<\/em><\/strong>, where k<\/em><\/strong> is a factor of n<\/em><\/strong>.<\/p>\n\n\n\nWe can split s<\/em><\/strong> into n\/k<\/strong> substrings where each subtring, ti<\/strong>, consists of a contiguous block of k<\/em><\/strong> characters in s<\/strong><\/em>. Then, use each ti<\/em><\/strong> to create string ui<\/em><\/strong> such that:<\/p>\n\n\n\nThe characters in ui<\/em><\/strong> are a subsequence of the characters in ti<\/em><\/strong>.<\/p>\n\n\n\nAny repeat occurrence of a character is removed from the string such that each character in ui<\/em><\/strong> occurs exactly once. In other words, if the character at some index j<\/em><\/strong> in ti<\/em><\/strong> occurs at a previous index <<\/strong> j<\/em><\/strong> in ti<\/strong><\/em>, then do not include the character in string ui<\/strong><\/em>.<\/p>\n\n\n\nGiven s<\/em><\/strong> and k<\/em><\/strong>, print n\/k<\/strong> lines where each line i<\/em><\/strong> denotes string ui<\/em><\/strong>.<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\ns<\/em> = “AAABCADDE”<\/strong><\/p>\n\n\n\nk<\/em> = 3<\/strong><\/p>\n\n\n\nThere are three substrings of length 3<\/strong> to consider: ‘AAA’, ‘BCA’ and ‘DDE’. The first substring is all ‘A’ characters, so u1 = ‘A’<\/strong>. The second substring has all distinct characters, so u2 <\/em>= ‘BCA’<\/strong>. The third substring has 2<\/strong> different characters, so u3 = ‘DE’<\/strong>. Note that a subsequence maintains the original order of characters encountered. The order of characters in each subsequence shown is important.<\/p>\n\n\n\n<\/span>Function Description <\/strong><\/span><\/h3>\n\n\n\nComplete the merge_the_tools function in the editor below.<\/p>\n\n\n\n
merge_the_tools has the following parameters:<\/p>\n\n\n\n
- string s: the string to analyze<\/li>
- int k: the size of substrings to analyze<\/li><\/ul>\n\n\n\n
<\/span>Prints<\/strong><\/span><\/h3>\n\n\n\nPrint each subsequence on a new line. There will be n\/k<\/strong> of them. No return value is expected.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains a single string, s<\/em><\/strong>.<\/p>\n\n\n\nThe second line contains an integer, k<\/em><\/strong>, the length of each substring.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= n <= 10^4, <\/strong>where n<\/strong> is the length of s<\/strong><\/li>
- 1 <= k <= n <\/strong><\/li>
- It is guaranteed that n<\/strong> is a multiple of k<\/em><\/strong>.<\/li><\/ul>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nSTDIN Function\n----- --------\nAABCAAADA s = 'AABCAAADA'\n3 k = 3<\/pre>\n\n\n\nSample Output <\/strong><\/p>\n\n\n\nAB\nCA\nAD<\/pre>\n\n\n\nExplanation <\/strong><\/p>\n\n\n\nSplit s<\/strong> into n\/k = 9\/3 = 3<\/strong> equal parts of length k<\/em> = 3<\/strong>. Convert each ti<\/strong><\/em> to ui<\/strong><\/em> by removing any subsequent occurrences of non-distinct characters in ti<\/em><\/strong>:<\/p>\n\n\n\n- t0 = “AAB” – u0 = “AB”<\/strong><\/li>
- t1 = “CAA” – u1 = “CA”<\/strong><\/li>
- t2 = “ADA” – u2 = “AD”<\/strong><\/li><\/ol>\n\n\n\n
Print each ui<\/em><\/strong> on a new line.<\/p>\n\n\n\n<\/span>Solution – Merge The Tools in Python – Hacker Rank Solution<\/strong><\/span><\/h2>\n\n\n\ndef merge_the_tools(string, k):\n # your code goes here\n temp = []\n len_temp = 0\n for item in string:\n len_temp += 1\n if item not in temp:\n temp.append(item)\n if len_temp == k:\n print (''.join(temp))\n temp = []\n len_temp = 0\nif __name__ == '__main__':\n string, k = input(), int(input())\n merge_the_tools(string, k)<\/pre>\n\n\n\n