A solution for r/dailyprogrammer challenge 119 [intermediate]