1. Engineering
  2. Computer Science
  3. 1 run lengths write a function in python and numpy...

Question: 1 run lengths write a function in python and numpy...

Question details

1 [Run Lengths] Write a function (in Python and numpy) with the following specification def run_lenths(n, p): """Return a list of the run lengths in n tosses of a coin whose heads probability is p. Arguments: n--Number of tosses (a positive int), p--The probability of observing heads for any given toss (float between 0 and 1). """ For example, if the simulated sequence of coin tosses is HTTHHTHHHTTHHTTTTH, then the list of run lengths the function returns will be [1, 2, 2, 1, 3, 2, 2, 4, 1]. Hint: Your function should not explicitly return or print the sequence of coin tosses itself. But you will have to generate this sequence of tosses internally and derive the sequence of run lengths from it. Printing the sequence of coin tosses (for small values of n) during testing and debugging can help you verify if your function works as expected. For example, it is easy to forget to include the last run. In the next two problems, we will be interested in the maximum 1 run length (4 in the example above) and the number of runs (9 in the example above). These are given by max(run_lengths(n, p)) and len(run_lengths(n, p)), respectively.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution