---
id: 69c5f3d787b1725d5f00c8bc
title: "Challenge 258: Word Decompressor"
challengeType: 29
dashedName: challenge-258
---

# --description--

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

- The given string is made up of words and numbers separated by spaces.
- Leave the words unchanged.
- Replace numbers with the word at that position, where the first word is at position 1.

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

# --hints--

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

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

`decompress("hello 1 1")` should return `"hello hello hello"`.

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

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

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

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

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

`decompress("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")` should return `"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"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(decompress("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"), "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")`)
}})
```

# --seed--

## --seed-contents--

```py
def decompress(s):

    return s
```

# --solutions--

```py
def decompress(s):
    tokens = s.split(' ')
    words = []
    for token in tokens:
        try:
            idx = int(token)
            words.append(tokens[idx - 1])
        except ValueError:
            words.append(token)
    return ' '.join(words)
```
