user
Task

Display a finite linear combination in an infinite vector basis 

(

e

1
,

e

2
,
…
)
{\displaystyle {\displaystyle (e\_{1},e\_{2},\ldots )}}
.

Write a function that, when given a finite list of scalars 

(

α

1
,

α

2
,
…
)
{\displaystyle {\displaystyle (\alpha ^{1},\alpha ^{2},\ldots )}}
,   
creates a string representing the linear combination 

∑

i

α

i

e

i
{\displaystyle {\displaystyle \sum \_{i}\alpha ^{i}e\_{i}}}
 in an explicit format often used in mathematics, that is:

:   α


    i

    1

    e


    i

    1
    ±

    |

    α


    i

    2

    |

    e


    i

    2
    ±

    |

    α


    i

    3

    |

    e


    i

    3
    ±
    …
    {\displaystyle {\displaystyle \alpha ^{i\_{1}}e\_{i\_{1}}\pm |\alpha ^{i\_{2}}|e\_{i\_{2}}\pm |\alpha ^{i\_{3}}|e\_{i\_{3}}\pm \ldots }}

where 

α


i

k
≠
0
{\displaystyle {\displaystyle \alpha ^{i\_{k}}\neq 0}}

The output must comply to the following rules:

* don't show null terms, unless the whole combination is null.

:   :   :   :   :   :   :   **e(1)**     is fine,     **e(1) + 0\*e(3)**     or     **e(1) + 0**     is wrong.

* don't show scalars when they are equal to one or minus one.

:   :   :   :   :   :   :   **e(3)**     is fine,     **1\*e(3)**     is wrong.

* don't prefix by a minus sign if it follows a preceding term.   Instead you use subtraction.

:   :   :   :   :   :   :   **e(4) - e(5)**     is fine,     **e(4) + -e(5)**     is wrong.

Show here output for the following lists of scalars:

```
 1)    1,  2,  3
 2)    0,  1,  2,  3
 3)    1,  0,  3,  4
 4)    1,  2,  0
 5)    0,  0,  0
 6)    0
 7)    1,  1,  1
 8)   -1, -1, -1
 9)   -1, -2,  0, -3
10)   -1

```
assistant
(formerly Perl 6)

```
sub linear-combination(@coeff) {
    (@coeff Z=> map { "e($_)" }, 1 .. *)
    .grep(+*.key)
    .map({ .key ~ '*' ~ .value })
    .join(' + ')
    .subst('+ -', '- ', :g)
    .subst(/<|w>1\*/, '', :g)
        || '0'
}
 
say linear-combination($_) for 
[1, 2, 3],
[0, 1, 2, 3],
[1, 0, 3, 4],
[1, 2, 0],
[0, 0, 0],
[0],
[1, 1, 1],
[-1, -1, -1],
[-1, -2, 0, -3],
[-1 ]
;

```

Output:

```
e(1) + 2*e(2) + 3*e(3)
e(2) + 2*e(3) + 3*e(4)
e(1) + 3*e(3) + 4*e(4)
e(1) + 2*e(2)
0
0
e(1) + e(2) + e(3)
-e(1) - e(2) - e(3)
-e(1) - 2*e(2) - 3*e(4)
-e(1)
```