Alice decides to invite 14 friends to an unbirthday party later this month. Unfortunately she discovers that because of their busy schedules she can't have them all over on the same day. In fact she will need to have at least 3 separate unbirthday parties in order that everyone can attend. (Since she has at least 364 unbirthdays a year, this isn't a big problem, but on the other hand she doesn't want to throw more unbirthday parties than necessary, at least not until next month.) So she decides to have 3 unbirthday parties. She'll have to divide her set $S$ of 14 friends into 3 subsets $S_0$, $S_1$, and $S_2$ with the following properties: (a) No one can be missed, so $S_0 \cup S_1 \cup S_2 = S$. And (b) no one should be invited twice, so $S_0 \cap S_1 = \emptyset$, $S_1 \cap S_2 = \emptyset$, and $S_1 \cap S_2 = \emptyset$. Any set of three sets $\{S_0, S_1, S_2\}$ with these two properties is called a partition of $S$ of size 3. We have one more requirement, none of the 3 subsets should be empty -- since a party with no guests is hardly a party at all. Alice wonders how many ways she can partition her 14 friends into 3 nonempty subsets.

The partitions in which Bill is not alone can be obtained by adding Bill to one of the 3 sets in a partition of the remaining 13 friends. So each solution to partitioning 13 friends into 3 sets yields three partions of the set of 14 friends into 3 partitions. For example the partition:
$$ \{ \{C, D, E, F, G\}, \{H, I, J, K, L\}, \{M, N, P\} \} $$
yields three different ways of partitioning the set of 14 friends
$$
\{ \{B, C, D, E, F, G\}, \{H, I, J, K, L\}, \{M, N, P\} \}\;\text{,}
\\ \{ \{C, D, E, F, G\}, \{B, H, I, J, K, L\}, \{M, N, P\} \}\;\text{, and}
\\ \{ \{C, D, E, F, G\}, \{H, I, J, K, L\}, \{B, M, N, P\} \}
$$
The three partitions generated are different from each other and also from any partitions generated in a similar way from a different partition of the remaining 13. And they are different from any partitions generated using the method in the previous paragraph. [You should stop and think about why this is true.]

The cases ($k=0$ and $k=n$) that aren't dealt with by this rule can be dealt with easily enough.

Consider a set $S$ of size $0$. There is one partition of size 0, the empty set.
$$
countPartitions(0, 0) = 1
$$
For a set of size $n$, with $n>0$, there are no partitions of size 0
$$
countPartitions(n, 0) = 0\text{ for all } n > 0
$$
there is one partition namely of size $n$, namely the partition that puts each member of $S$ on its own.
$$
countPartitions(n, n) = 1\text{ for all } n > 0
$$
The first rule and the third can above be combined, so altogether we have three properties

defcountPartitionsMine(n,k):""" Pre: k and n are integers such that 0 <= k and k <= n Post: result == the number of ways to partition a set of size n into k nonempty subsets. """assertisinstance(n,int)andn>=0assertisinstance(k,int)and0<=kandk<=nifk==n:return1elifk==0:return0else:returncountPartitionsMine(n-1,k-1)+k*countPartitionsMine(n-1,k)

As with combinations, the straightforward recursive solution that I've used is very inefficient. I'm going to ignore this problem for now. The point of these exercises is to get comfortable with creating correct recursive solutions without worrying about efficiency. Later we'll come back to the problem of making this solution efficient by using a technique called "memoization".