from dotenv import load_dotenv
import google.generativeai as genai
import os
load_dotenv()
genai.configure(api_key=os.environ['API_KEY'])
MODEL_NAME_LATEST = os.environ['MODEL_NAME_LATEST']
model = genai.GenerativeModel(
model_name=MODEL_NAME_LATEST,
tools='code_execution')
response = model.generate_content((
'Implement different sorting algorithms (bubble sort, insertion sort, merge sort, quick sort) and compare their '
'performance on different datasets.'
'Generate and run code for the experiment, and ensure that the datasets are not the same.'))
print(response)
print(response.text)
response = model.generate_content((
'Implement binary search and explore its variations (ternary search, exponential search)'
'Generate and run code for the experiment.'))
Recitation refers to a mystery algorithm that looks for verbatim reproduction of training corpus material, in an attempt to lessen the reproduction of potentially copyrighted text. If the model sings “Obla di obla da” too many times, the output will shut down.
It is possible that some sort algorithm that was scraped multiple times is being played back for you after the token sequence becomes obvious. You could modify the code-writing instruction, such as "Write well-commented Python using type hints, where variable names all start with “sort_”, in order to throw some originality into what is being generated.
I have tried what @Jay has suggested and even added an extension to prompt on how much each dataset should be and seems to work as expected.
import google.generativeai as genai
import os
from google.colab import userdata
GOOGLE_API_KEY = userdata.get ('GOOGLE_API_KEY')
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel(
model_name="gemini-1.5-pro-latest",
tools="code_execution"
)
response = model.generate_content((
"""Implement different sorting algorithms (bubble sort, insertion sort, merge sort, quick sort) and compare their
performance on different datasets in Python with well written docstrings and type hints, where variable names all start with “sort_”. Generate and run code for the experiment, and ensure that the datasets are not the same.
For example for each algorithm take around 10,000 different randomly generated integer numbers and evaluate the results.
"""))
print(response)
print(response.text)
response:
response:
GenerateContentResponse(
done=True,
iterator=None,
result=protos.GenerateContentResponse({
"candidates": [
{
"content": {
"parts": [
{
"text": "```python\nimport random\nimport time\nfrom typing import List\n\n\ndef sort_generate_random_list(sort_list_size: int) -> List[int]:\n \"\"\"Generate a list of random integers.\n\n Args:\n sort_list_size (int): The size of the list to generate.\n\n Returns:\n List[int]: A list of random integers.\n \"\"\"\n return random.sample(range(1, sort_list_size + 1), sort_list_size)\n\n\ndef sort_bubble_sort(sort_list: List[int]) -> List[int]:\n \"\"\"Sort a list of integers using bubble sort.\n\n Args:\n sort_list (List[int]): The list to sort.\n\n Returns:\n List[int]: The sorted list.\n \"\"\"\n sort_n = len(sort_list)\n for sort_i in range(sort_n):\n sort_swapped = False\n for sort_j in range(0, sort_n - sort_i - 1):\n if sort_list[sort_j] > sort_list[sort_j + 1]:\n sort_list[sort_j], sort_list[sort_j + 1] = sort_list[sort_j + 1], sort_list[sort_j]\n sort_swapped = True\n if not sort_swapped:\n break\n return sort_list\n\n\ndef sort_insertion_sort(sort_list: List[int]) -> List[int]:\n \"\"\"Sort a list of integers using insertion sort.\n\n Args:\n sort_list (List[int]): The list to sort.\n\n Returns:\n List[int]: The sorted list.\n \"\"\"\n for sort_i in range(1, len(sort_list)):\n sort_key = sort_list[sort_i]\n sort_j = sort_i - 1\n while sort_j >= 0 and sort_key < sort_list[sort_j]:\n sort_list[sort_j + 1] = sort_list[sort_j]\n sort_j -= 1\n sort_list[sort_j + 1] = sort_key\n return sort_list\n\n\ndef sort_merge_sort(sort_list: List[int]) -> List[int]:\n \"\"\"Sort a list of integers using merge sort.\n\n Args:\n sort_list (List[int]): The list to sort.\n\n Returns:\n List[int]: The sorted list.\n \"\"\"\n if len(sort_list) > 1:\n sort_mid = len(sort_list) // 2\n sort_left_half = sort_list[:sort_mid]\n sort_right_half = sort_list[sort_mid:]\n\n sort_merge_sort(sort_left_half)\n sort_merge_sort(sort_right_half)\n\n sort_i = sort_j = sort_k = 0\n while sort_i < len(sort_left_half) and sort_j < len(sort_right_half):\n if sort_left_half[sort_i] < sort_right_half[sort_j]:\n sort_list[sort_k] = sort_left_half[sort_i]\n sort_i += 1\n else:\n sort_list[sort_k] = sort_right_half[sort_j]\n sort_j += 1\n sort_k += 1\n\n while sort_i < len(sort_left_half):\n sort_list[sort_k] = sort_left_half[sort_i]\n sort_i += 1\n sort_k += 1\n\n while sort_j < len(sort_right_half):\n sort_list[sort_k] = sort_right_half[sort_j]\n sort_j += 1\n sort_k += 1\n return sort_list\n\n\ndef sort_quick_sort(sort_list: List[int]) -> List[int]:\n \"\"\"Sort a list of integers using quick sort.\n\n Args:\n sort_list (List[int]): The list to sort.\n\n Returns:\n List[int]: The sorted list.\n \"\"\"\n if len(sort_list) < 2:\n return sort_list\n sort_pivot = sort_list[0]\n sort_less = [sort_i for sort_i in sort_list[1:] if sort_i <= sort_pivot]\n sort_greater = [sort_i for sort_i in sort_list[1:] if sort_i > sort_pivot]\n return sort_quick_sort(sort_less) + [sort_pivot] + sort_quick_sort(sort_greater)\n\n\ndef sort_run_experiment(sort_list_size: int) -> None:\n \"\"\"Generate random lists and compare the performance of different sorting algorithms.\n\n Args:\n sort_list_size (int): The size of the lists to generate.\n \"\"\"\n sort_list1 = sort_generate_random_list(sort_list_size)\n sort_list2 = sort_list1.copy()\n sort_list3 = sort_list1.copy()\n sort_list4 = sort_list1.copy()\n\n sort_start_time = time.time()\n sort_bubble_sort(sort_list1)\n sort_end_time = time.time()\n sort_bubble_sort_time = sort_end_time - sort_start_time\n\n sort_start_time = time.time()\n sort_insertion_sort(sort_list2)\n sort_end_time = time.time()\n sort_insertion_sort_time = sort_end_time - sort_start_time\n\n sort_start_time = time.time()\n sort_merge_sort(sort_list3)\n sort_end_time = time.time()\n sort_merge_sort_time = sort_end_time - sort_start_time\n\n sort_start_time = time.time()\n sort_quick_sort(sort_list4)\n sort_end_time = time.time()\n sort_quick_sort_time = sort_end_time - sort_start_time\n\n print(f\"List size: {sort_list_size}\")\n print(f\"Bubble Sort Time: {sort_bubble_sort_time:.6f} seconds\")\n print(f\"Insertion Sort Time: {sort_insertion_sort_time:.6f} seconds\")\n print(f\"Merge Sort Time: {sort_merge_sort_time:.6f} seconds\")\n print(f\"Quick Sort Time: {sort_quick_sort_time:.6f} seconds\\n\")\n\n\nif __name__ == \"__main__\":\n sort_run_experiment(10000)\n```"
}
],
"role": "model"
},
"finish_reason": "STOP",
"index": 0,
"safety_ratings": [
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"probability": "NEGLIGIBLE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"probability": "NEGLIGIBLE"
}
]
}
],
"usage_metadata": {
"prompt_token_count": 95,
"candidates_token_count": 1602,
"total_token_count": 1697
}
}),
)
```python
import random
import time
from typing import List
def sort_generate_random_list(sort_list_size: int) -> List[int]:
"""Generate a list of random integers.
Args:
sort_list_size (int): The size of the list to generate.
Returns:
List[int]: A list of random integers.
"""
return random.sample(range(1, sort_list_size + 1), sort_list_size)
def sort_bubble_sort(sort_list: List[int]) -> List[int]:
"""Sort a list of integers using bubble sort.
Args:
sort_list (List[int]): The list to sort.
Returns:
List[int]: The sorted list.
"""
sort_n = len(sort_list)
for sort_i in range(sort_n):
sort_swapped = False
for sort_j in range(0, sort_n - sort_i - 1):
if sort_list[sort_j] > sort_list[sort_j + 1]:
sort_list[sort_j], sort_list[sort_j + 1] = sort_list[sort_j + 1], sort_list[sort_j]
sort_swapped = True
if not sort_swapped:
break
return sort_list
def sort_insertion_sort(sort_list: List[int]) -> List[int]:
"""Sort a list of integers using insertion sort.
Args:
sort_list (List[int]): The list to sort.
Returns:
List[int]: The sorted list.
"""
for sort_i in range(1, len(sort_list)):
sort_key = sort_list[sort_i]
sort_j = sort_i - 1
while sort_j >= 0 and sort_key < sort_list[sort_j]:
sort_list[sort_j + 1] = sort_list[sort_j]
sort_j -= 1
sort_list[sort_j + 1] = sort_key
return sort_list
def sort_merge_sort(sort_list: List[int]) -> List[int]:
"""Sort a list of integers using merge sort.
Args:
sort_list (List[int]): The list to sort.
Returns:
List[int]: The sorted list.
"""
if len(sort_list) > 1:
sort_mid = len(sort_list) // 2
sort_left_half = sort_list[:sort_mid]
sort_right_half = sort_list[sort_mid:]
sort_merge_sort(sort_left_half)
sort_merge_sort(sort_right_half)
sort_i = sort_j = sort_k = 0
while sort_i < len(sort_left_half) and sort_j < len(sort_right_half):
if sort_left_half[sort_i] < sort_right_half[sort_j]:
sort_list[sort_k] = sort_left_half[sort_i]
sort_i += 1
else:
sort_list[sort_k] = sort_right_half[sort_j]
sort_j += 1
sort_k += 1
while sort_i < len(sort_left_half):
sort_list[sort_k] = sort_left_half[sort_i]
sort_i += 1
sort_k += 1
while sort_j < len(sort_right_half):
sort_list[sort_k] = sort_right_half[sort_j]
sort_j += 1
sort_k += 1
return sort_list
def sort_quick_sort(sort_list: List[int]) -> List[int]:
"""Sort a list of integers using quick sort.
Args:
sort_list (List[int]): The list to sort.
Returns:
List[int]: The sorted list.
"""
if len(sort_list) < 2:
return sort_list
sort_pivot = sort_list[0]
sort_less = [sort_i for sort_i in sort_list[1:] if sort_i <= sort_pivot]
sort_greater = [sort_i for sort_i in sort_list[1:] if sort_i > sort_pivot]
return sort_quick_sort(sort_less) + [sort_pivot] + sort_quick_sort(sort_greater)
def sort_run_experiment(sort_list_size: int) -> None:
"""Generate random lists and compare the performance of different sorting algorithms.
Args:
sort_list_size (int): The size of the lists to generate.
"""
sort_list1 = sort_generate_random_list(sort_list_size)
sort_list2 = sort_list1.copy()
sort_list3 = sort_list1.copy()
sort_list4 = sort_list1.copy()
sort_start_time = time.time()
sort_bubble_sort(sort_list1)
sort_end_time = time.time()
sort_bubble_sort_time = sort_end_time - sort_start_time
sort_start_time = time.time()
sort_insertion_sort(sort_list2)
sort_end_time = time.time()
sort_insertion_sort_time = sort_end_time - sort_start_time
sort_start_time = time.time()
sort_merge_sort(sort_list3)
sort_end_time = time.time()
sort_merge_sort_time = sort_end_time - sort_start_time
sort_start_time = time.time()
sort_quick_sort(sort_list4)
sort_end_time = time.time()
sort_quick_sort_time = sort_end_time - sort_start_time
print(f"List size: {sort_list_size}")
print(f"Bubble Sort Time: {sort_bubble_sort_time:.6f} seconds")
print(f"Insertion Sort Time: {sort_insertion_sort_time:.6f} seconds")
print(f"Merge Sort Time: {sort_merge_sort_time:.6f} seconds")
print(f"Quick Sort Time: {sort_quick_sort_time:.6f} seconds\n")
if __name__ == "__main__":
sort_run_experiment(10000)