curriculum/challenges/english/blocks/daily-coding-challenges-python/69162d64f96574d9bb629f04.md
Given a string sentence, return a compressed version of the sentence where consecutive duplicate words are replaced by the word followed with the number of times it repeats in parentheses.
For example, given "yes yes yes please", return "yes(3) please".
compress_string("yes yes yes please") should return "yes(3) please".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress_string("yes yes yes please"), "yes(3) please")`)
}})
compress_string("I have have have apples") should return "I have(3) apples".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress_string("I have have have apples"), "I have(3) apples")`)
}})
compress_string("one one three and to the the the the") should return "one(2) three and to the(4)".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress_string("one one three and to the the the the"), "one(2) three and to the(4)")`)
}})
compress_string("route route route route route route tee tee tee tee tee tee") should return "route(6) tee(6)".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress_string("route route route route route route tee tee tee tee tee tee"), "route(6) tee(6)")`)
}})
def compress_string(sentence):
return sentence
def compress_string(sentence):
words = sentence.split(" ")
result = []
count = 1
for i in range(len(words)):
if i < len(words) - 1 and words[i] == words[i + 1]:
count += 1
else:
if count > 1:
result.append(f"{words[i]}({count})")
else:
result.append(words[i])
count = 1
return " ".join(result)