Difference between revisions of "W1349 Higher Order Functions"
From Coder Merlin
Line 520: | Line 520: | ||
'''A''' | '''A''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let percentageGryffindor = 100.0 * Double(recentSort.filter {$0 == "G"}.count) / Double(recentSort.count) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''B''' | '''B''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let percentageGryffindor = 100.0 * recentSort.filter {$0 == "G"} / Double(recentSort.count) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''C''' | '''C''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let isGryffindor = {(letter:String) -> Double in if letter == "G" {return 1.0} else {return 0.0}} | let isGryffindor = {(letter:String) -> Double in if letter == "G" {return 1.0} else {return 0.0}} | ||
let percentageGryffindor = 100.0 * recentSort.map (isGryffindor).reduce(0.0, +) / Double(recentSort.count) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
'''D''' | '''D''' | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let isGryffindor = {(letter:String) -> Bool in return letter == "G"} // | let isGryffindor = {(letter:String) -> Bool in return letter == "G"} // | ||
let percentageGryffindor = 100.0 * Double(recentSort.filter (isGryffindor).count) / Double(recentSort.count) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
|type="[]"} | |type="[]"} |
Revision as of 16:35, 24 March 2019
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Exercises[edit]
Professor Snape needs your help to organize ingredients for a wide variety of potions. You'll need to verify blocks of code to ensure that they're doing exactly what Professor Snape needs. Be careful! Any mistakes can be hazardous to his health. In some cases, more than one answer will be correct.