Complete code for my answers posted on Stack Exchange.