Snowflake
Online AssessmentPython: Documentation Generator
Implement the get_doc(fun: Callable) -> str function that automatically generates documentation for a given function.
The input function fun has the following characteristics:
• It has 0 or more positional parameters.
• Its parameters have type annotations of either int or str.
• Its return type has a type annotation of either int or str.
The returned documentation string must follow this format:
function name
<name_of_fun>
params (<num_of_params>)
<param_1_name> <param_1_type_annotation>
<param_2_name> <param_2_type_annotation>
...
return type
<return_type_annotation>
example usage
<name_of_fun>(<param_1_example>, <param_2_example>, ...) -> <return_value_of_calling_fun_with_given_arguments>
Rules for example values:
• int parameters are consecutive positive integers starting from 2 (2, 3, 4, ...).
• str parameters are consecutive lowercase English letters starting from 'a' ('a', 'b', 'c', ...).
- ▸A Python Callable `fun` with positional parameters annotated with int or str and return type annotated with int or str.
- ▸A single formatted multiline string matching the documentation template.
- Number of parameters >= 0
- Parameter annotations in {int, str}
- Return type annotation in {int, str}
def increment(x: int) -> int:
return x + 1function name increment params (1) x int return type int example usage increment(2) -> 3
Function name is increment, has 1 int parameter x, return type is int. The int parameter starts with 2, giving increment(2) -> 3.
def repeat_word(word: str, count: int) -> str:
return word * countfunction name
repeat_word
params (2)
word str
count int
return type
str
example usage
repeat_word('a', 2) -> aaword receives 'a' and count receives 2. Calling repeat_word('a', 2) evaluates to 'aa'.
Use Python's inspect.signature(fun) to extract parameter names and annotations. Maintain counters for consecutive integer and string arguments, invoke fun(*args) to compute the example output, and join lines with 2-space indentation.