small fixes in puthon code documentation assistant
This commit is contained in:
@@ -69,17 +69,17 @@
|
|||||||
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
|
"google_api_key = os.getenv('GOOGLE_API_KEY')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if openai_api_key:\n",
|
"if openai_api_key:\n",
|
||||||
" print(f\"OpenAI API Key exists and begins {openai_api_key[:8]}\")\n",
|
" print(f\"OpenAI API Key exists and begins with: {openai_api_key[:8]}\")\n",
|
||||||
"else:\n",
|
"else:\n",
|
||||||
" print(\"OpenAI API Key not set\")\n",
|
" print(\"OpenAI API Key not set\")\n",
|
||||||
" \n",
|
" \n",
|
||||||
"if anthropic_api_key:\n",
|
"if anthropic_api_key:\n",
|
||||||
" print(f\"Anthropic API Key exists and begins {anthropic_api_key[:7]}\")\n",
|
" print(f\"Anthropic API Key exists and begins with: {anthropic_api_key[:7]}\")\n",
|
||||||
"else:\n",
|
"else:\n",
|
||||||
" print(\"Anthropic API Key not set\")\n",
|
" print(\"Anthropic API Key not set\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if google_api_key:\n",
|
"if google_api_key:\n",
|
||||||
" print(f\"Google API Key exists and begins {google_api_key[:4]}\")\n",
|
" print(f\"Google API Key exists and begins with: {google_api_key[:4]}\")\n",
|
||||||
"else:\n",
|
"else:\n",
|
||||||
" print(\"Google API Key not set\")"
|
" print(\"Google API Key not set\")"
|
||||||
]
|
]
|
||||||
@@ -475,6 +475,127 @@
|
|||||||
"exec(open(claude_hard).read())"
|
"exec(open(claude_hard).read())"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "7f60d33c-f6b7-4fc5-bc2b-57957b076e34",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
"This module implements a Linear Congruential Generator (LCG) and uses it\n",
|
||||||
|
"to generate random numbers for calculating the maximum subarray sum.\n",
|
||||||
|
"It includes functions for the LCG, finding the maximum subarray sum, and\n",
|
||||||
|
"aggregating results over multiple runs.\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"\n",
|
||||||
|
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Implements a Linear Congruential Generator (LCG) to produce a sequence of\n",
|
||||||
|
" pseudorandom numbers.\n",
|
||||||
|
"\n",
|
||||||
|
" The generator uses the formula: X_{n+1} = (a * X_n + c) % m.\n",
|
||||||
|
"\n",
|
||||||
|
" Args:\n",
|
||||||
|
" seed (int): The initial seed value for the generator (X_0).\n",
|
||||||
|
" a (int, optional): The multiplier. Defaults to 1664525 (common LCG parameter).\n",
|
||||||
|
" c (int, optional): The increment. Defaults to 1013904223 (common LCG parameter).\n",
|
||||||
|
" m (int, optional): The modulus. Defaults to 2**32, meaning numbers will be\n",
|
||||||
|
" between 0 and m-1.\n",
|
||||||
|
"\n",
|
||||||
|
" Yields:\n",
|
||||||
|
" int: The next pseudorandom number in the sequence.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" value = seed\n",
|
||||||
|
" while True:\n",
|
||||||
|
" # Calculate the next pseudorandom number using the LCG formula.\n",
|
||||||
|
" value = (a * value + c) % m\n",
|
||||||
|
" yield value\n",
|
||||||
|
"\n",
|
||||||
|
"def max_subarray_sum(n, seed, min_val, max_val):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Calculates the maximum possible sum of a contiguous subarray within a list\n",
|
||||||
|
" of 'n' pseudorandom numbers.\n",
|
||||||
|
"\n",
|
||||||
|
" The random numbers are generated using an LCG based on the provided seed,\n",
|
||||||
|
" and then mapped to the range [min_val, max_val].\n",
|
||||||
|
" This implementation uses a brute-force approach with O(n^2) complexity.\n",
|
||||||
|
"\n",
|
||||||
|
" Args:\n",
|
||||||
|
" n (int): The number of random integers to generate for the array.\n",
|
||||||
|
" seed (int): The seed for the LCG to generate the random numbers.\n",
|
||||||
|
" min_val (int): The minimum possible value for the generated random numbers.\n",
|
||||||
|
" max_val (int): The maximum possible value for the generated random numbers.\n",
|
||||||
|
"\n",
|
||||||
|
" Returns:\n",
|
||||||
|
" int: The maximum sum found among all contiguous subarrays.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" lcg_gen = lcg(seed)\n",
|
||||||
|
" # Generate a list of 'n' random numbers within the specified range [min_val, max_val].\n",
|
||||||
|
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
|
||||||
|
"\n",
|
||||||
|
" max_sum = float('-inf') # Initialize max_sum to negative infinity to handle all negative numbers.\n",
|
||||||
|
"\n",
|
||||||
|
" # Iterate through all possible starting points of a subarray.\n",
|
||||||
|
" for i in range(n):\n",
|
||||||
|
" current_sum = 0\n",
|
||||||
|
" # Iterate through all possible ending points for the current starting point.\n",
|
||||||
|
" for j in range(i, n):\n",
|
||||||
|
" current_sum += random_numbers[j]\n",
|
||||||
|
" # Update max_sum if the current subarray sum is greater.\n",
|
||||||
|
" if current_sum > max_sum:\n",
|
||||||
|
" max_sum = current_sum\n",
|
||||||
|
" return max_sum\n",
|
||||||
|
"\n",
|
||||||
|
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" Calculates the sum of maximum subarray sums over 20 separate runs.\n",
|
||||||
|
"\n",
|
||||||
|
" Each run generates a new set of 'n' random numbers for `max_subarray_sum`\n",
|
||||||
|
" using a new seed derived from the initial LCG sequence.\n",
|
||||||
|
"\n",
|
||||||
|
" Args:\n",
|
||||||
|
" n (int): The number of random integers for each subarray sum calculation.\n",
|
||||||
|
" initial_seed (int): The initial seed for the LCG that generates seeds\n",
|
||||||
|
" for individual `max_subarray_sum` runs.\n",
|
||||||
|
" min_val (int): The minimum possible value for random numbers in each run.\n",
|
||||||
|
" max_val (int): The maximum possible value for random numbers in each run.\n",
|
||||||
|
"\n",
|
||||||
|
" Returns:\n",
|
||||||
|
" int: The sum of the maximum subarray sums across all 20 runs.\n",
|
||||||
|
" \"\"\"\n",
|
||||||
|
" total_sum = 0\n",
|
||||||
|
" lcg_gen = lcg(initial_seed) # LCG to generate seeds for subsequent runs.\n",
|
||||||
|
" # Perform 20 independent runs.\n",
|
||||||
|
" for _ in range(20):\n",
|
||||||
|
" # Get a new seed for each run from the initial LCG generator.\n",
|
||||||
|
" seed = next(lcg_gen)\n",
|
||||||
|
" # Add the maximum subarray sum of the current run to the total sum.\n",
|
||||||
|
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
|
||||||
|
" return total_sum\n",
|
||||||
|
"\n",
|
||||||
|
"# Parameters for the simulation\n",
|
||||||
|
"n = 10000 # Number of random numbers to generate for each subarray\n",
|
||||||
|
"initial_seed = 42 # Initial seed for the LCG that generates seeds for runs\n",
|
||||||
|
"min_val = -10 # Minimum value for the random numbers\n",
|
||||||
|
"max_val = 10 # Maximum value for the random numbers\n",
|
||||||
|
"\n",
|
||||||
|
"# Import the time module to measure execution time.\n",
|
||||||
|
"import time\n",
|
||||||
|
"\n",
|
||||||
|
"# Record the start time before executing the main function.\n",
|
||||||
|
"start_time = time.time()\n",
|
||||||
|
"# Call the function to calculate the total maximum subarray sum over multiple runs.\n",
|
||||||
|
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
|
||||||
|
"# Record the end time after the function completes.\n",
|
||||||
|
"end_time = time.time()\n",
|
||||||
|
"\n",
|
||||||
|
"# Print the final aggregated result.\n",
|
||||||
|
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
|
||||||
|
"# Print the total execution time, formatted to 6 decimal places.\n",
|
||||||
|
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "ff02ce09-0544-49a5-944d-a57b25bf9b72",
|
"id": "ff02ce09-0544-49a5-944d-a57b25bf9b72",
|
||||||
@@ -578,6 +699,14 @@
|
|||||||
" return output.getvalue()"
|
" return output.getvalue()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "8391444b-b938-4f92-982f-91439b38d901",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Gradio App"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -624,7 +753,6 @@
|
|||||||
" code_input = gr.Code(\n",
|
" code_input = gr.Code(\n",
|
||||||
" language=\"python\", \n",
|
" language=\"python\", \n",
|
||||||
" value=python_hard,\n",
|
" value=python_hard,\n",
|
||||||
" lines=25,\n",
|
|
||||||
" elem_id=\"code_input\"\n",
|
" elem_id=\"code_input\"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" \n",
|
" \n",
|
||||||
@@ -632,8 +760,8 @@
|
|||||||
" gr.Markdown(\"### Annotated code:\")\n",
|
" gr.Markdown(\"### Annotated code:\")\n",
|
||||||
" annotated_output = gr.Code(\n",
|
" annotated_output = gr.Code(\n",
|
||||||
" language=\"python\",\n",
|
" language=\"python\",\n",
|
||||||
" lines=25,\n",
|
" elem_id=\"annotated_code\",\n",
|
||||||
" elem_id=\"annotated_code\"\n",
|
" interactive=False\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with gr.Row():\n",
|
" with gr.Row():\n",
|
||||||
@@ -661,7 +789,7 @@
|
|||||||
" inputs=[code_input, model_dropdown],\n",
|
" inputs=[code_input, model_dropdown],\n",
|
||||||
" outputs=[annotated_output]\n",
|
" outputs=[annotated_output]\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
" run_btn.click(execute_python, inputs=[code_input], outputs=[result_output])\n",
|
" run_btn.click(execute_python, inputs=[annotated_output], outputs=[result_output])\n",
|
||||||
"\n",
|
"\n",
|
||||||
" \n",
|
" \n",
|
||||||
"demo_v2.launch(inbrowser=True)\n"
|
"demo_v2.launch(inbrowser=True)\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user