Probability theory combinatoric problem 
A total of $n$ bar magnets are placed end to end in a line with random
  independent orientations. Adjacent ends with equal polarities repel
  each other, and adjacent ends with opposite polarities join to form
  blocks. Let $X$ be the number of blocks of joined magnets. Find $\text{E}(X)$
  and $\text{Var}(X)$.

The obvious method to solve this problem is to find the pmf of $X$ and then simply use the definitions of the expectancy and variance. I'm however a bit stuck at the first line of this reasoning, i.e. how to find the pmf for $X$.
It feels like somehow a binomial distribution should be involved but the problem is is that there are many ways of creating — for example — two or five blocks out of $n$ magnets, each of which models a binomial distribution. How do I derive a reasonably "clean" pmf for $X$?
 A: Let $0$ represent one orientation and $1$ represent the other orientation. Each arrangement of the $n$ magnets then corresponds to an $n$-bit binary string. Two adjacent magnets repel if they are in opposite orientations and attract if they are in the same orientation. Thus, a block of magnets corresponds exactly to a block of identical bits. For $n=8$, for instance, the string $00111011$ has $4$ blocks: $00$, $111$, $0$, and $11$. The number of blocks is therefore one more than the total number of $01$ and $10$ transitions.
Suppose that an $n$-bit string has $k$ transitions. There are $\binom{n-1}k$ ways to choose where they occur, and the first bit can be either $0$ or $1$, so there are $2\binom{n-1}k$ such strings. Thus, 
$$\Bbb P(X=k)=\frac{2\binom{n-1}{k-1}}{2^n}=\frac1{2^{n-1}}\binom{n-1}{k-1}\;,$$
and
$$\begin{align*}
\Bbb E(X)&=\frac1{2^{n-1}}\sum_{k=1}^nk\binom{n-1}{k-1}\\\\
&=\frac1{2^{n-1}}\sum_{k=0}^{n-1}(k+1)\binom{n-1}k\tag{1}\\\\
&=\frac1{2^{n-1}}\left(\sum_{k=0}^{n-1}k\binom{n-1}k+\sum_{k=0}^{n-1}\binom{n-1}k\right)\;.
\end{align*}$$
Now observe that $k\dbinom{n-1}k=(n-1)\dbinom{n-2}{k-1}$ and simplify.
Added: The variance is $\Bbb E(X^2)-\big(\Bbb E(X)\big)^2$, so we also need to calculate
$$\begin{align*}
\frac1{2^{n-1}}\sum_{k=1}^nk^2\binom{n-1}{k-1}&=\frac1{2^{n-1}}\sum_{k=0}^{n-1}(k+1)^2\binom{n-1}k\\\\
&=\frac1{2^{n-1}}\sum_{k=0}^{n-1}(k^2+2k+1)\binom{n-1}k\\\\
&=\frac1{2^{n-1}}\left(\sum_{k=0}^{n-1}k^2\binom{n-1}k+2\sum_{k=0}^{n-1}k\binom{n-1}k+\sum_{k=0}^{n-1}\binom{n-1}k\right)\tag{2}
\end{align*}$$
We’ve already dealt with the last two summations in $(2)$, so all that remains is to deal with
$$\sum_{k=0}^{n-1}k^2\binom{n-1}k=(n-1)\sum_{k=0}^{n-1}k\binom{n-2}{k-1}=(n-1)\sum_{k=0}^{n-2}(k+1)\binom{n-2}k\;.\tag{3}$$
The last summation in $(3)$ is essentially just another instance of the summation in $(1)$.
A: 
The obvious method to solve this problem is to find the pmf of $X$...

This problem illustrates perfectly that the "obvious method" might not be the easiest. 
Here we can find  $\mathbb{E}(X)$ and $\mathbb{V}(X)$ without working out the pmf of $X$. 
Instead let's  represent the random variable $X$ with indicator random variables. 
For $i=1,\dots, n-1$ define $Z_i$ to be one if magnets $i$ and $i+1$ repel, 
and zero if they attract. Then the $Z_i$s are independent, and the 
number of blocks satisfies $X=1+\sum_{i=1}^{n-1} Z_i.$
Since $\mathbb{E}(Z_i)=1/2$ and $\mathbb{V}(Z_i)=1/4$ we get 
$$\mathbb{E}(X)=1+(n-1)/2={n+1\over 2}\qquad\mbox{and}\qquad  \mathbb{V}(X)={n-1\over 4}.$$
We do not need to calculate any probabilities! This trick is worth remembering,
 and likely what user JiK had in mind.
A: The number of blocks is one plus the number of adjacent ends with equal polarities. Start reading the magnets from the left. What is the probability that the $k+1$th magnet is oriented so that the $k$th and the $k+1$th magnet repel each other, given the orientations of the first $k$ magnets?
