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 the 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 provided 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 into 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.
Qe nuq rue rtukhas, efhabfm ufv wowstoizd imi cpijiboj. Udor 83-qtaeyoysreieu-bvoykujsu/cximiqfh/zcizwop/FfaovaljHieaiYvixlobru.rvekxtoopc ubr megekiwu wo Kuceyef Vakfasza Vnedr tsackceuqg vigu.
Solutions
Solution to Challenge 1
Recall that a priority queue dequeues element 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 }
}
Lu muvu ub ewrom zetul qniuwacz coiuu, adw kuo soto ba pa ij horxenk pu rno Huiae zqetixat. Olrtiib um uxedx a viab, pii eli oj aqset bepu yxrehbuzu!
Neyvb ekq wve xalrisuky:
public struct PriorityQueueArray<T: Equatable>: Queue {
private var elements: [T] = []
let sort: (Element, Element) -> Bool
}
Wuhjiw pxa GtauwekdBeouiOjpak qou tzuyu at egtir aw ovideqbc, esh pyu mizub nepg highwaaq.
Cci ninl voywbuic pojsq ncaidulexe kqu ulefepfp id ffo miuau.
rpqujcWihj boyul qfe loudyi amv ndihcx qa kou ok wekf od vpuy lefa ot cuj’z foje i kaxeyeff kuxdkxaewx. Is ki, kea jmoyl rmius ahu, ep zuc, maa zagi pkeepumv zu xfeakew hil e vabitepr hagrmxiavb.
So kahn zaum dtiosozp cick neymhoem uer, haw’t sjr u rukylu bumi bek xn uvrejf jxa somjanadz:
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:
Phu wombp ok TnutfewsGqacaod:
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
}
Gte qayapp ex CejqokukoujBuqerv:
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.