Ads Area

Lesson 2.1: Prompts for Code Generation and Debugging | Free Prompt Engineering Course For Developers

Writing Prompts for Code Snippets or Functions

In this lesson, you will learn how to write clear and effective prompts to generate code snippets or functions using ChatGPT. The goal is to create concise, accurate prompts that guide the AI in generating relevant code.

A prompt should specify the type of code, language, and any necessary constraints. For example:

# Example 1: Generate a Python function to calculate the factorial of a number
"Generate a Python function that calculates the factorial of a given number using recursion."
      

Another example could be:

# Example 2: Generate a JavaScript function for sorting an array
"Write a JavaScript function to sort an array of integers in ascending order using the bubble sort algorithm."
      

Refining Prompts for Complex Coding Tasks

For complex coding tasks, it's essential to refine your prompts to be very specific. In these cases, you should break down the task into smaller, manageable components and provide clear instructions for each one. Here's an example:

# Example 3: Generate a Python function that connects to a MySQL database and retrieves data
"Write a Python function that connects to a MySQL database using the MySQL Connector library. The function should accept a query string as input, execute the query, and return the results."
      

In this case, the prompt specifies the database type, library, and expected functionality.

# Example 4: JavaScript function to fetch data from an API and display it
"Create a JavaScript function that fetches data from a public API and displays it in the browser's console. The API should be 'https://jsonplaceholder.typicode.com/users'."
      

Exercises: Coding Prompts for Python, JavaScript Functions

Here are some exercises to practice writing prompts for coding tasks:

  • Write a Python function that accepts a list of numbers and returns the largest number.
  • Write a JavaScript function that converts a string to title case (first letter of each word capitalized).
  • Create a Python function that reverses a string without using the built-in reverse function.
  • Write a JavaScript function to check if a given string is a palindrome.
  • Write a Python function that merges two sorted lists into one sorted list.

Example Prompts with Responses

Here are 10 example prompts you can use to generate code and debug:

# Example 5: Python function to find prime numbers
"Write a Python function that takes an integer as input and returns True if the number is prime, False otherwise."
Response: 
def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, num):
        if num % i == 0:
            return False
    return True
      
# Example 6: JavaScript function to find the factorial of a number
"Create a JavaScript function to find the factorial of a given number."
Response:
function factorial(n) {
    if (n === 0) return 1;
    return n * factorial(n - 1);
}
      
# Example 7: Python program to check if a number is even or odd
"Write a Python program that checks if a number is even or odd."
Response:
def is_even_or_odd(num):
    if num % 2 == 0:
        return 'Even'
    else:
        return 'Odd'
      
# Example 8: JavaScript function to calculate the sum of all elements in an array
"Write a JavaScript function to sum all the elements in an array."
Response:
function sumArray(arr) {
    return arr.reduce((sum, current) => sum + current, 0);
}
      
# Example 9: Python code to reverse a list
"Write a Python program that reverses a list without using the built-in reverse method."
Response:
def reverse_list(lst):
    return lst[::-1]
      
# Example 10: JavaScript function to remove duplicates from an array
"Write a JavaScript function to remove duplicates from an array."
Response:
function removeDuplicates(arr) {
    return [...new Set(arr)];
}
      
# Example 11: Python function to merge two dictionaries
"Create a Python function to merge two dictionaries."
Response:
def merge_dicts(dict1, dict2):
    dict1.update(dict2)
    return dict1
      
# Example 12: JavaScript function to format a date
"Write a JavaScript function that formats a date in 'YYYY-MM-DD' format."
Response:
function formatDate(date) {
    let d = new Date(date);
    return d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2);
}
      
# Example 13: Python function to convert a string to uppercase
"Write a Python function that converts a string to uppercase."
Response:
def to_uppercase(s):
    return s.upper()
      
# Example 14: JavaScript function to find the length of the longest string in an array
"Write a JavaScript function that finds the longest string in an array."
Response:
function findLongestString(arr) {
    return arr.reduce((longest, current) => current.length > longest.length ? current : longest, "");
}
      

Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad

Ads Area