JPMorganChase Interview Question

Write a python program for the task below