Merge the Tools in python - HackerRank Solution
Problem Statement :
Consider the following:
1. A string, s, of length where s=c0,c1.....cn-1.
2. An integer, k, where k is a factor of n.
We can split s into n/k subsegments where each subsegment, ti, consists of a contiguous block of k characters in s. Then, use each ti to create string ui such that:
1. The characters in ui are a subsequence of the characters in ti.
2. Any repeat occurrence of a character is removed from the string such that each character in ui
occurs exactly once. In other words, if the character at some index j in ti occurs at a previous
index <j in ti, then do not include the character in string ui.
Given s and k, print n/k lines where each line denotes string ui.
Input Format:
The first line contains a single string denoting s.
The second line contains an integer, k, denoting the length of each subsegment.
Constraints:
1. 1<=n<=10^4
2. 1<=k<=n
3. It is guaranteed that n is a multiple of k.
Output Format:
Print n/k lines where each line i contains string ui.
Solution:
# the complete code as required for hackerrank challenge
def merge_the_tools(string, k):
split_string=(string[i:i+k] for i in range(0,len(string),k))
for i in split_string:
u=[]
u.append(i[0])
for j in range(1,len(i)):
if i[j] in u:
continue
else:
u.append(i[j])
print(''.join(str(e) for e in u))
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)
Disclaimer:-