Answered step by step
Verified Expert Solution
Question
1 Approved Answer
IN PYTHON. You will create a function pokemon_machine that will utilize the provided DLL class. Your function should be able to add, remove, and swap
IN PYTHON.
You will create a function pokemon_machine that will utilize the provided DLL class. Your function should be able to add, remove, and swap pokemon in the pokemons DLL. Your function will receive two lists, pokemons and orders. The pokemons list will hold a list of pokemon names represented as strings. This list will be edited in-place by your function, which means you are not to declare any additional lists in your function . And the orders list is a list of tuples. The possible tuples are add, remove, and swap. The add tuple will look as follows (add, integer index to insert the pokemon at, pokemon name) so an example would be ("add, 37, vulpix"). The remove tuple will look as follows ("remove", integer index of the pokemon that is to be removed) so an example would be (remove, 2). And the swap tuple will look as follows (swap, integer index of the first pokemon to be swapped, integer index of the second pokemon to be swapped) so an example would be (swap, 37, 157). Your goal is to use the orders list to edit the pokemons list using its list of commands. The edited pokemons list will then be returned from the function as a python list (use the linked_list_to_list function given in the DLL class). Assignment Specifications Class DLLNode: DO NOT MODIFY the following attributes/functions This is doubly linked list node for providing you the linked list Attributes o val: T: Value held by this node. Note that this may be any type, such as a str, int, float, dict, or a more complex object. o nxt: DLLNode: Reference to the node that come after this node (may be None) o prev: DLLNode: Reference to the node that come before this node (may be None) __init__(self, value: T, next: Node = None, prev: Node = None) -> None o Initialize doubly linked list node o val: T: Value held by this node. Note that this may be any type, such as a str, int, float, dict, or a more complex object. o nxt : DLLNode: Reference to the node that come after this node (may be None) o prev: DLLNode: Reference to the node that come before this node (may be None) o return: None _str__(self) -> str and __repr__(self) -> str o Represents the Doubly Linked List Node as string o This function might help you debug in Pycharm since it automatically calls this function to show the class for you o return: string representing the node Class LinkedList: DO NOT MODIFY the following attributes/functions This is doubly linked list class will help you create the pokemon PC Attributes o head: DLLNode: The head of doubly linked list o tail: DLLNode: The tail of doubly linked list ___init__(self, container: List[T] = None) -> None o Initialize doubly linked list o container: List[T]: Container that contain the elements that will store in linked list o To reduce bug and error of linked list head, this function constructs the None node to be the header node. The rest of linked list data will push after this node. Note that this node cannot be removed. o If container is not None, this class will automatically generate the linked list that contains every element in the container. o return: None _str__(self) -> str and __repr__(self) -> str o Represent Doubly Linked List Node as string o This function might help you debug in Pycharm since it automatically calls this function to show the class for you o return: string represented entire linked list Warning: Do not convert the LinkedList to a built-in Python list until the very end of your function when you return. Solutions which simply convert the LinkedList to a Python list and use built-in operations to solve the problem will lose ALL credit. pokemon_machine (pokemon: Linkedlist, orders: List[Tuple]) -> LinkedList: o pokemon: LinkedList: The first input is a linked list of strings representing pokemon's name that are initially stored in the PC o orders: List[Tuple): The second input is a python list of three possible commands used to modify the PC The first command is for adding pokemon. This command is a tuple of length 3, where Index O is a string, "add" Index 1 is an integer representing the position to put the new pokemon Index 2 is string represented the name of new pokemon Must call add_pokemon The second command is for removing pokemon. This command is a tuple of length 2, where Index 0 is a string, remove" Index 1 an integer representing the position to remove pokemon from PC Must call remove_pokemon The last possible command is for swapping pokemons. This command is a tuple of length 3, where Index 0 is a string, swap" Index 1 an integer representing the position of the first pokemon to swap Index 2 an integer representing the position of the second pokemon to swap Must call swap_pokemon o Return: A linked list of strings represents the current pokemons within the PC in the right order. o Time Complexity: Olop). Where o is the length of the orders list and p is the length of the pokemon list add_pokemon(cur_node: DLLnode, add_pokemon: str) -> None o This is the inner function inside pokemon_machine. o This function will add pokemon after the current node o cur_node: str: Current node to add the next pokemon after this node o add_pokemon: str: Name of pokemon to add o Return: None o Time Complexity: O(1) remove_pokemon(cur_node: DLLnode) -> None o This is the inner function inside pokemon_machine. o This function will remove pokemon at the current node o cur_node: Current node to add the next pokemon after this node o Return: None o Time Complexity: 0(1) swap_pokemon(first_node: DLLnode, second_node: DLLnode) -> None o This is the inner function inside pokemon_machine. o This function will swap pokemon between the first node and the second node o first_node: DLLnode: The first node to swap o second_node: DLLnode: The second node to swap o return: None o Time Complexity: O(1) Examples: Ex. 1: pokemons: ["Charizard", "Bulbasaur", "Venusaur"] orders: [("add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] Order 1: The first order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to add Mew into the index 1. This will add Mew after Charizard List after this process: ["Charizard", Mew, "Bulbasaur", "Venusaur"] Order 2: The second order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to add Mew-Two into the index 2. This will add Mew-Two after Mew List after this process: ["Charizard", Mew, Mew-Two, "Bulbasaur", "Venusaur"] Order 3: The third order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to remove index 3 pokemon out of the list. This will remove Bulbasaur out of the list List after this process: ["Charizard", Mew, Mew-Two", "Venusaur"] After doing all the order, the PC contains ["Charizard", "Mew, Mew-Two", "Venusaur"] You will create a function pokemon_machine that will utilize the provided DLL class. Your function should be able to add, remove, and swap pokemon in the pokemons DLL. Your function will receive two lists, pokemons and orders. The pokemons list will hold a list of pokemon names represented as strings. This list will be edited in-place by your function, which means you are not to declare any additional lists in your function . And the orders list is a list of tuples. The possible tuples are add, remove, and swap. The add tuple will look as follows (add, integer index to insert the pokemon at, pokemon name) so an example would be ("add, 37, vulpix"). The remove tuple will look as follows ("remove", integer index of the pokemon that is to be removed) so an example would be (remove, 2). And the swap tuple will look as follows (swap, integer index of the first pokemon to be swapped, integer index of the second pokemon to be swapped) so an example would be (swap, 37, 157). Your goal is to use the orders list to edit the pokemons list using its list of commands. The edited pokemons list will then be returned from the function as a python list (use the linked_list_to_list function given in the DLL class). Assignment Specifications Class DLLNode: DO NOT MODIFY the following attributes/functions This is doubly linked list node for providing you the linked list Attributes o val: T: Value held by this node. Note that this may be any type, such as a str, int, float, dict, or a more complex object. o nxt: DLLNode: Reference to the node that come after this node (may be None) o prev: DLLNode: Reference to the node that come before this node (may be None) __init__(self, value: T, next: Node = None, prev: Node = None) -> None o Initialize doubly linked list node o val: T: Value held by this node. Note that this may be any type, such as a str, int, float, dict, or a more complex object. o nxt : DLLNode: Reference to the node that come after this node (may be None) o prev: DLLNode: Reference to the node that come before this node (may be None) o return: None _str__(self) -> str and __repr__(self) -> str o Represents the Doubly Linked List Node as string o This function might help you debug in Pycharm since it automatically calls this function to show the class for you o return: string representing the node Class LinkedList: DO NOT MODIFY the following attributes/functions This is doubly linked list class will help you create the pokemon PC Attributes o head: DLLNode: The head of doubly linked list o tail: DLLNode: The tail of doubly linked list ___init__(self, container: List[T] = None) -> None o Initialize doubly linked list o container: List[T]: Container that contain the elements that will store in linked list o To reduce bug and error of linked list head, this function constructs the None node to be the header node. The rest of linked list data will push after this node. Note that this node cannot be removed. o If container is not None, this class will automatically generate the linked list that contains every element in the container. o return: None _str__(self) -> str and __repr__(self) -> str o Represent Doubly Linked List Node as string o This function might help you debug in Pycharm since it automatically calls this function to show the class for you o return: string represented entire linked list Warning: Do not convert the LinkedList to a built-in Python list until the very end of your function when you return. Solutions which simply convert the LinkedList to a Python list and use built-in operations to solve the problem will lose ALL credit. pokemon_machine (pokemon: Linkedlist, orders: List[Tuple]) -> LinkedList: o pokemon: LinkedList: The first input is a linked list of strings representing pokemon's name that are initially stored in the PC o orders: List[Tuple): The second input is a python list of three possible commands used to modify the PC The first command is for adding pokemon. This command is a tuple of length 3, where Index O is a string, "add" Index 1 is an integer representing the position to put the new pokemon Index 2 is string represented the name of new pokemon Must call add_pokemon The second command is for removing pokemon. This command is a tuple of length 2, where Index 0 is a string, remove" Index 1 an integer representing the position to remove pokemon from PC Must call remove_pokemon The last possible command is for swapping pokemons. This command is a tuple of length 3, where Index 0 is a string, swap" Index 1 an integer representing the position of the first pokemon to swap Index 2 an integer representing the position of the second pokemon to swap Must call swap_pokemon o Return: A linked list of strings represents the current pokemons within the PC in the right order. o Time Complexity: Olop). Where o is the length of the orders list and p is the length of the pokemon list add_pokemon(cur_node: DLLnode, add_pokemon: str) -> None o This is the inner function inside pokemon_machine. o This function will add pokemon after the current node o cur_node: str: Current node to add the next pokemon after this node o add_pokemon: str: Name of pokemon to add o Return: None o Time Complexity: O(1) remove_pokemon(cur_node: DLLnode) -> None o This is the inner function inside pokemon_machine. o This function will remove pokemon at the current node o cur_node: Current node to add the next pokemon after this node o Return: None o Time Complexity: 0(1) swap_pokemon(first_node: DLLnode, second_node: DLLnode) -> None o This is the inner function inside pokemon_machine. o This function will swap pokemon between the first node and the second node o first_node: DLLnode: The first node to swap o second_node: DLLnode: The second node to swap o return: None o Time Complexity: O(1) Examples: Ex. 1: pokemons: ["Charizard", "Bulbasaur", "Venusaur"] orders: [("add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] Order 1: The first order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to add Mew into the index 1. This will add Mew after Charizard List after this process: ["Charizard", Mew, "Bulbasaur", "Venusaur"] Order 2: The second order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to add Mew-Two into the index 2. This will add Mew-Two after Mew List after this process: ["Charizard", Mew, Mew-Two, "Bulbasaur", "Venusaur"] Order 3: The third order in the list [["add", 1, "Mew"), ("add", 2, "Mew-Two"), ("remove", 3)] wants you to remove index 3 pokemon out of the list. This will remove Bulbasaur out of the list List after this process: ["Charizard", Mew, Mew-Two", "Venusaur"] After doing all the order, the PC contains ["Charizard", "Mew, Mew-Two", "Venusaur"]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started