Solution (source code)

= Solution

The <classification of compact surfaces> says that every <compact>, <connected> <topological surface> without <boundary> is <homeomorphic> to exactly one of the following: the <sphere> $S^2$; a <connected sum of surfaces> consisting of $g\geq1$ <tori>; or a <connected sum of surfaces> consisting of $k\geq1$ <real projective planes>. The first two cases are <orientable>; the last case is a <nonorientable surface>. Their <Euler characteristics> are
$$
\boxed{\chi(S^2)=2,\qquad\chi(\Sigma_g)=2-2g,\qquad\chi(N_k)=2-k.}
$$
Here $\Sigma_g$ has <genus> $g$, and $N_k$ has $k$ crosscaps; $N_1$ is the <real projective plane> and $N_2$ the <Klein bottle>. Thus <orientability> together with the <Euler characteristic> determines the closed <surface> up to <homeomorphism>.

If the convention permits a <surface with boundary>, also remove the interiors of $b\geq0$ disjoint <closed discs> from one of these closed <surfaces>. The <Euler characteristic> decreases by $b$, giving $2-2g-b$ in the <orientable> case and $2-k-b$ in the <nonorientable surface> case. The number of <boundary> components is then an additional invariant.