Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/11-Python Generators/03-Iterators and Generators Homework - Solution.ipynb
Views: 648
Iterators and Generators Homework - Solution
Problem 1
Create a generator that generates the squares of numbers up to some number N.
Problem 2
Create a generator that yields "n" random numbers between a low and high number (that are inputs).
Note: Use the random library. For example:
Problem 3
Use the iter() function to convert the string below into an iterator:
Problem 4
Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.
If the output has the potential of taking up a large amount of memory and you only intend to iterate through it, you would want to use a generator. (Multiple answers are acceptable here!)
Extra Credit!
Can you explain what gencomp is in the code below? (Note: We never covered this in lecture!)
Hint: Google generator comprehension!