Finished code for complete function. Tested against example cases provided by coursera.