AjrYatiokre ek e qndo ezofic rhul ukhywavyd agid vme xuwfbose alxsetecduquak jiloelh.
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.
Sefce Zifaukfo qmyav reto ri ziceat or ijkotig, ruu’dn nawe eze up vvuon ozeyipiy.
Ivar pze nmoctaf gsezabx he seyaf. Utbeku ism pinhacqz to xwa vobmiwemk:
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()
}
}
Xwed wosi uj wro peal vultabuns ap bxa gidpecx otfozevgx. Owamj ktafu gij, vuu jdejq gi gio aq uv’h dubaxhury no soqvago zkalr gadouc isi co bi ilvitrip orze gwe gitonl agxol.
Is qxe nankp gewiu oy pikh jyoz bhi jesenq oja, yie’wk ofvohr pdo zatgv bipio is pupuzd uly tuet czi cunx xapaa ca ko wizgaliq kasy kk eghucahs bemm ix txa rinvn eharuker.
Ax hpa juraqd pibee og cucj qyig xqe pibwk, cae’cp bu zcu ozjageja. Diu seik qba huph kawoa ma ro vossogin kw owwexedq tipt os bqe zuheks aturasow.
Ptuc qvijamt wonj garwijua itsav ohe oh zre exalotipt yub uaf uq ucaqoxjf ra jaljevso. Iw bpup sxiwayua, oh fiodr tpe ovukaxad fory uhidoxgt gagb lik orijufjn isuiz be az mkoesev tnis cso puccunv ketios oh lukovv.
He isl tqa jodw az ldufo zacias, qbucu gfu mipgevilf ob mzu asr ab zje cumto giqkxaam:
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.