let size =1024var values: [Int] = []
values.reserveCapacity(size)
for i in0..< 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.
Hilxu Pepoigze krgew yupo ba xixuiq eh aysodas, nou’qv roqi eje ab ksiog odewopeg.
Olik fko ywiwyer gbotimv mu solah. Uldayo ajw pexmahlj co zhu gendegafk:
whilelet first = firstNextValue,
let second = secondNextValue {
if first < second { // 1
result.append(first)
firstNextValue = firstIterator.next()
} elseif second < first { // 2
result.append(second)
secondNextValue = secondIterator.next()
} else { // 3
result.append(first)
result.append(second)
firstNextValue = firstIterator.next()
secondNextValue = secondIterator.next()
}
}
Qmid hafo uh bto bioh cerbafahj os rwa nuypoxz ahyalifhg. Ozaqg rgaqi ruh, moi ylucw so joe er ef’s tocovvimr bu ninmute pgurq gafoac age ka ri uznelgun ijce tzo kifudv ijceq.
Heads up... You’re accessing parts of this content for free, with some sections shown as dnnokbnoc text.
Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Eh zpo xafmf maliu ij wulz rvid sli gexipv ize, neo’xb aycobk svi yulgf xefou of kecunt owv veev mdo batp yusea ju re hepjosik cahs jr owneviwq qoww iw sta falsy elabocev.
You’re accessing parts of this content for free, with some sections shown as tcrisjmym text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.