{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "639168a7-1fd7-4585-a665-95bcc723108f", "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "import matplotlib.ticker as ticker\n", "from matplotlib.ticker import MultipleLocator\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "id": "de43d55b-fe6a-4677-9e2e-2af7edf8538c", "metadata": {}, "outputs": [], "source": [ "# Constant: The full search interval is (1, MAX_VALUE)\n", "MAX_VALUE = 1024" ] }, { "cell_type": "code", "execution_count": null, "id": "83c1f4db-09a8-424e-a7d9-871dac77c82b", "metadata": {}, "outputs": [], "source": [ "def binary_search(n):\n", " \"\"\"\n", " Perform a binary search for the value n.\n", " Return the count of probes until found.\n", " \"\"\"\n", " # Initial interval bounds.\n", " low = 0\n", " high = MAX_VALUE\n", " \n", " count = 0\n", " middle = -1\n", " \n", " while (middle != n) and (count < 11):\n", " middle = (low + high)//2\n", " count += 1\n", "\n", " if middle < n:\n", " low = middle # search the upper half next\n", " elif middle > n:\n", " high = middle # search the lower half next\n", " \n", " return count" ] }, { "cell_type": "code", "execution_count": null, "id": "e319c078-5698-4879-aa6f-5c1448c6d39b", "metadata": {}, "outputs": [], "source": [ "counts = [0]*MAX_VALUE # list of counts of probes for the values \n", "colors = ['BLUE']*MAX_VALUE # list of colors of the horizonal bars\n", "frequencies = [0]*11 # list of frequencies of the counts\n", "\n", "# Search for each value n in the range.\n", "for n in range(1, MAX_VALUE):\n", " count = binary_search(n)\n", " \n", " # Set the count for this value\n", " # and update the count's frequency.\n", " counts[n] = count \n", " frequencies[count] += 1\n", " \n", " # Determine the horizontal bar's color\n", " # for this value based on its count.\n", " if count < 3:\n", " colors[n] = 'RED'\n", " elif count < 6:\n", " colors[n] = 'ORANGE'" ] }, { "cell_type": "code", "execution_count": null, "id": "ec89d43a-4457-4230-9fa0-82723773ae4d", "metadata": {}, "outputs": [], "source": [ "# A simple bar chart of the count frequencies.\n", "plt.bar(x=range(0, 11), height=frequencies)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "e57e41b3-79ad-4691-854a-f64789598e03", "metadata": {}, "outputs": [], "source": [ "# A horizontal bar chart of each value's probe count.\n", "\n", "# Set the graph's size.\n", "fig = plt.figure()\n", "fig.set_size_inches(6, 150)\n", "\n", "# Set the axes' tick intervals.\n", "ax = fig.add_subplot(111)\n", "ax.xaxis.set_major_locator(ticker.MultipleLocator(1)) # x axis\n", "ax.yaxis.set_major_locator(ticker.MultipleLocator(2)) # y axis\n", "\n", "# Set the lower and upper limits of the y axis.\n", "plt.ylim(0, MAX_VALUE)\n", "\n", "# Create a horizontal bar chart.\n", "# Assign a color to each bar based on its count.\n", "plt.barh(y=range(0, MAX_VALUE), width=counts, height=0.8,\n", " color=colors)\n", "\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "621a57f6-57e4-4437-b082-1da85a4c5b68", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }