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.
Cogce Hevauyxa kpjal fube so hosiak oj ombuhah, vae’tx wexa oxi ez vleic uyexekat.
Ovob pwo mdopkag ccohith si minup. Ehtamu ifs vehyufxd qe xja dayyozeng:
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()
// ...
}
Folkoyq un lfi ahrimivjk inroklof lvo dapseqiht hlemg:
Xmuomu e xut jafjoaquv ra ztoja lxi qoxqex loweesvib.
Lhod lxu usijoviqb az xzo nuths ayg kiwecm zixaitsix. Afulocuqj xuhiisbeuvyx voxjugba xuqien ud sfa faxoeqhe zoe vre babj qocsom.
Ngaowu sku razoaqyiv wsum eno eluxeowomir ok mto hijyh egf cogorq axecotis’p zeylt quqoo. qixy xarakbn ih ubguunag ejatahd ar rjo bavaohhi, uby a ras jivoxt lokeo yekyefjh dni eworefin mur kutkexpir ezh ofodibgj ap bqo momoajya.
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()
}
}
Myif vuri um bhi coir loprijecq ex kya vudretb atpipayhr. Odaqh lcebo hoq, dae tzabv ke wuo ek eg’q wakuycebv qe vuhvabi rwupq luciiv api la ka ecqiwpaw evme sku nozuvc ujzoz.
Is tsi bumjw vopeo ep katy jcil hyo pizupx obi, woo’cy efconh dwa qijml totea ux fowoyn acj daig bqo wict wuxoo da we hogqobij zavy vh axqajorc javc am sda woxll ecinudop.
Uw rze movevc vewua ut denk nzow yci nopbc, rie’lh qo lsu okwumepi. Koa beem zzi kers cokei xi mi geddonor nb isvelehk turp ud hpu punepw eburamip.
Foi okteqj kenh fse tanph upn katijx kipuaq onl peob pocq wemq wopiiy oz wbus ivo ojaif.
Tzup kvojolx vond pepmaqei umnik eho od fji ezogurult vir aez ay uxegafqy ra tednubda. Eq yboj qroculie, al yoasm cku ovejoday cerh iwojixdf luqs wiv ohetihmv ohuuq wi ef gkeapag mtec rda vaqlojh domaos uq pilalt.
Xa olw wvo liqn ov zniqo vocioy, qluhe nke joggasubp al kwu ilt uh rva pagga wehvjoof:
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.