예제 #1
0
 def __init__(self):
     self.planet_repository = PlanetRepository(
     )  # a new repository created for each space station
     self.astronaut_repository = AstronautRepository(
     )  # a new repository created for each space station
     self.astronauts_limit = 5
     self.eligible_astronauts = []
     self.completed_missions = 0
     self.not_completed_missions = 0
예제 #2
0
class SpaceStation:
    __ASTRONAUT_TYPES = {
        "Biologist": Biologist,
        "Geodesist": Geodesist,
        "Meteorologist": Meteorologist,
    }

    def __init__(self):
        self.planet_repository = PlanetRepository()
        self.astronaut_repository = AstronautRepository()
        self.successful_missions = 0
        self.unsuccessful_missions = 0

    def add_astronaut(self, astronaut_type: str, name: str):
        if astronaut_type not in self.__ASTRONAUT_TYPES:
            raise Exception("Astronaut type is not valid!")

        if self.astronaut_repository.find_by_name(name) is not None:
            return f"{name} is already added."

        astronaut = self.__ASTRONAUT_TYPES[astronaut_type](name)
        self.astronaut_repository.add(astronaut)
        return f"Successfully added {astronaut_type}: {name}."

    def add_planet(self, name: str, items: str):
        if self.planet_repository.find_by_name(name) is not None:
            return f"{name} is already added."

        planet = Planet(name)
        planet.items.extend(items.split(", "))
        self.planet_repository.add(planet)
        return f"Successfully added Planet: {name}."

    def retire_astronaut(self, name):
        astronaut = self.astronaut_repository.find_by_name(name)

        if astronaut is None:
            raise Exception(f"Astronaut {name} doesn't exist!")

        self.astronaut_repository.remove(astronaut)
        return f"Astronaut {name} was retired!"

    def recharge_oxygen(self):
        for astronaut in self.astronaut_repository.astronauts:
            astronaut.increase_oxygen(10)

    def send_on_mission(self, planet_name: str):
        planet = self.planet_repository.find_by_name(planet_name)
        if planet is None:
            raise Exception("Invalid planet name!")

        suitable_astronauts = [
            astronaut for astronaut in self.astronaut_repository.astronauts
            if astronaut.oxygen > 30
        ]
        if not suitable_astronauts:
            raise Exception(
                f"You need at least one astronaut to explore the planet!")

        approved_astronauts = []
        for astronaut in sorted(suitable_astronauts,
                                key=lambda ast: -ast.oxygen):
            if len(approved_astronauts) == 5:
                break
            approved_astronauts.append(astronaut)

        participated_astronauts = 0
        for astronaut in approved_astronauts:
            participated_astronauts += 1
            while astronaut.oxygen > 0 and planet.items:
                astronaut.backpack.append(planet.items.pop())
                astronaut.breathe()

            if not planet.items:
                self.successful_missions += 1
                return f"Planet: {planet_name} was explored. " \
                       f"{participated_astronauts} astronauts participated in collecting items."

        self.unsuccessful_missions += 1
        return "Mission is not completed."

    def report(self):
        info = [
            f"{self.successful_missions} successful missions!",
            f"{self.unsuccessful_missions} missions were not completed!",
            f"Astronauts' info:"
        ]

        info.extend([
            str(astronaut)
            for astronaut in self.astronaut_repository.astronauts
        ])
        return '\n'.join(info)
예제 #3
0
 def __init__(self):
     self.planet_repository = PlanetRepository()
     self.astronaut_repository = AstronautRepository()
     self.successful_missions = 0
     self.unsuccessful_missions = 0
