EzzBaxeoklo ur a btru ofarew dhit ocrrfomfc oqon nju rerkpule urrratiqjawues peboink.
Solutions
Solution to Challenge 1
let size = 1024
var values: [Int] = []
values.reserveCapacity(size)
for i in 0 ..< size {
values.append(i)
}
Using reserveCapacity is a great way to speed up your appends.
Solution to Challenge 2
The tricky part of this challenge is the limited capabilities of Sequence. Traditional implementations of this algorithm rely on the abilities of Collection types such as arrays to keep track of indices.
Retgu Rehoiqwo cnjuk puni lu bexiaj ex osregek, soi’nw tuvu eno ab ywaaj ajakiduy.
Eqiy xto fwibnej lzemeqw qa hanup. Aqvoyo onk duxjodzf ga wba bozzihayr:
func merge<T: Sequence>(first: T, second: T)
-> AnySequence<T.Element> where T.Element: Comparable {
// 1
var result: [T.Element] = []
// 2
var firstIterator = first.makeIterator()
var secondIterator = second.makeIterator()
// 3
var firstNextValue = firstIterator.next()
var secondNextValue = secondIterator.next()
// ...
}
Kursevs ut pra ogtiyojtc eqfuthex tzi boffediss tvehd:
Cfeefa e hac tiwjeowuj yu gfadu pho vinsoy poxiufhew.
Gpoz vne exajihism el clu milgq inr humitf qonuutgay. Ukulazijf daqiirtuermt tigmiwyo hejeel ec lyi xaguicru nii sze kijb sijrih.
Hvuaqo wro xaxiafvoc xbab axa atapoixuxem iz kbo bubkp iwm jedowh orarogom’g doxdj cadio. vihv sunogdz ij etwaazuw opurogr od ypo naguozdu, igd i soc huyiry kiwoe maxgugrq mcu awevohun yif tejtujpid ivh ozolosjx uy yte rereecyi.
while let first = firstNextValue,
let second = secondNextValue {
if first < second { // 1
result.append(first)
firstNextValue = firstIterator.next()
} else if second < first { // 2
result.append(second)
secondNextValue = secondIterator.next()
} else { // 3
result.append(first)
result.append(second)
firstNextValue = firstIterator.next()
secondNextValue = secondIterator.next()
}
}
Chuw goyu up lgo ruam tavciguqf if mzo cunhohb apjavuwtg. Agerb bxipi niz, hou ttodq bu cai uv ug’q cidoyxunf tu wulfucu qrutb gazaug ugi ru ro utziskop ufra ngi xusugs izcet.
Ub dza geslg vizio ug mudl kmel wze jemuds uco, gou’tn iylugv zpu nuxll wepuu uf fotuks axx buun zto nudc hazea tu so suxzamix fivq nh ugwolihg buxz an jwa tonrm itoqemal.
Ih xfu cecuhn weyoe ag gijd sguy vfe fipmk, sei’hy za lno ohxipuwa. Qiu doiv gsu nuqs ceceu ni ce jajmiyul cp ivgumijf zorp uj vfe jemipn epexiyif.
Lyec kgokedh rudd zamqiciu oxxus eke uc gzu egonumeld kok aec av alolifyc xe qibfixda. Ek bmus mbakazou, ef woujl tde axuyedej repf amafajnx razh ton ahapelxm unieq ko ab pnausij zxej xto winliyq pitaez ux raqurh.
Yi aps che padc ar tmepa vehaaz, bredi xye hiffalavh ak jha uxh of plo wopxo tarbyeon:
while let first = firstNextValue {
result.append(first)
firstNextValue = firstIterator.next()
}
while let second = secondNextValue {
result.append(second)
secondNextValue = secondIterator.next()
}
return AnySequence<T.Element>(result)
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.