From cb0b8a8d45686555b53a99f9b04ea23c11d81698 Mon Sep 17 00:00:00 2001 From: Edward Donner Date: Tue, 24 Dec 2024 17:59:48 +0000 Subject: [PATCH] Updated descriptions --- ...-webscraping-selenium-for-javascript.ipynb | 32 +++++----- week2/day4.ipynb | 8 --- week4/day3.ipynb | 8 ++- week4/day4.ipynb | 62 +++++++++++++------ week4/optimized.cpp | 41 +++++------- week4/simple.cpp | 7 +++ week8/day5.ipynb | 8 +++ 7 files changed, 96 insertions(+), 70 deletions(-) create mode 100644 week4/simple.cpp diff --git a/week1/community-contributions/day1-webscraping-selenium-for-javascript.ipynb b/week1/community-contributions/day1-webscraping-selenium-for-javascript.ipynb index 6b7a266..a9fc4cc 100644 --- a/week1/community-contributions/day1-webscraping-selenium-for-javascript.ipynb +++ b/week1/community-contributions/day1-webscraping-selenium-for-javascript.ipynb @@ -28,7 +28,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", "metadata": {}, "outputs": [], @@ -91,7 +91,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "019974d9-f3ad-4a8a-b5f9-0a3719aea2d3", "metadata": {}, "outputs": [], @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "id": "c5e793b2-6775-426a-a139-4848291d0463", "metadata": {}, "outputs": [], @@ -197,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "abdb8417-c5dc-44bc-9bee-2e059d162699", "metadata": {}, "outputs": [], @@ -211,7 +211,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "f0275b1b-7cfe-4f9d-abfa-7650d378da0c", "metadata": {}, "outputs": [], @@ -258,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "id": "f25dcd35-0cd0-4235-9f64-ac37ed9eaaa5", "metadata": {}, "outputs": [], @@ -292,7 +292,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "id": "0134dfa4-8299-48b5-b444-f2a8c3403c88", "metadata": {}, "outputs": [], @@ -328,7 +328,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "id": "905b9919-aba7-45b5-ae65-81b3d1d78e34", "metadata": {}, "outputs": [], @@ -356,7 +356,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "id": "3d926d59-450e-4609-92ba-2d6f244f1342", "metadata": {}, "outputs": [], @@ -459,7 +459,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "682eff74-55c4-4d4b-b267-703edbc293c7", "metadata": {}, "outputs": [], @@ -474,7 +474,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "90ca6dd0", "metadata": {}, "outputs": [], @@ -532,7 +532,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "947eac30", "metadata": {}, "outputs": [], @@ -543,7 +543,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "id": "2cba8c91", "metadata": {}, "outputs": [], @@ -572,7 +572,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": null, "id": "7880ce6a", "metadata": {}, "outputs": [], @@ -601,7 +601,7 @@ ], "metadata": { "kernelspec": { - "display_name": "llm_env", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -615,7 +615,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.9" + "version": "3.11.11" } }, "nbformat": 4, diff --git a/week2/day4.ipynb b/week2/day4.ipynb index 0151e7d..bc3f0df 100644 --- a/week2/day4.ipynb +++ b/week2/day4.ipynb @@ -228,14 +228,6 @@ "source": [ "gr.ChatInterface(fn=chat, type=\"messages\").launch()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "11c9da69-d0cf-4cf2-a49e-e5669deec47b", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/week4/day3.ipynb b/week4/day3.ipynb index 74cbc7c..bfa8765 100644 --- a/week4/day3.ipynb +++ b/week4/day3.ipynb @@ -275,9 +275,12 @@ "It compiles the file `optimized.cpp` into an executable called `optimized` \n", "Then it runs the program called `optimized`\n", "\n", - "You can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n", + "In the next lab (day4), a student has contributed a full solution that compiles to efficient code on Mac, PC and Linux!\n", + "\n", + "You can wait for this, or you can google (or ask ChatGPT!) for how to do this on your platform, then replace the lines below.\n", "If you're not comfortable with this step, you can skip it for sure - I'll show you exactly how it performs on my Mac.\n", "\n", + "\n", "OR alternatively: student Sandeep K.G. points out that you can run Python and C++ code online to test it out that way. Thank you Sandeep! \n", "> Not an exact comparison but you can still get the idea of performance difference.\n", "> For example here: https://www.programiz.com/cpp-programming/online-compiler/" @@ -326,7 +329,8 @@ "metadata": {}, "outputs": [], "source": [ - "python_hard = \"\"\"\n", + "python_hard = \"\"\"# Be careful to support large number sizes\n", + "\n", "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n", " value = seed\n", " while True:\n", diff --git a/week4/day4.ipynb b/week4/day4.ipynb index 97730f1..fe1e925 100644 --- a/week4/day4.ipynb +++ b/week4/day4.ipynb @@ -282,7 +282,8 @@ "metadata": {}, "outputs": [], "source": [ - "python_hard = \"\"\"\n", + "python_hard = \"\"\"# Be careful to support large number sizes\n", + "\n", "def lcg(seed, a=1664525, c=1013904223, m=2**32):\n", " value = seed\n", " while True:\n", @@ -461,31 +462,31 @@ "outputs": [], "source": [ "def execute_python(code):\n", - " try:\n", - " output = io.StringIO()\n", - " sys.stdout = output\n", - " exec(code)\n", - " finally:\n", - " sys.stdout = sys.__stdout__\n", - " return output.getvalue()" + " try:\n", + " output = io.StringIO()\n", + " sys.stdout = output\n", + " exec(code)\n", + " finally:\n", + " sys.stdout = sys.__stdout__\n", + " return output.getvalue()" ] }, { "cell_type": "code", - "execution_count": 107, + "execution_count": null, "id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6", "metadata": {}, "outputs": [], "source": [ "def execute_cpp(code):\n", - " write_output(code)\n", - " try:\n", - " compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n", - " run_cmd = [\"./optimized\"]\n", - " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n", - " return run_result.stdout\n", - " except subprocess.CalledProcessError as e:\n", - " return f\"An error occurred:\\n{e.stderr}\"" + " write_output(code)\n", + " try:\n", + " compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n", + " run_cmd = [\"./optimized\"]\n", + " run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n", + " return run_result.stdout\n", + " except subprocess.CalledProcessError as e:\n", + " return f\"An error occurred:\\n{e.stderr}\"" ] }, { @@ -640,6 +641,27 @@ " yield stream_so_far " ] }, + { + "cell_type": "markdown", + "id": "4b0a6a97-5b8a-4a9b-8ee0-7561e0ced673", + "metadata": {}, + "source": [ + "\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " \n", + " \n", + "

Thank you to @CloudLlama for an amazing contribution

\n", + " \n", + " A student has contributed a chunk of code to improve this, in the next 2 cells. You can now select which Python porgram to run,\n", + " and a compiler is automatically selected that will work on PC, Windows and Mac. Massive thank you @CloudLlama!\n", + " \n", + "
" + ] + }, { "cell_type": "code", "execution_count": null, @@ -658,7 +680,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": null, "id": "e42286bc-085c-45dc-b101-234308e58269", "metadata": {}, "outputs": [], @@ -795,7 +817,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f12bfe23-135b-45a7-8c6d-0c27d68b0a82", + "id": "9d0ad093-425b-488e-8c3f-67f729dd9c06", "metadata": {}, "outputs": [], "source": [] @@ -817,7 +839,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.10" + "version": "3.11.11" } }, "nbformat": 4, diff --git a/week4/optimized.cpp b/week4/optimized.cpp index 365cbee..b76e850 100644 --- a/week4/optimized.cpp +++ b/week4/optimized.cpp @@ -4,15 +4,12 @@ #include #include -using namespace std; -using namespace chrono; - class LCG { private: uint64_t value; - static const uint64_t a = 1664525; - static const uint64_t c = 1013904223; - static const uint64_t m = 1ULL << 32; + const uint64_t a = 1664525; + const uint64_t c = 1013904223; + const uint64_t m = 1ULL << 32; public: LCG(uint64_t seed) : value(seed) {} @@ -25,21 +22,17 @@ public: int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) { LCG lcg(seed); - vector random_numbers(n); + std::vector random_numbers(n); for (int i = 0; i < n; ++i) { - random_numbers[i] = lcg.next() % (max_val - min_val + 1) + min_val; + random_numbers[i] = static_cast(lcg.next() % (max_val - min_val + 1) + min_val); } - int64_t max_sum = numeric_limits::min(); + int64_t max_sum = std::numeric_limits::min(); int64_t current_sum = 0; - int64_t min_sum = 0; - for (int i = 0; i < n; ++i) { - current_sum += random_numbers[i]; - max_sum = max(max_sum, current_sum - min_sum); - min_sum = min(min_sum, current_sum); + current_sum = std::max(static_cast(random_numbers[i]), current_sum + random_numbers[i]); + max_sum = std::max(max_sum, current_sum); } - return max_sum; } @@ -54,19 +47,19 @@ int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int ma } int main() { - const int n = 10000; - const uint64_t initial_seed = 42; - const int min_val = -10; - const int max_val = 10; + int n = 10000; + uint64_t initial_seed = 42; + int min_val = -10; + int max_val = 10; - auto start_time = high_resolution_clock::now(); + auto start_time = std::chrono::high_resolution_clock::now(); int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val); - auto end_time = high_resolution_clock::now(); + auto end_time = std::chrono::high_resolution_clock::now(); - auto duration = duration_cast(end_time - start_time); + auto duration = std::chrono::duration_cast(end_time - start_time); - cout << "Total Maximum Subarray Sum (20 runs): " << result << endl; - cout << "Execution Time: " << fixed << setprecision(6) << duration.count() / 1e6 << " seconds" << endl; + std::cout << "Total Maximum Subarray Sum (20 runs): " << result << std::endl; + std::cout << "Execution Time: " << std::fixed << std::setprecision(6) << duration.count() / 1e6 << " seconds" << std::endl; return 0; } \ No newline at end of file diff --git a/week4/simple.cpp b/week4/simple.cpp new file mode 100644 index 0000000..a580b59 --- /dev/null +++ b/week4/simple.cpp @@ -0,0 +1,7 @@ + +#include + +int main() { + std::cout << "Hello"; + return 0; +} diff --git a/week8/day5.ipynb b/week8/day5.ipynb index fb1a1db..625232e 100644 --- a/week8/day5.ipynb +++ b/week8/day5.ipynb @@ -172,6 +172,14 @@ " \n", "" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "096397f9-1215-4814-ab4b-e32002ff4ceb", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {