I am taking efforts to solve problem K-th Symbol in Grammar - LeetCode

On the first row, we write a

`0`

. Now in every subsequent row, we look at the previous row and replace each occurrence of`0`

with`01`

, and each occurrence of`1`

with`10`

.`Explanation: row 1: 0 row 2: 01 row 3: 0110 row 4: 01101001`

I wrote such a replace function

```
def replace(row: "List[int]") -> "List[int]":
"""
rtype: row
"""
for i in range(len(row)):
if row[i] == 0: #0 -> 01
row.insert(i+1, 1)
elif row[i] == 1: #1 -> 10
row.insert(i+1, 0)
return row
```

However, it does not work properly.

```
In [3]: r2 = replace([0])
In [4]: r2
Out[4]: [0, 1]
In [5]: r3 = replace(r2); print(r3)
[0, 1, 0, 1] # correct is [0, 1, 1, 0]
In [6]: r4 = replace(r3); print(r4)
[0, 1, 0, 1, 0, 1, 0, 1] #correct is ['0', '1', '1', '0', '1', '0', '0', '1']
```

Use a new list does not work either.

```
def replace(row: "List[int]") -> "List[int]":
"""
rtype: row
"""
copy = list(row)
for i in range(len(copy)):
if copy[i] == 0: #0 -> 01
row.insert(i+1, 1)
elif copy[i] == 1: #1 -> 10
row.insert(i+1, 0)
return row
```

What's the problem?

`replace`

– pLOPeGG Apr 18 at 8:31