JPMorganChase interview question

Write a python program for the task below