Reddit Dailyprogrammer Challenge #249

I have not previously posted my solutions to the Reddit Daily Programmer (For learning, refreshing, or just for fun!). I figure it is a good idea to practice my writing skills as well as my programming skills. Great excuse to use programming languages you are less proficient in.

This challenge, Challenge #249 [Easy] Playing the Stock Market, I solved in C++11 (using lamdas). The goal is to figure out when to buy stock and when to sell them to gain the most profit, given a string of space separated floating-point numbers. I tried solving it using more STL than I am used to and I am pleased with the result. This was also the first time I had used any of the string stream functions - which was interesting to learn about.

./playing-the-stock-market

Enter a list of space separated decimal floats:

9.20 8.03 10.02 8.08 8.14 8.10 8.31 8.28 8.35 8.34 8.39 8.45 8.38 8.38 8.32 8.36 8.28 8.28 8.38 8.48 8.49 8.54 8.73 8.72 8.76 8.74 8.87 8.82 8.81 8.82 8.85 8.85 8.86 8.63 8.70 8.68 8.72 8.77 8.69 8.65 8.70 8.98 8.98 8.87 8.71 9.17 9.34 9.28 8.98 9.02 9.16 9.15 9.07 9.14 9.13 9.10 9.16 9.06 9.10 9.15 9.11 8.72 8.86 8.83 8.70 8.69 8.73 8.73 8.67 8.70 8.69 8.81 8.82 8.83 8.91 8.80 8.97 8.86 8.81 8.87 8.82 8.78 8.82 8.77 8.54 8.32 8.33 8.32 8.51 8.53 8.52 8.41 8.55 8.31 8.38 8.34 8.34 8.19 8.17 8.16

Outputs the following expected response from the challenge.

buy at 8.03 - sell at 9.34

The source code is a bit too long to post here but is available here.