class SpaceStation:
    def __init__(self):
        self.planet_repository = PlanetRepository()
        self.astronaut_repository = AstronautRepository()
        self.successful_missions = 0
        self.failed_missions = 0

    def add_astronaut(self, astronaut_type: str, name: str):
        if self.astronaut_repository.find_by_name(name):
            return f"{name} is already added."
        astronaut = self.__create_astronaut(astronaut_type, name)
        self.astronaut_repository.add(astronaut)
        return f"Successfully added {astronaut_type}: {name}."

    def add_planet(self, name: str, items: str):
        if self.planet_repository.find_by_name(name):
            return f"{name} is already added."
        planet = Planet(name)
        planet.items = items.split(', ')
        self.planet_repository.add(planet)

        return f"Successfully added Planet: {name}."

    def retire_astronaut(self, name: str):
        astronaut = self.astronaut_repository.find_by_name(name)

        if astronaut is None:
            raise Exception("Astronaut {astronaut_name} doesn't exist!")

        self.astronaut_repository.remove(astronaut)
        return f"Astronaut {name} was retired!"

    def recharge_oxygen(self):
        for astronaut in self.astronaut_repository.astronauts:
            astronaut.increase_oxygen(10)

    def send_on_mission(self, planet_name: str):
        planet = self.planet_repository.find_by_name(planet_name)
        if planet is None:
            raise Exception("Invalid planet name!")

        astronauts = self.astronaut_repository.find_astronaut_for_mission(
            5, 30)

        if len(astronauts) == 0:
            raise Exception(
                "You need at least one astronaut to explore the planet!")

        participated_astronauts = 0
        for astronaut in astronauts:
            if len(planet.items) == 0:
                break

            while astronaut.oxygen > 0 and len(planet.items) > 0:
                astronaut.backpack.append(planet.items.pop())
                astronaut.breathe()
            participated_astronauts += 1

        if len(planet.items) == 0:
            self.successful_missions += 1
            return f"Planet: {planet_name} was explored. {participated_astronauts} astronauts participated in collecting items."
        else:
            self.failed_missions += 1
            return "Mission is not completed."

    def report(self):
        result = f"{self.successful_missions} successful missions!" + '\n'
        result += f"{self.failed_missions} missions were not completed!" + '\n'
        result += f"Astronauts' info:" + '\n'

        for astronaut in self.astronaut_repository.astronauts:
            result += str(astronaut) + '\n'

        return result.strip()

    def __create_astronaut(self, astronaut_type, name):
        if astronaut_type == Biologist.__name__:
            return Biologist(name)
        if astronaut_type == Geodesist.__name__:
            return Geodesist(name)
        if astronaut_type == Meteorologist.__name__:
            return Meteorologist(name)
        raise Exception('Astronaut type is not valid!')
예제 #5
0
class SpaceStation:
    astronaut_types = ("Biologist", "Geodesist", "Meteorologist")

    def __init__(self):
        self.planet_repository = PlanetRepository()
        self.astronaut_repository = AstronautRepository()
        self.successful_missions_count = 0
        self.unsuccessful_missions_count = 0

    def add_astronaut(self, astronaut_type: str, name: str):
        if astronaut_type == "Biologist":
            bio = Biologist(name)
            if self.astronaut_repository.find_by_name(name):
                return f"{name} is already added."
            self.astronaut_repository.add(bio)
            return f"Successfully added {astronaut_type}: {name}."

        elif astronaut_type == "Geodesist":
            geo = Geodesist(name)
            if self.astronaut_repository.find_by_name(name):
                return f"{name} is already added."
            self.astronaut_repository.add(geo)
            return f"Successfully added {astronaut_type}: {name}."

        elif astronaut_type == "Meteorologist":
            met = Geodesist(name)
            if self.astronaut_repository.find_by_name(name):
                return f"{name} is already added."
            self.astronaut_repository.add(met)
            return f"Successfully added {astronaut_type}: {name}."

    def add_planet(self, name: str, items: str):
        if self.planet_repository.find_by_name(name):
            return f"{name} is already added."
        planet = Planet(name, items.split(", "))
        self.planet_repository.add(planet)
        return f"Successfully added Planet: {name}."

    def retire_astronaut(self, name: str):
        retired_astronaut = self.astronaut_repository.find_by_name(name)
        if retired_astronaut is None:
            return f"Astronaut {name} doesn't exists!"
        self.astronaut_repository.remove(retired_astronaut)
        return f"Astronaut {name} was retired!"

    def recharge_oxygen(self):
        for astronaut in self.astronaut_repository.astronauts:
            astronaut.increase_oxygen(10)

    def send_on_mission(self, planet_name: str):
        if not self.planet_repository.find_by_name(planet_name):
            raise Exception("Invalid planet name!")
        astronauts_on_mission = deque()
        for astronaut in sorted(self.astronaut_repository.astronauts,
                                key=lambda x: x.oxygen,
                                reverse=True):
            if astronaut.oxygen >= 30 and len(astronauts_on_mission) < 6:
                astronauts_on_mission.append(astronaut)
            else:
                break
        if not len(astronauts_on_mission) > 0:
            raise Exception(
                "You need at least one astronaut to explore the planet!")
        astronauts_count = len(astronauts_on_mission)
        current_astronaut = astronauts_on_mission.popleft()
        planet = self.planet_repository.find_by_name(planet_name)
        while planet.items:
            item = planet.items.pop()
            current_astronaut.breathe()
            current_astronaut.backpack.append(item)
            if current_astronaut.oxygen <= 0:
                if not astronauts_on_mission:
                    self.unsuccessful_missions_count += 1
                    return "Mission is not completed."
                astronauts_on_mission.popleft()
            self.successful_missions_count += 1
        return f"Planet: {planet} was explored. {astronauts_count} astronauts participated in collecting items."

    def report(self):
        result = f"{self.successful_missions_count} successful missions!\n"
        result += f"{self.unsuccessful_missions_count} missions were not completed!\n"
        for astronaut in self.astronaut_repository.astronauts:
            result += f"Astronauts' info:\nName: {astronaut.name}\nOxygen: {astronaut.oxygen}\n"
            if len(astronaut.backpack) > 0:
                result += f"Backpack items: {', '.join([item for item in astronaut.backpack])}\n"
            else:
                result += 'Backpack items: "none"\n'
        return result
