Skip to content

πŸ‘©β€πŸ’» Coding Assistant

Abstract

This section covers how to craft prompts to use chatbots for various coding tasks like code generation and code documentation generation.

Along with text, chatbots are also trained on large volumes of code and hence chatbots are equipped with good coding skills along with good writing skills. With good coding skills, chatbots can serve as valuable coding assistants in various ways, helping both beginners and experienced programmers with their coding tasks. Chatbots can help programmers in a variety of coding tasks like code generation, code documentation etc. Let us see in detail how to craft prompts to use chatbots for various coding tasks effectively.

βœ… Code Generation

A programmer’s job involves writing a lot of code. Writing code involves a lot of time and effort. Thanks to Chabots, now you need not to write code from scratch. Given a detailed description of what you want, chatbots can generate the code for you. Let us see how to craft a prompt for code generation.

🍿 Example-1

Let us assume that you are given the following coding challenge. You are given a list of numbers, a = [-3, 5, 8, 6, 7]. Swap the adjacent numbers in the list i.e., swap 1st number and second numbers, third and fourth numbers, fifth and sixth numbers etc. As a beginner, you may not be aware of how to do this. Now, you can ask chatbots to do this by using the following prompt.

Prompt

Write Python code for the given programming challenge using functions. 

Programming Challenge: You are given a list of numbers, a = [-3, 5, 8, 6, 7]. Swap the adjacent numbers in 
the list i.e., swap 1st number and second numbers, third and fourth numbers, fifth and sixth numbers etc. 

Constraints: Make sure that the python code works for any input list. Just generate the code without any 
explanations.

Output

def swap_adjacent_numbers(lst):
    for i in range(0, len(lst)-1, 2):
        lst[i], lst[i+1] = lst[i+1], lst[i]

a = [-3, 5, 8, 6, 7]
swap_adjacent_numbers(a)
print(a)

🍿 Example-2

Let us assume that you are given the following coding challenge. You are given a list of numbers, a = [4, 8, 16, 24]. Write Python code to display the perfect square numbers in the given list. For this, you can craft a prompt as shown below.

Prompt

Write Python code for the given programming challenge. 

Programming Challenge: Write Python code to display the perfect square numbers in the given list, 
a = [4, 8, 16, 24]. 

Constraints: Make sure (i) to generate the code using functions and (ii) generated code works for any input 
list. Just generate the code without any explanations.

Output

def is_perfect_square(num):
    return int(num**0.5)**2 == num

def perfect_square_numbers(lst):
    return [x for x in lst if is_perfect_square(x)]

a = [4, 8, 16, 24]
perfect_squares = perfect_square_numbers(a)
print(perfect_squares)

βœ… Code Documentation Generation

Once code is written for a task, the next important step is documenting the code. Documenting the code is a very important step as it enhances the readability of the code and helps others to quickly understand the code. As coding assistants, chatbots can easily understand the code by leveraging their programming knowledge and can generate code documentation by leveraging their natural language generation abilities. Let us see how to craft effective prompts to generate code documentation using chatbots.

🍿 Example-1

Let us assume that you are given the following coding challenge. You are given a list of numbers, a = [-3, 5, 8, 6, 7]. Swap the adjacent numbers in the list i.e., swap 1st number and second numbers, third and fourth numbers, fifth and sixth numbers etc. For this, you can use a Chatbot to generate and understand the following code.

def swap_adjacent_numbers(lst):
    for i in range(0, len(lst)-1, 2):
        lst[i], lst[i+1] = lst[i+1], lst[i]

a = [-3, 5, 8, 6, 7]
swap_adjacent_numbers(a)
print(a)

Now, you have to document this code to enhance its readability. Instead of manually doing it which will take time, you can ask the chatbot to generate documentation using the following prompt.

Prompt

You are an expert Python programmer and trainer. I will give you the Python code to swap adjacent numbers in the list. Generate the documentation for the given code. 
Code: def swap_adjacent_numbers(lst):
    for i in range(0, len(lst)-1, 2):
        lst[i], lst[i+1] = lst[i+1], lst[i]

a = [-3, 5, 8, 6, 7]
swap_adjacent_numbers(a)
print(a)

Documented Code:

Output

def swap_adjacent_numbers(lst):
    """
    Swaps adjacent elements in a list.

    This function takes a list as input and swaps adjacent elements in the list.
    If the list has an odd number of elements, the last element remains unchanged.

    Parameters:
    lst (list): The input list to be modified.

    Returns:
    None: This function modifies the input list in-place and does not return a new list.

    Example:
    >>> a = [-3, 5, 8, 6, 7]
    >>> swap_adjacent_numbers(a)
    >>> print(a)
    [5, -3, 6, 8, 7]
    """
    for i in range(0, len(lst) - 1, 2):
        lst[i], lst[i + 1] = lst[i + 1], lst[i]

a = [-3, 5, 8, 6, 7]
swap_adjacent_numbers(a)
print(a)

🍿 Example-2

Let us assume that you are given the following coding challenge. You are given a list of numbers, a = [4, 8, 16, 24]. Write Python code to find the perfect numbers in the list. For this, you can write the code or use the chatbot to generate the following code.

def is_perfect_square(num):
    return int(num**0.5)**2 == num

def perfect_square_numbers(lst):
    return [x for x in lst if is_perfect_square(x)]

a = [4, 8, 16, 24]
perfect_squares = perfect_square_numbers(a)
print(perfect_squares)

Now you can ask the chatbot to generate the documentation using the following prompt.

Prompt

You are an expert Python programmer and trainer. I will give you the Python code to to find the perfect numbers 
in the list. Generate the documentation for the given code. 

Code: def is_perfect_square(num):
    return int(num**0.5)**2 == num

def perfect_square_numbers(lst):
    return [x for x in lst if is_perfect_square(x)]

a = [4, 8, 16, 24]
perfect_squares = perfect_square_numbers(a)
print(perfect_squares)

Constraints: Just generate the documented code, don’t give code explanations. 

Documented Code:

Output

def is_perfect_square(num):
    """
    Check if a given number is a perfect square.

    Args:
        num (int): The number to be checked.

    Returns:
        bool: True if 'num' is a perfect square, False otherwise.
    """
    return int(num**0.5)**2 == num

def perfect_square_numbers(lst):
    """
    Find perfect square numbers in a list.

    Args:
        lst (list): A list of integers.

    Returns:
        list: A new list containing only the perfect square numbers from the input list.
    """
    return [x for x in lst if is_perfect_square(x)]

a = [4, 8, 16, 24]
perfect_squares = perfect_square_numbers(a)
print(perfect_squares)