You are viewing a single comment's thread. Return to all comments →
Without itertools using sliding window concept.
from collections import defaultdict strings = str(input()) dicts = defaultdict() answers = [] left = 0 right = 1 if len(strings) == 1: print(f'({1}, {int(strings)})') while(right < len(strings)): if strings[left] == strings[right]: if strings[left] not in dicts.keys(): dicts[strings[left]] = 2 else: dicts[strings[left]] += 1 if right == len(strings) - 1: tuples = tuple([dicts[strings[left]],int(strings[left])]) answers.append(tuples) dicts = defaultdict() else: if strings[left] not in dicts.keys(): dicts[strings[left]] = 1 tuples = tuple([dicts[strings[left]],int(strings[left])]) answers.append(tuples) dicts = defaultdict() dicts[strings[right]] = 1 left = right if right == len(strings) - 1: dicts[strings[left]] = 1 tuples = tuple([dicts[strings[left]],int(strings[left])]) answers.append(tuples) right += 1 print(' '.join(f'({x}, {y})' for x,y in answers))
Seems like cookies are disabled on this browser, please enable them to open this website
Compress the String!
You are viewing a single comment's thread. Return to all comments →
Without itertools using sliding window concept.