예제 #6
0
class SpaceStation:
    def __init__(self):
        self.planet_repository = PlanetRepository(
        )  # a new repository created for each space station
        self.astronaut_repository = AstronautRepository(
        )  # a new repository created for each space station
        self.astronauts_limit = 5
        self.eligible_astronauts = []
        self.completed_missions = 0
        self.not_completed_missions = 0

    def add_astronaut(self, astronaut_type: str, name: str):
        if self.astronaut_repository.find_by_name(name) is not None:
            return f"{name} is already added."

        if astronaut_type not in ["Biologist", "Geodesist", "Meteorologist"]:
            raise Exception("Astronaut type is not valid!")

        astronaut = None
        if astronaut_type == "Biologist":
            astronaut = Biologist(name)
        elif astronaut_type == "Geodesist":
            astronaut = Geodesist(name)
        elif astronaut_type == "Meteorologist":
            astronaut = Meteorologist(name)

        self.astronaut_repository.add(astronaut)
        return f"Successfully added {astronaut_type}: {name}."

    def add_planet(self, name: str, items: str):
        if self.planet_repository.find_by_name(name) is not None:
            return f"{name} is already added."

        planet = Planet(name, items)
        self.planet_repository.add(planet)
        return f"Successfully added Planet: {name}."

    def retire_astronaut(self, name: str):
        astronaut = self.astronaut_repository.find_by_name(name)
        if astronaut is None:
            return f"Astronaut {name} doesn't exists!"

        self.astronaut_repository.remove(astronaut)
        return f"Astronaut {name} was retired!"

    def recharge_oxygen(self):
        for astronaut in self.astronaut_repository.astronauts:
            astronaut.increase_oxygen(10)

    def send_on_mission(self, planet_name: str):
        planet_found = self.planet_repository.find_by_name(planet_name)

        if planet_found is None:
            raise Exception("Invalid planet name!")

        self.eligible_astronauts = self.check_for_suitable_astronauts()

        if not self.eligible_astronauts:
            raise Exception(
                "You need at least one astronaut to explore the planet!")

        # TODO check the reversed list by attribute
        self.eligible_astronauts.sort(key=lambda a: a.oxygen, reverse=True)

        astronauts_participated = 0
        sorted_astronauts = deque(self.eligible_astronauts)
        planet_items_reversed = deque(planet_found.items)

        while True:
            astronaut = sorted_astronauts.popleft()
            astronauts_participated += 1

            while planet_items_reversed:
                item = planet_items_reversed.pop()
                astronaut.breathe()
                astronaut.backpack.append(item)

                if astronaut.oxygen <= 0:
                    break

            if len(sorted_astronauts) > 0 and len(planet_items_reversed) > 0:
                continue

            elif not planet_items_reversed:
                self.completed_missions += 1
                return f"Planet: {planet_name} was explored. {astronauts_participated} astronauts participated in collecting items."

            elif planet_items_reversed:
                self.not_completed_missions += 1
                return "Mission is not completed."

    def report(self):
        data = f"{self.completed_missions} successful missions!" + '\n'
        data += f"{self.not_completed_missions} missions were not completed!" + '\n'
        data += "Astronauts' info:" + '\n'

        for astronaut in self.astronaut_repository.astronauts:
            data += f"Name: {astronaut.name}" + '\n'
            data += f"Oxygen: {astronaut.oxygen}" + '\n'
            if not astronaut.backpack:
                data += f"Backpack items: none" + '\n'
            else:
                data += f"Backpack items: {', '.join(astronaut.backpack)}" + '\n'
        return data

    def check_for_suitable_astronauts(self):

        # TODO You should pick up to 5 astronauts with the highest amount of oxygen among the ones with oxygen above 30 units.

        astronauts_count = 0

        self.astronaut_repository.astronauts.sort(key=lambda a: a.oxygen,
                                                  reverse=True)
        for astronaut in self.astronaut_repository.astronauts:
            if astronaut.oxygen > 30 and astronauts_count < self.astronauts_limit:
                self.eligible_astronauts.append(astronaut)
                astronauts_count += 1

        return self.eligible_astronauts
