sorted function and dict inbuilt type should not be used . should be in python A WalkerPersonality
Question:
sorted
function and dict
inbuilt type should not be used . should be in python
A WalkerPersonality
is a class that implements two methods:
add_mountain
, which allows a Walker to note that mountain they've just walked by
select_branch
, which given two Trails as input, decides which of them they will take by returning the Enum PersonalityDecision
. If STOP
is returned, then neither branch should be taken and the path ends here.
Your task is to, without using recursion, implement the Trail method follow_path
, which takes in an argument personality
of type WalkerPersonality
, and on your trail, calls personality.add_mountain
for every mountain on your trail this walker personality would pass by.
For example, if personality
defined select_branch
as return PersonalityDecision.TOP
(Always choose the top (first) branch), then in the previous example:
We would call personality.add_mountain
with Mountains named top-top
, top-middle
and final
.
For (somewhat) more complicated personalities, see personality.py
. below is personality.py
from _future_ import annotations from abc import ABC, abstractmethod from enum import auto from base_enum import BaseEnum from mountain import Mountain from trail import Trail class PersonalityDecision(BaseEnum): TOP = auto() BOTTOM = auto() STOP = auto() class WalkerPersonality(ABC): def _init_(self) -> None: self.mountains = [] def add_mountain(self, mountain: Mountain) -> None: self.mountains.append(mountain) @abstractmethod def select_branch(self, top_branch: Trail, bottom_branch: Trail) -> PersonalityDecision: raise NotImplementedError() class TopWalker(WalkerPersonality): def select_branch(self, top_branch: Trail, bottom_branch: Trail) -> PersonalityDecision: # Always select the top branch return PersonalityDecision.TOP class BottomWalker(WalkerPersonality): def select_branch(self, top_branch: Trail, bottom_branch: Trail) -> PersonalityDecision: # Always select the bottom branch return PersonalityDecision.BOTTOM class LazyWalker(WalkerPersonality): def select_branch(self, top_branch: Trail, bottom_branch: Trail) -> PersonalityDecision: """ Try looking into the first mountain on each branch, take the path of least difficulty. """ # isinstance breaks across imports if running the original file as main # So just check _class.name_ :( top_m = top_branch.store._class.name_ == "TrailSeries" bot_m = bottom_branch.store._class.name_ == "TrailSeries" if top_m and bot_m: if top_branch.store.mountain.difficulty_level < bottom_branch.store.mountain.difficulty_level: return PersonalityDecision.TOP elif top_branch.store.mountain.difficulty_level > bottom_branch.store.mountain.difficulty_level: return PersonalityDecision.BOTTOM return PersonalityDecision.STOP # If one of them has a mountain, don't take it. # If neither do, then take the top branch. if top_m: return PersonalityDecision.BOTTOM return PersonalityDecision.TOP
with the instructions said above create
def follow_path(self, personality: WalkerPersonality) -> None:
"""Follow a path and add mountains according to a personality."""
raise NotImplementedError()