Updated descriptions
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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": [
|
||||
"<table style=\"margin: 0; text-align: left;\">\n",
|
||||
" <tr>\n",
|
||||
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
|
||||
" <img src=\"../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <h2 style=\"color:#090;\">Thank you to @CloudLlama for an amazing contribution</h2>\n",
|
||||
" <span style=\"color:#090;\">\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",
|
||||
" </span>\n",
|
||||
" </td>\n",
|
||||
" </tr>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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,
|
||||
|
||||
@@ -4,15 +4,12 @@
|
||||
#include <limits>
|
||||
#include <iomanip>
|
||||
|
||||
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<int64_t> random_numbers(n);
|
||||
std::vector<int> 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<int>(lcg.next() % (max_val - min_val + 1) + min_val);
|
||||
}
|
||||
|
||||
int64_t max_sum = numeric_limits<int64_t>::min();
|
||||
int64_t max_sum = std::numeric_limits<int64_t>::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<int64_t>(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<microseconds>(end_time - start_time);
|
||||
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(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;
|
||||
}
|
||||
7
week4/simple.cpp
Normal file
7
week4/simple.cpp
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::cout << "Hello";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user