Arrange balls

There are p, q and r balls. arrange balls in a straight line such that no two balls of the same type are adjacent.

Hint

Start with any box (let's say P). Remove one ball from the box you started with (P). Choose the next ball from either of the remaining boxes (Q or R). Repeat the steps until only one ball remains. If the last ball must come from a specific box (P, Q, or R), and there are no balls left in the other boxes, that's the only choice.

# Python implementation
def arrange(p, q, r, pick):
  if (p < 0 or q < 0 or r < 0):
    return 0

  if (
    (pick=='p' and p==1 and q==0 and r==0) or
    (pick=='q' and p==0 and q==1 and r==0) or
    (pick=='r' and p==0 and q==0 and r==1)):
    return 1

  if pick == 'p':
    return arrange(p-1, q, r, 'q') + arrange(p-1, q, r, 'r')
  elif pick == 'q':
    return arrange(p, q-1, r, 'p') + arrange(p, q-1, r, 'r')
  elif pick == 'r':
    return arrange(p, q, r-1, 'p') + arrange(p, q, r-1, 'q')

p = 1
q = 1
r = 0

print(arrange(p, q, r, 'p') + arrange(p, q, r, 'q') + arrange(p, q, r, 'r'))
// Javascript implementation
function arrange(p, q, r, from) {
  if (p < 0 || q < 0 || r < 0) {
    return 0;
  }

  if (
    (from=='p' && p==1 && q==0 && r==0) ||
    (from=='q' && p==0 && q==1 && r==0) || 
    (from=='r' && p==0 && q==0 && r==1)) {
    return 1;
  }

  switch(from) {
    case 'p':
      return arrange(p-1, q, r, 'q') + arrange(p-1, q, r, 'r');
    case 'q':
      return arrange(p, q-1, r, 'p') + arrange(p, q-1, r, 'r');
    case 'r':
      return arrange(p, q, r-1, 'p') + arrange(p, q, r-1, 'q');
  }
}

const p = 1;
const q = 1;
const r = 0;

console.log(arrange(p, q, r, 'p') + arrange(p, q, r, 'q') + arrange(p, q, r, 'r'));