Snowflake

Online Assessment
Online Assessment//Python: Documentation Generator
Q2
Medium

Python: Documentation Generator

Python IntrospectionInspect ModuleType AnnotationsCallables
Set 1 • 90 min
Problem Statement

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', ...).

Input Format
  • ▸A Python Callable `fun` with positional parameters annotated with int or str and return type annotated with int or str.
Output Format
  • ▸A single formatted multiline string matching the documentation template.
Constraints
  • Number of parameters >= 0
  • Parameter annotations in {int, str}
  • Return type annotation in {int, str}
Sample Test Cases
Example 1
Input
def increment(x: int) -> int:
    return x + 1
Output
function name
  increment
params (1)
  x int
return type
  int
example usage
  increment(2) -> 3
Explanation

Function name is increment, has 1 int parameter x, return type is int. The int parameter starts with 2, giving increment(2) -> 3.

Example 2
Input
def repeat_word(word: str, count: int) -> str:
    return word * count
Output
function name
  repeat_word
params (2)
  word str
  count int
return type
  str
example usage
  repeat_word('a', 2) -> aa
Explanation

word receives 'a' and count receives 2. Calling repeat_word('a', 2) evaluates to 'aa'.

Complexity
TimeO(P) where P is the number of parameters
SpaceO(P) auxiliary space for constructing the output lines
Expected Approach

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.