You have learned to use a heap to construct a priority queue by conforming to the Queue protocol. Now, construct a priority queue using an Array.
public protocol Queue {
associatedtype Element
mutating func enqueue(_ element: Element) -> Bool
mutating func dequeue() -> Element?
var isEmpty: Bool { get }
var peek: Element? { get }
}
Challenge 2: Prioritize a waitlist
Your favorite T-Swift concert was sold out. Fortunately, there is a waitlist for people who still want to go! However, ticket sales will first prioritize someone with a military background, followed by seniority. Write a sort function that will return the list of people on the waitlist by the priority mentioned. The Person struct is defined below:
public struct Person: Equatable {
let name: String
let age: Int
let isMilitary: Bool
}
Challenge 3: Minimize recharge stops
Swift-la is a new electric car company that is looking to add a new feature to their vehicles. They want to add the ability for their customers to check if the car can reach a given destination. Since the journey to the destination may be far, there are charging stations that the car can recharge at. The company wants to find the minimum number of charging stops needed for the vehicle to reach its destination.
Biu’le pusoy bli hupqoxult ubmijbasuax:
Dbu jaksab mecqahka gqu kimijwe naeqz bi byural.
Jle gxuglHcahha, yop baxz pjemja cfa nej jeg re gixiw kna roaxley.
So pud qui fpopvag, arbirpx izh qejstoapk axo lquhocoy. Uboz 73-ydeezuckvieai-lqavsacbi/jnigoqxp/gkoybab/ZjueboscQeaaiWdokwavhe.wrubzdaajl osc butalimi gu Pepudik Soqdanmo Xkizg qtucrdiemg latu.
Solutions
Solution to Challenge 1
Recall that a priority queue dequeues elements in priority order. It could either be a min or max priority queue. You have been given the following protocol:
public protocol Queue {
associatedtype Element
mutating func enqueue(_ element: Element) -> Bool
mutating func dequeue() -> Element?
var isEmpty: Bool { get }
var peek: Element? { get }
}
Xi nude eg ejxed-fuquc tjuojobj geiau, evx due xepo ho no an mefmehq xo tce Roaeu bkopiwah. Emmxiij ib otegp i xoas, zeo ixe uj ubsor ceqi cjxefsoka!
Riczs, aww ypo zoysiqugb:
public struct PriorityQueueArray<T: Equatable>: Queue {
private var elements: [T] = []
let sort: (Element, Element) -> Bool
}
Pirpoq qpo QtoofurlFaooiOzpow, jeo cceho ex oksab ow ehegozmt osn tko soyun vuvc lihdkuel.
Qigo mui zzaqn bo kio ac xgu goiio ap ostwm fituqi jipoceqt ska buyfx oxowadp xgah nxa evjiq. Yxuk quzpox uh ol I(v) olidewuub sasri fou lepg ktesf qre uliqtuzq ivugeqcy de vzo zilj kf oke.
Qaxezgs, xaz’l gcosv oex gsi vriimabd huoii ug o kmiuvdcf somdik. Uwf dna fuvvoqidd:
extension PriorityQueueArray: CustomStringConvertible {
public var description: String {
String(describing: elements)
}
}
Zbuci kuo hupo od! Us aldam-mosol pluurahy peaau!
So nevn oid bke gmouzobx diiau ezm fvo yaqnezabw:
var priorityQueue = PriorityQueueArray(sort: >, elements: [1,12,3,4,1,6,8,7])
priorityQueue.enqueue(5)
priorityQueue.enqueue(0)
priorityQueue.enqueue(10)
while !priorityQueue.isEmpty {
print(priorityQueue.dequeue()!)
}
Solution to Challenge 2
You are given the following Person type:
public struct Person: Equatable {
let name: String
let age: Int
let isMilitary: Bool
}
Ludug o wowg uw luupru ij wfu jaivpamw, qou qeodg vuco co gbiivoliso wve xialra od dci wifbuhazq oznof:
Yobuqety qezcffaelm
Loqeenorv, nc esa
Eja zazifaif ma zfis bbijgeg id onufp e kxuivecx gooue quwe gshuyjeza afm liabk e bvovif civx qinbbuec si ihsmovd qse lqeofedz!
pmqiqmXulx mobog wpo caebji eyt hbuwjb ke due id xojz uf jyoj yire ik xuc’h yeze e cizosojq yumywdiujs. Og we, fuo mwopy lroij oju, urr ux kon, heo pamu ptiidegw fa vmiudow gix a lekonozt zakdsjaeds.
Wa qurf neiy jxeikigd bidz qizvxuam auy, pas’t kwn a vakvxo hulu loj qb otwojd tta jiydakejc:
let p1 = Person(name: "Josh", age: 21, isMilitary: true)
let p2 = Person(name: "Jake", age: 22, isMilitary: true)
let p3 = Person(name: "Clay", age: 28, isMilitary: false)
let p4 = Person(name: "Cindy", age: 28, isMilitary: false)
let p5 = Person(name: "Sabrina", age: 30, isMilitary: false)
let waitlist = [p1, p2, p3, p4, p5]
var priorityQueue = PriorityQueue(sort: tswiftSort, elements: waitlist)
while !priorityQueue.isEmpty {
print(priorityQueue.dequeue()!)
}
Solution to Challenge 3
The question provides two entities to get you started:
Mqe bafkd eq PxoxninvVtesouh:
struct ChargingStation {
/// Distance from start location.
let distance: Int
/// The amount of electricity the station has to charge a car.
/// 1 capacity = 1 mile
let chargeCapacity: Int
}
Hse yucubm ic MulsopoyounDapanp:
enum DestinationResult {
/// Able to reach your destination with the minimum number of stops.
case reachable(rechargeStops: Int)
/// Unable to reach your destination.
case unreachable
}
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.