Barclays Interview Question

a beautiful house problems. I use python to solve