Batching

The questions below are due on Thursday July 01, 2021; 11:00:00 AM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

So far in these exercises, you've mainly implemented one function per exercise. You'll reap even bigger benefits from writing your code in functions once your programs grow large and complex enough to contain multiple functions. In preparation for that, we want to introduce a useful form of documentation: the docstring.

A Python function docstring is a comment string, conventially encased in triple quotes, on the first line after the def statement of a function. The docstring describes the input(s) and output of the function. Someone reading your code should be able to figure out a function's behavior just by reading its docstring, without examining your code. You should write docstrings for any functions which you expect to reuse or share with others.

To illustrate the value of a docstring, for this problem, we've described the function you should implement via the docstring below.

First read the docstring below, but we also provide a few examples:1:

  • batch((13, 2, 3, 4, 3, 1, 1, 1, 4, 2, 3), 5) returns [[13], [2, 3], [4, 3], [1, 1, 1, 4], [2, 3]]

  • batch((6, 7, 6, 8, 6), 7) returns [[6, 7], [6, 8], [6]]

  • batch((4, 4), 4) returns [[4], [4]]

To check your understanding, after reading through the docstring, please answer this checker. To clarify, we make the batch by adding in elements from the input tuple until the batch reaches the size amount. Then we stop that batch and begin the next one. If this is confusing, please take a look at the answer and explanation after getting a 100% on the checker.

What should be the returned value of the call batch((1, 8, 3, 4, 3, 2, 4), 8)

Next Exercise: Second Largest

Back to exercises


 
Footnotes

1There is in fact a nice way to integrate these example tests into docstrings themselves: doctests. But, we settle for manual separate examples here. (click to return to text)