Solution to Lonely Planet coding exercise