Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write and test a Python implementation of the Markov chain algorithm. 2. Add

ID: 3626587 • Letter: 1

Question

1. Write and test a Python implementation of the Markov chain algorithm.

2. Add testing code to the Python implementation of the Markov chain algorithm, which verifies that each pair of consecutive words obtained during generate phase also occurred during build phase as a pair of consecutive words coming from the input document. During build phase create a vector consisting of all input words. During generate phase verify that every pair of consecutive random suffixes occurs as a pair of consecutive words inside the vector of input words.

Explanation / Answer

import collections import random class DynamicDie(collections.defaultdict): def __init__(self): collections.defaultdict.__init__(self, int) def add_side(self, side): self[side] += 1 def total_sides(self): return sum(self.values()) def roll(self): random_num = random.randint(0, self.total_sides()) total_pos = 0 for side, qty in self.items(): total_pos += qty if random_num