Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.6 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69c5f3d787b1725d5f00c8bb Challenge 257: Word Compressor 29 challenge-257

--description--

Given a string, return a compressed version of the string using the following rules:

  • The first occurrence of a word remains unchanged.
  • Subsequent occurrences are replaced with the position of the first occurrence, where the first word is at position 1.
  • Words are separated by a single space.

For example, given "practice makes perfect and perfect practice makes perfect", return "practice makes perfect and 3 1 2 3".

--hints--

compress("practice makes perfect and perfect practice makes perfect") should return "practice makes perfect and 3 1 2 3".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress("practice makes perfect and perfect practice makes perfect"), "practice makes perfect and 3 1 2 3")`)
}})

compress("hello hello hello") should return "hello 1 1".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress("hello hello hello"), "hello 1 1")`)
}})

compress("the cat sat on the mat on which the cat sat") should return "the cat sat on 1 mat 4 which 1 2 3".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress("the cat sat on the mat on which the cat sat"), "the cat sat on 1 mat 4 which 1 2 3")`)
}})

compress("the more you know the more you realize you don't know") should return "the more you know 1 2 3 realize 3 don't 4".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress("the more you know the more you realize you don't know"), "the more you know 1 2 3 realize 3 don't 4")`)
}})

compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero") should return "lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero"), "lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10")`)
}})

--seed--

--seed-contents--

def compress(s):

    return s

--solutions--

def compress(s):
    words = s.split(' ')
    seen = {}
    result = []
    for i, word in enumerate(words):
        if word in seen:
            result.append(str(seen[word]))
        else:
            seen[word] = i + 1
            result.append(word)
    return ' '.join(result)