IBM interview question

Solving a DP question in python