Barclays interview question

a beautiful house problems. I use python to solve