예제 #7
0
 def __init__(self):
     self.planet_repository = PlanetRepository()
     self.astronaut_repository = AstronautRepository()
     self.number_of_successful_missions = 0
     self.number_of_not_completed_missions = 0
예제 #8
0
class SpaceStation:
    def __init__(self):
        self.planet_repository = PlanetRepository()
        self.astronaut_repository = AstronautRepository()
        self.number_of_successful_missions = 0
        self.number_of_not_completed_missions = 0

    def add_astronaut(self, astronaut_type: str, name: str):

        if self.astronaut_repository.find_by_name(name):
            return f"{name} is already added."
        if astronaut_type == "Biologist":
            self.astronaut_repository.astronauts.append(Biologist(name))
            return f"Successfully added {astronaut_type}: {name}."
        elif astronaut_type == "Geodesist":
            self.astronaut_repository.astronauts.append(Geodesist(name))
            return f"Successfully added {astronaut_type}: {name}."
        elif astronaut_type == "Meteorologist":
            self.astronaut_repository.astronauts.append(Meteorologist(name))
            return f"Successfully added {astronaut_type}: {name}."
        else:
            raise Exception("Astronaut type is not valid!")

    def add_planet(self, name: str, items: str):

        if self.planet_repository.find_by_name(name):
            return f"{name} is already added."

        planet = Planet(name)
        planet.items = items.split(", ")
        self.planet_repository.planets.append(planet)
        return f"Successfully added Planet: {name}."

    def retire_astronaut(self, name: str):
        astronaut = self.astronaut_repository.find_by_name(name)
        if not astronaut:
            raise Exception(f"Astronaut {name} doesn't exists!")

        self.astronaut_repository.astronauts.remove(astronaut)
        return f"Astronaut {name} was retired!"

    def recharge_oxygen(self):
        for astronaut in self.astronaut_repository.astronauts:
            astronaut.increase_oxygen(10)

    def send_on_mission(self, planet_name: str):
        find_planet = [
            p for p in self.planet_repository.planets if p.name == planet_name
        ]
        if not find_planet:
            raise Exception("Invalid planet name!")

        sorted_astronaut = []
        for astronaut in self.astronaut_repository.astronauts:
            if astronaut.oxygen > 30:
                sorted_astronaut.append(astronaut)

        if not sorted_astronaut:
            raise Exception(
                "You need at least one astronaut to explore the planet!")
        choosing_astronaut = []
        for astronaut in sorted(sorted_astronaut, key=lambda x: -x.oxygen):
            if len(choosing_astronaut) < 5:
                choosing_astronaut.append(astronaut)
            else:
                break

        planet = find_planet[0]
        counter = 0

        for astronaut in choosing_astronaut:
            if planet.items:
                counter += 1
                for item in planet.items[::-1]:

                    astronaut.backpack.append(item)
                    planet.items.remove(item)
                    astronaut.breathe()
                    if astronaut.oxygen <= 0:
                        break
            else:
                self.number_of_successful_missions += 1
                return f"Planet: {planet_name} was explored. {counter} astronauts participated in collecting items."

        self.number_of_not_completed_missions += 1
        return "Mission is not completed."

    def report(self):
        result = [
            f"{self.number_of_successful_missions} successful missions!",
            f"{self.number_of_not_completed_missions} missions were not completed!",
            f"Astronauts' info:"
        ]
        for astronaut in self.astronaut_repository.astronauts:
            result.extend([
                f"Name: {astronaut.name}", f"Oxygen: {astronaut.oxygen}",
                f"Backpack items: {', '.join(astronaut.backpack) if astronaut.backpack else 'none'}"
            ])

        return '\n'.join(result)