Skip to content

Function Generator

Ask Nova to create Python functions based on detailed specifications.

System Prompt Template

You are an expert {programming language} developer who specializes in writing clean, efficient, and well-documented functions.
Follow these instructions:
{enumerated instructions}

User Prompt Template

## Requirements ##
Create a function called 'group_by_frequency' that takes a list of any hashable items and returns a dictionary where:
{list of function abilities}
For example: {psuedocode for function return value}

Example

Amazon Nova Pro System Prompt

You are an expert Python developer who specializes in writing clean, efficient, and well-documented functions.
Follow these instructions:
1. Read and understand the function requirements in ## Requirements ##
2. Write a brief analysis of the requirements in ## Analysis ##, including:
    - Input/output specifications
    - Edge cases to handle
    - Any potential optimizations
3. Write the Python function in ## Code ## using ```python``` code blocks
4. Include docstring documentation and type hints
5. Add example usage in ## Example ## using ```python``` code blocks

Amazon Nova Pro User Prompt

## Requirements ##
Create a function called 'group_by_frequency' that takes a list of any hashable items and returns a dictionary where:
- Keys are the frequencies (number of occurrences)
- Values are lists of items that appear that many times
- The items in each list should be sorted in their natural order
- Empty input should return an empty dictionary
For example: group_by_frequency([1,1,2,2,2,3]) should return {1: [3], 2: [1], 3: [2]}

Amazon Nova Pro Sample Response

Response

Analysis

The function group_by_frequency needs to process a list of hashable items and produce a dictionary where: - The keys are the frequencies of the items in the input list. - The values are lists of items that appear with that frequency. - The items within each list should be sorted in their natural order. - If the input list is empty, the function should return an empty dictionary.

Input/Output Specifications

  • Input: A list of hashable items (e.g., integers, strings, tuples).
  • Output: A dictionary where:
  • Keys are integers representing frequencies.
  • Values are lists of items that appear with that frequency, sorted in natural order.

Edge Cases to Handle

  1. Empty Input: The function should return an empty dictionary if the input list is empty.
  2. Single Unique Item: If the list contains only one unique item, the result should reflect that item's frequency.
  3. All Items Same Frequency: If all items appear the same number of times, the result should have a single key-value pair.
  4. Mixed Frequencies: The function should correctly handle lists with items of varying frequencies.

Potential Optimizations

  • Use collections.Counter to efficiently count the frequencies of items.
  • Use collections.defaultdict to group items by their frequencies.
  • Sort the items within each frequency group to ensure they are in natural order.

Code

from collections import Counter, defaultdict

def group_by_frequency(items: list) -> dict:
    """
    Group items by their frequency of occurrence in the input list.

    Parameters:
    items (list): A list of hashable items.

    Returns:
    dict: A dictionary where keys are frequencies and values are lists of items
          that appear with that frequency, sorted in natural order.
    """
    if not items:
        return {}

    # Count the frequency of each item
    frequency_count = Counter(items)

    # Group items by their frequency
    grouped_by_frequency = defaultdict(list)
    for item, frequency in frequency_count.items():
        grouped_by_frequency[frequency].append(item)

    # Sort items within each frequency group
    for frequency in grouped_by_frequency:
        grouped_by_frequency[frequency].sort()

    # Convert defaultdict to regular dict
    return dict(grouped_by_frequency)

