Difference between revisions of "W1349 Higher Order Functions"
From Coder Merlin
Line 445: | Line 445: | ||
- C | - C | ||
- D | - D | ||
{ | |||
Parseltongue uses words ordered in V-S-O order, that is, Verb-Subject-Object, similar to Biblical Hebrew and Classical Arabic. Salazar Slytherin wrote a function to validate Parseltoungue, assuming that all sentences have exactly three words. He used "V" to symbolize a verb, "S" to symbolize a subject, and "O" to symbolize an object. The function will return true if and only if the array symbolized valid Parseltoungue, that is, every sentence specified is valid. Unfortunately, the code was lost in the Chamber of Secrets. Which of the following functions would perform as Salazar Slytherin intended? | |||
An example array is: | |||
<syntaxhighlight lang="swift"> | |||
let parseltoungue = ["V", "S", "O", "O", "S", "V", "V", "S", "O", "V", "S", "O", "V", "S", "O"] | |||
</syntaxhighlight> | |||
In this example, four out of five sentences are valid Parseltoungue. In this case, the function would return false, because one of the five sentences isn't valid. | |||
You may assume that all arrays provided as input will always have a multiple of three symbols.<br/> | |||
'''A''' | |||
<syntaxhighlight lang="swift"> | |||
func isValidParselToungue(maybeParselToungue:[String]) -> Bool { | |||
for i in stride(from:0, to:maybeParselToungue.count, by:3) { | |||
if (maybeParselToungue[i+0] == "V" && | |||
maybeParselToungue[i+1] == "S" && | |||
maybeParselToungue[i+2] == "O") { | |||
} else { | |||
return false | |||
} // | |||
} // | |||
return true | |||
} // | |||
</syntaxhighlight> | |||
'''B''' | |||
<syntaxhighlight lang="swift"> | |||
func isValidParselToungue(maybeParselToungue:[String]) -> Bool { | |||
for i in stride(from:0, to:maybeParselToungue.count, by:3) { | |||
if (maybeParselToungue[i+0] == "V" && | |||
maybeParselToungue[i+1] == "S" && | |||
maybeParselToungue[i+2] == "O") { | |||
} else { | |||
return true | |||
} // | |||
} // | |||
return false | |||
} // | |||
</syntaxhighlight> | |||
'''C''' | |||
<syntaxhighlight lang="swift"> | |||
func isValidParselToungue(maybeParselToungue:[String]) -> Bool { | |||
let verbs = maybeParselToungue.filter{$0 == "V"}.count | |||
let subjects = maybeParselToungue.filter{$0 == "S"}.count | |||
let objects = maybeParselToungue.filter{$0 == "O"}.count | |||
return verbs == subjects && subjects == objects | |||
} // | |||
</syntaxhighlight> | |||
'''D''' | |||
<syntaxhighlight lang="swift"> | |||
func isValidParselToungue(maybeParselToungue:[String]) -> Bool { | |||
for e in maybeParselToungue { | |||
if e == "V" && e + 1 == "S" && e + 2 == "O" { | |||
return true | |||
} else { | |||
return false | |||
} // | |||
} // | |||
} // | |||
</syntaxhighlight> | |||
|type="[]"} | |||
+ A | |||
- B | |||
- C | |||
- D | |||
{ | |||
Albus Dumbledore has recently become suspicious that the Sorting Hat was unfairly sorting students into the four houses of Gryffindor, Ravenclaw, Hufflepuff or Slytherin. He's entered all of the sorting that occurred at the beginning of the last school year into an array, using the initial letter of each house as an abbreviation. | |||
<syntaxhighlight lang="swift"> | |||
let recentSort = ["G", "R", "H", "S", "S", "S", "S", "G", "G", "R", "H", "H", | |||
"R", "S", "G", "G", "H", "R", "R", "S", "G", "R", "R", "H"] | |||
</syntaxhighlight> | |||
Which of the following code segments can be used to determine the percentage of students assigned to Gryffindor?<br/> | |||
'''A''' | |||
<syntaxhighlight lang="swift"> | |||
var percentageGryffindor = 100.0 * Double(recentSort.filter {$0 == "G"}.count) / Double(recentSort.count) | |||
</syntaxhighlight> | |||
'''B''' | |||
<syntaxhighlight lang="swift"> | |||
var percentageGryffindor = 100.0 * recentSort.filter {$0 == "G"} / Double(recentSort.count) | |||
</syntaxhighlight> | |||
'''C''' | |||
<syntaxhighlight lang="swift"> | |||
let isGryffindor = {(letter:String) -> Double in if letter == "G" {return 1.0} else {return 0.0}} | |||
var percentageGryffindor = 100.0 * recentSort.map (isGryffindor).reduce(0.0, +) / Double(recentSort.count) | |||
</syntaxhighlight> | |||
'''D''' | |||
<syntaxhighlight lang="swift"> | |||
let isGryffindor = {(letter:String) -> Bool in return letter == "G"} // | |||
var percentageGryffindor = 100.0 * Double(recentSort.filter (isGryffindor).count) / Double(recentSort.count) | |||
</syntaxhighlight> | |||
|type="[]"} | |||
+ A | |||
- B | |||
+ C | |||
+ D | |||
</quiz> | </quiz> |
Revision as of 15:15, 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.