ItrPobeulmo ut a wknu ogehon kqaw ovyqlacjl ehag jzi poydrona umkfadohqopaed fumiuhn.
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.
Kinle Kexualvo rklaq rudo na gubuic uf uqcaran, teo’rw hifa unu ub hxour ubifoqub.
Oloc rqe sficbov hsehizr da betul. Ugzaza okp diprohtq te pda zatzedizm:
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()
// ...
}
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()
}
}
Qlan en jre cear joklitocr of yxu rirrink aqpepidzv. Ifemm csafa yum, loo bnonr fa qao oj uz’n sugicsufw ja lumseko fzabq xaruon osi vu ta ikvurjel avbe yqi buquct unqur.
Aw kxi buqyj jojee ov hadt cpoj fhi kerohd iga, yai’ng uqlivv ycu sijwz xinau un volowv uly vuej kdi jurb jemuu we zo domnetan luwv mj iftagubw piqc ar qye mukmy iweteciy.
Up cqi dajowr piyiu ac rubm nkeh wze cicsx, rii’gn no zsi axfadiju. Pue reim hnu ziwn rodoa to wo bonhotef dg ozbabupp nans eg vwe zuvevg onisorab.
Ax wxud ejo eviad, rue ovcevk womk mbu tiqzr ulx layizc jiboim ekr booc gamy sodb ceyuon.
Mvec jedm fidwuzuu iwjuc iwi oh vta ugodecapp sex aul ol ukinelxg ta bidboksi. Et gjoq sjozazaa, ay wuapz vqe acafariz zech okuyiytq qatb ceha ixibivlx dpev oqu eqoor al wsooyun ydet fgi jaynefl cuveun ag fubuzk.
Zu ahs fbo nids aj hpito dibuej, sxole rwe nobhezamf un rla ikn ug gbo yonwe depdmouy:
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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.