Question: (F). Implement PR_Iterator Define a PR_Iterator class with a custom _next_() method. The _init_ method of this class should create instance variables to store the


(F). Implement PR_Iterator Define a PR_Iterator class with a custom _next_() method. The _init_ method of this class should create instance variables to store the PR_DiGraph object from which it was constructed; a counter i, starting at 0, to log the number of steps taken, and a current_state variable whose value is one of the keys of the link_dict of the Pr_DiGraph . You can choose its initial value arbitrarily; in my solution code I chose self.current_state = "hamilton". We are going to use iteration to implement the PageRank algorithm. This means we are going to imagine a surfer who is following the links in our data set. Implement the following two methods: 1. follow_link(). A. Pick a random new character mentioned by the current character, or new airport served by the current airport. Let's call this next_state. B. If next_state != current_state, set current_state to next_state. C. Otherwise (if next_state == current_state), teleport (see below). D. You might run into KeyError s, in which case you should again teleport (use a try-except block). 2. teleport(). A. Set the current state to a new state (key of the link dict) completely at random. Hint: use random. choice from the random module to choose elements of lists. Finally, implement _next_0. _next_() should do follow_link() with 85% probability, and do teleport() with 15% probability. You should also define a custom StopIteration condition to ensure that only as many steps are taken as the iteration_limit supplied to the PR_DiGraph initializer. 1. To do something with 85% probability, use the following: if random.random()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
