{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0683c00e-bd2d-430f-a4f0-629457bfcc0c", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "fbcb8ef4a4754556991458d17072576a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value='
>\n", " You are a helpful coding assistant that provides code based on the given query in context. \n", "<>\n", "Write a python program to perform binary search in a given list.[/INST] Of course! Here is a basic implementation of a binary search algorithm in Python:\n", "```\n", "def binary_search(list, target):\n", " # Initialize the indices of the left and right halves of the list\n", " low = 0\n", " high = len(list) - 1\n", "\n", " # While the list has not been searched thoroughly\n", " while low < high:\n", " # Calculate the index of the middle element of the list\n", " mid = (low + high) // 2\n", "\n", " # Check whether the target is equal to the middle element\n", " if list[mid] == target:\n", " # Return the index of the target\n", " return mid\n", "\n", " # If the target is less than the middle element, move to the right\n", " elif list[mid] < target:\n", " high = mid - 1\n", " # If the target is greater than the middle element, move to the left\n", " else:\n", " low = mid + 1\n", "\n", " # If we reach this point, the target was not found in the list\n", " return -1\n", "\n", "# Example usage\n", "list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "target = 5\n", "print(binary_search(list, target))\n", "```\n", "This code uses a simple loop to perform the binary search. It iterates through the list, comparing each element to the target, and adjusting the position of the current element based on the result of the comparison. At the end of the loop, it returns the index of the target element if it is found, or -1 if it is not found.\n", "You can test the code with your own list by replacing the `list` variable with a list of your own, and calling the `binary_search` function with the target element you want to search for. For example, if your list is `['apple', 'banana', 'orange', 'banana', 'orange', 'apple']`, and you want to search for the element 'banana', you can call the function like this: `print(binary_search(list, 'banana')`.\n" ] } ], "source": [ "conversation_text = '''[INST]<>\n", " You are a helpful coding assistant that provides code based on the given query in context. \n", "<>\n", "Write a python program to perform binary search in a given list.[/INST]'''\n", "\n", "sequences = pipeline(\n", " conversation_text,\n", " do_sample=True,\n", " top_k=10,\n", " num_return_sequences=1,\n", " eos_token_id=tokenizer.eos_token_id,\n", " max_length=2048,\n", ")\n", "for seq in sequences:\n", " print(f\"Result: {seq['generated_text']}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "e85dd028-e20d-496d-a5ac-6835ecb3e212", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }