Solution (source code)

= Solution

The Shannon-Fano lengths are
$$
(\ell_1,\ell_2,\ell_3,\ell_4)=(4,3,2,2).
$$
One corresponding prefix-free assignment is
$$
\boxed{1\mapsto1010,\quad
2\mapsto100,\quad
3\mapsto00,\quad
4\mapsto01}.
$$
Its expected word length is
$$
\boxed{\overline\ell_{\rm SF}
=\frac1{10}4+\frac2{10}3+\frac3{10}2+\frac4{10}2
=2.4}.
$$