## Example ##
```python
# Example usage of the group_by_frequency function
print(group_by_frequency([1, 1, 2, 2, 2, 3]))  # Output: {1: [3], 2: [1], 3: [2]}
print(group_by_frequency([]))                  # Output: {}
print(group_by_frequency([1]))                  # Output: {1: [1]}
print(group_by_frequency([1, 1, 2, 2, 3, 3]))  # Output: {2: [1, 3], 3: [2]}

API Request

import json
import boto3

bedrock_runtime = boto3.client(
            service_name="bedrock-runtime",
            region_name="us-west-2"
        )

response = bedrock_runtime.converse(
                modelId='us.amazon.nova-pro-v1:0',
                system = [
                    {
                        "text": """You are an expert Python developer who specializes in writing clean, efficient, and well-documented functions.
                            Follow these instructions:
                            1. Read and understand the function requirements in ## Requirements ##
                            2. Write a brief analysis of the requirements in ## Analysis ##, including:
                               - Input/output specifications
                               - Edge cases to handle
                               - Any potential optimizations
                            3. Write the Python function in ## Code ## using ```python``` code blocks
                            4. Include docstring documentation and type hints
                            5. Add example usage in ## Example ## using ```python``` code blocks
                            """
                    }
                ],
                messages = [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": """## Requirements ##
                                Create a function called 'group_by_frequency' that takes a list of any hashable items and returns a dictionary where:
                                - Keys are the frequencies (number of occurrences)
                                - Values are lists of items that appear that many times
                                - The items in each list should be sorted in their natural order
                                - Empty input should return an empty dictionary
                                For example: group_by_frequency([1,1,2,2,2,3]) should return {1: [3], 2: [1], 3: [2]}"""
                        }
                    ]
                }
            ],
            inferenceConfig={
                "temperature": 0.1,
                "topP": .99,
                "maxTokens": 1024
            }
            )

print(json.dumps(response, indent=2))
aws bedrock-runtime converse \
  --model-id "us.amazon.nova-pro-v1:0" \
  --system '[
    {
      "text": "You are an expert Python developer who specializes in writing clean, efficient, and well-documented functions.\nFollow these instructions:\n1. Read and understand the function requirements in ## Requirements ##\n2. Write a brief analysis of the requirements in ## Analysis ##, including:\n   - Input/output specifications\n   - Edge cases to handle\n   - Any potential optimizations\n3. Write the Python function in ## Code ## using ```python``` code blocks\n4. Include docstring documentation and type hints\n5. Add example usage in ## Example ## using ```python``` code blocks"
    }
  ]' \
  --messages '[
    {
      "role": "user",
      "content": [
        {
          "text": "## Requirements ##\nCreate a function called '\''group_by_frequency'\'' that takes a list of any hashable items and returns a dictionary where:\n- Keys are the frequencies (number of occurrences)\n- Values are lists of items that appear that many times\n- The items in each list should be sorted in their natural order\n- Empty input should return an empty dictionary\nFor example: group_by_frequency([1,1,2,2,2,3]) should return {1: [3], 2: [1], 3: [2]}"
        }
      ]
    }
  ]' \
  --inference-config '{
    "temperature": 0.1,
    "topP": 0.99,
    "maxTokens": 512
  }' \
  --region us-west-2

json { "modelId": "us.amazon.nova-pro-v1:0", "system": [ { "text": "You are an expert Python developer who specializes in writing clean, efficient, and well-documented functions.\nFollow these instructions:\n1. Read and understand the function requirements in ## Requirements ##\n2. Write a brief analysis of the requirements in ## Analysis ##, including:\n - Input/output specifications\n - Edge cases to handle\n - Any potential optimizations\n3. Write the Python function in ## Code ## usingpythoncode blocks\n4. Include docstring documentation and type hints\n5. Add example usage in ## Example ## usingpython``` code blocks" } ], "messages": [ { "role": "user", "content": [ { "text": "## Requirements ##\nCreate a function called 'group_by_frequency' that takes a list of any hashable items and returns a dictionary where:\n- Keys are the frequencies (number of occurrences)\n- Values are lists of items that appear that many times\n- The items in each list should be sorted in their natural order\n- Empty input should return an empty dictionary\nFor example: group_by_frequency([1,1,2,2,2,3]) should return {1: [3], 2: [1], 3: [2]}" } ] } ], "inferenceConfig": { "temperature": 0.1, "topP": 0.99, "maxTokens": 512 } }