Difference between revisions of "W1349 Higher Order Functions"
From Coder Merlin
Line 303: | Line 303: | ||
} // | } // | ||
} // | } // | ||
</syntaxhighlight> | |||
|type="[]"} | |||
+ A | |||
+ B | |||
+ C | |||
- D | |||
{ | |||
Bubotuber pus is the liquid found in the swellings of the magical Bubotuber plant. It is very valuable for its acne-ridding qualities. It is a thick, yellowish-green liquid and smells strongly of petrol. Combining two drops of Bubotuber pus increases the efficacy of all other ingredients by 159%. Professor Snape has an array of efficacies of several ingredients and he knows that he can calculate the new efficacies as follows: | |||
<syntaxhighlight lang="swift"> | |||
let efficacies = [124.3, 157.9, 2.4, 15.9, 108.8, 16.5, 74.3] | |||
let adjuster = {(originalEfficacy:Double) -> Double in return originalEfficacy * 1.59} // | |||
var adjustedEfficacies = efficacies.map(adjuster) | |||
</syntaxhighlight> | |||
Which of the following produce identical results?<br/> | |||
'''A''' | |||
<syntaxhighlight lang="swift"> | |||
var adjustedEfficacies = efficacies.map {$0 * 1.59} // | |||
</syntaxhighlight> | |||
'''B''' | |||
<syntaxhighlight lang="swift"> | |||
var adjustedEfficacies = efficacies.map {$1 * 1.59} // | |||
</syntaxhighlight> | |||
'''C''' | |||
<syntaxhighlight lang="swift"> | |||
var adjustedEfficacies = [Double]() | |||
for efficacy in efficacies { | |||
adjustedEfficacies.append(efficacy * 1.59) | |||
} // | |||
</syntaxhighlight> | |||
'''D''' | |||
<syntaxhighlight lang="swift"> | |||
var adjustedEfficacies = [Double]() | |||
for i in 0 ..< efficacies.count { | |||
adjustedEfficacies.append(Double(i) * 1.59) | |||
} // | |||
</syntaxhighlight> | |||
|type="[]"} | |||
+ A | |||
- B | |||
+ C | |||
- D | |||
{ | |||
As is well known: | |||
* 17 sickles = 1 galleon | |||
* 29 knuts = 1 sickle | |||
Professor Snape has a list of prices for potion ingredients in knuts and wants to convert these prices to fractional galleons and then determine the total price for all ingredients. He knows that one way to do this is: | |||
<syntaxhighlight lang="swift"> | |||
let pricesInKnuts = [498.0, 398.0, 943.0, 1094.0, 1523.0, 327.0, 105.0] | |||
let knutsPerSickle = 29.0 | |||
let sicklesPerGalleon = 17.0 | |||
var totalPriceInGalleons = 0.0 | |||
for priceInKnuts in pricesInKnuts { | |||
let priceInSickles = priceInKnuts / knutsPerSickle | |||
let priceInGalleons = priceInSickles / sicklesPerGalleon | |||
totalPriceInGalleons += priceInGalleons | |||
} // | |||
</syntaxhighlight> | |||
Which of the following produce identical results?<br/> | |||
'''A''' | |||
<syntaxhighlight lang="swift"> | |||
var totalPriceInGalleons = 0.0 | |||
for priceInKnuts in pricesInKnuts { | |||
totalPriceInGalleons += priceInKnuts / knutsPerSickle / sicklesPerGalleon | |||
} // | |||
</syntaxhighlight> | |||
'''B''' | |||
<syntaxhighlight lang="swift"> | |||
var pricesInSickles = [Double]() | |||
for priceInKnuts in pricesInKnuts { | |||
pricesInSickles.append(priceInKnuts / knutsPerSickle) | |||
} // | |||
var pricesInGalleons = [Double]() | |||
for priceInSickles in pricesInSickles { | |||
pricesInGalleons.append(priceInSickles / sicklesPerGalleon) | |||
} // | |||
var totalPriceInGalleons = 0.0 | |||
for priceInGalleons in pricesInGalleons { | |||
totalPriceInGalleons += priceInGalleons | |||
} // | |||
</syntaxhighlight> | |||
'''C''' | |||
<syntaxhighlight lang="swift"> | |||
var totalPriceInGalleons = pricesInKnuts.map {$0 / knutsPerSickle}.map {$0 / sicklesPerGalleon}.reduce(0.0, +) | |||
</syntaxhighlight> | |||
'''D''' | |||
<syntaxhighlight lang="swift"> | |||
var totalPriceInGalleons = pricesInKnuts.filter {$0 > knutsPerSickle}.filter {$0 > sicklesPerGalleon}.reduce(0.0, +) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
|type="[]"} | |type="[]"} |
Revision as of 14:59, 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.