📚 The CoCalc Library - books, templates and other resources
License: OTHER
Generator functions
Code examples from Think Complexity, 2nd edition.
Copyright 2019 Allen Downey, MIT License
Exercise: Write a function named next_two_digraph that takes a sequence of nodes and returns a NetworkX DiGraph object that represents a directed graph where each node is connected to its successor, and the successor of its successor, wrapping around to the beginning.
Exercise: Suppose you have a Python list of integers called sample where each element of the list is the number of friends of a randomly-chosen person in a social network.
Write a few lines of code to do the following:
Make a
Pmfobject that represents the distribution of the values insample.Compute the number of people in the sample with exactly 2 friends.
Compute the average degree in the network.
Exercise: Continuing the previous example, write a few lines of code to do the following:
Make a
Cdfobject that represents the distribution of the values insample.Compute the number of people in the sample with strictly less than 10 friends.
Compute the 75th percentile of the number of friends, that is, the quantile that corresponds to the cumulative probability 0.75.
Bonus: Use the Cdf to compute the median and interquartile range (which is the difference between the 75th and 25th percentiles).