IBM Interview Question

Solving a DP question in python