From c715f83c1cd1b513f7dcd145c85f57c6a8743f0c Mon Sep 17 00:00:00 2001
From: lramir14 <73917621+lramir14@users.noreply.github.com>
Date: Tue, 12 Mar 2024 22:41:20 +0100
Subject: [PATCH 3/4] Finished question 1 with final review. Awaiting for last
review before submission
---
flask_app.py | 14 +++++++++++---
templates/circle_calculator.html | 13 +++++--------
2 files changed, 16 insertions(+), 11 deletions(-)
diff --git a/flask_app.py b/flask_app.py
index e907db2..13b285e 100644
--- a/flask_app.py
+++ b/flask_app.py
@@ -41,10 +41,18 @@ def calculate():
@app.route('/circle', methods=['GET', 'POST'])
def circle_calculator():
+ perimeter = None
+ area = None
+
if request.method == 'POST':
radius = request.form.get('radius', type=float)
+ calculation_type = request.form.get('calculation')
circle = Circle(radius)
- perimeter = circle.calc_perimeter()
- area = circle.calc_area()
- return render_template('circle_calculator.html', perimeter=perimeter, area=area)
+
+ if calculation_type=='perimeter':
+ perimeter = circle.calc_perimeter()
+ elif calculation_type=='area':
+ area = circle.calc_area()
+
+ return render_template('circle_calculator.html', perimeter= perimeter, area=area)
return render_template('circle_calculator.html')
diff --git a/templates/circle_calculator.html b/templates/circle_calculator.html
index be5eb48..05217b5 100644
--- a/templates/circle_calculator.html
+++ b/templates/circle_calculator.html
@@ -3,15 +3,12 @@
{% block content %}
Circle Calculator
From c93b540c4b01a1ba62cc5b483ceecf7f34d32174 Mon Sep 17 00:00:00 2001
From: lramir14 <73917621+lramir14@users.noreply.github.com>
Date: Sat, 20 Apr 2024 21:24:22 +0200
Subject: [PATCH 4/4] assignment two already sent and delivered
---
.../assignment2_luis_ramirez-checkpoint.ipynb | 120 ++++++++++++++++++
assignment2_luis_ramirez.ipynb | 120 ++++++++++++++++++
2 files changed, 240 insertions(+)
create mode 100644 .ipynb_checkpoints/assignment2_luis_ramirez-checkpoint.ipynb
create mode 100644 assignment2_luis_ramirez.ipynb
diff --git a/.ipynb_checkpoints/assignment2_luis_ramirez-checkpoint.ipynb b/.ipynb_checkpoints/assignment2_luis_ramirez-checkpoint.ipynb
new file mode 100644
index 0000000..20b77f8
--- /dev/null
+++ b/.ipynb_checkpoints/assignment2_luis_ramirez-checkpoint.ipynb
@@ -0,0 +1,120 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "6e910236-7b26-437b-b443-451113b2b1f4",
+ "metadata": {},
+ "source": [
+ "## Question 1 \n",
+ "URL for pull request: \n",
+ "\n",
+ "https://github.com/lenafm/calculator_app/pull/32"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1902777b-52e2-4147-b7a2-77a8a9887a8c",
+ "metadata": {},
+ "source": [
+ "## Question 2\n",
+ "Examine the following code:\r\n",
+ "1 def check_array ( input ) :\r\n",
+ "2 for idx in range (len( input ) ) :\r\n",
+ "3 if input [ idx ][0] == \"1\":\r\n",
+ "4 input [ idx ] = None\r\n",
+ "5 return input\r\n",
+ "6\r\n",
+ "7 original_array = [\"1_3\", \"5_2\"]\r\n",
+ "8\r\n",
+ "9 new_array = check_array ( original_array )\r\n",
+ "10\r\n",
+ "11 print ( original_array )\r\n",
+ "12 print (\n",
+ " new_array )\r\n",
+ "What does it output? Why? Use the language we developed in lecture 3, page 30-32 (about\r\n",
+ "pointers)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bde00535-b314-4b83-807c-c3f106a1419a",
+ "metadata": {},
+ "source": [
+ "### Answer: \n",
+ "The first line defines a function \"check_array\" for the parameter input. Then the loop starts by iterating over a sequence of numbers from 0 to the length of the object \"input\" that serves as a placeholder for the data. Then the condition is to check if the first element of the list (in this case input), which in Python is (0), contains the character \"1\", then if this is true the input is NONE. Then it returns the value replaced from the string that contained character \"1\" to \"None\". \n",
+ "\n",
+ "Line 7 defines a list named \"original array\" with two elements that are strings: \"1_3\" and \"5_2\".\n",
+ "Lastly, the \"new array\" object calls the previously defined function \"check array\" with \"original array\" as the argument. \n",
+ "\n",
+ "The \"print\" function simply prints the content of the original array and the new array.\n",
+ "\n",
+ "To understand the output of this function we have to use the pointers concept in data structures. \n",
+ "\n",
+ "When we define a list it is important to remember that are mutable in Python, so when they are called in a function they are passed by referencing their address where the content of this particular list is stored. So any changes or modifications to the \"input\" argument in the function will directly change the \"original array\" object since they are both using the same memory addresses for each element they contain. \n",
+ "\n",
+ "Now, when we create the \"original_array\" what is happening is the creation and storage of this list content in specific memory slots. \n",
+ "\n",
+ "So, when the \"new_array\" object is created then it tells Python to point to the memory addresses in which the original array is stored. Then since the pointers only reference the memory addresses, not their content, when the function is applied it changes the content in the memory slots but not their addresses. So, in the end, both objects are changed and the output for both will be [\"None\", \"5,2\"]. \n",
+ "[\"None\", \"5,2\"]. \n",
+ "[\"None\", \"5,2\"]. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9aa34f2b-9078-4a81-a4e4-fc59b8a06601",
+ "metadata": {},
+ "source": [
+ "## Question 3\n",
+ "(2 points) Consider the following algorithm:\r\n",
+ "1 max_sum = None\r\n",
+ "2 for i in range (len ( array ) ) :\r\n",
+ "3 sum_subarray = 0\r\n",
+ "4 for j in range (i , len( array ) ) :\r\n",
+ "5 sum_subarray += array [ j ]\r\n",
+ "6 if max_sum is None or max_sum < sum_subarray :\r\n",
+ "7 max_sum = sum_subarray\r\n",
+ "8 print ( m\n",
+ "\n",
+ "This algorithm takes an array (assumed to be non-empty) named array and finds the subarray\r\n",
+ "(contiguous elements of that array of any size) with the largest sum and outputs that sum.\r\n",
+ "Is the algorithm defined above ”efficient” in the sense we defined in lecture 5 (slide 30)? That\r\n",
+ "is, is its runtime polynomial in the size of the array? If not, explain why not. Ivet is, githe\r\n",
+ "its runtime in Big-O notation with the smallest d( ^uh th ^One. true.ax_sum )"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3c027dc3-52c6-4248-8708-b1e2968459a7",
+ "metadata": {},
+ "source": [
+ "### Answer: \n",
+ "The described algorithm needs to find all possible subarrays within an array. The formula first has to find all possible subarrays within an array n*(n+1)/2 and then add their elements to find the one subarray/s with the largest sum.\n",
+ "The Big O notation for the first operation is O(n^2). Since the algorithms' runtime complexity does not depend on the size of n, although the absolute running time will still depend on the size of n, then n^2 still represents the runtime since it is the same from its size (it is constant and does change whether n is small or big). \n",
+ "The second operation for the subarray entails a sum of the inner elements from this subarray, thus this means that the runtime is restricted by the size of this subarray relative to the array, so the length is n/2. Since the upper bound is still dominated by n^2m the Big O notation remains O(n^2). \n",
+ "\n",
+ "So, under this analysis, the algorithm is efficient since n^2 is polynomial becasue all these operations occur at a constant time regardless of the size of n, . \n"
+ ]
+ }
+ ],
+ "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.11.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/assignment2_luis_ramirez.ipynb b/assignment2_luis_ramirez.ipynb
new file mode 100644
index 0000000..93a907e
--- /dev/null
+++ b/assignment2_luis_ramirez.ipynb
@@ -0,0 +1,120 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "6e910236-7b26-437b-b443-451113b2b1f4",
+ "metadata": {},
+ "source": [
+ "## Question 1 \n",
+ "URL for pull request: \n",
+ "\n",
+ "https://github.com/lenafm/calculator_app/pull/32"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1902777b-52e2-4147-b7a2-77a8a9887a8c",
+ "metadata": {},
+ "source": [
+ "## Question 2\n",
+ "Examine the following code:\r\n",
+ "1 def check_array ( input ) :\r\n",
+ "2 for idx in range (len( input ) ) :\r\n",
+ "3 if input [ idx ][0] == \"1\":\r\n",
+ "4 input [ idx ] = None\r\n",
+ "5 return input\r\n",
+ "6\r\n",
+ "7 original_array = [\"1_3\", \"5_2\"]\r\n",
+ "8\r\n",
+ "9 new_array = check_array ( original_array )\r\n",
+ "10\r\n",
+ "11 print ( original_array )\r\n",
+ "12 print (\n",
+ " new_array )\r\n",
+ "What does it output? Why? Use the language we developed in lecture 3, page 30-32 (about\r\n",
+ "pointers)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "bde00535-b314-4b83-807c-c3f106a1419a",
+ "metadata": {},
+ "source": [
+ "### Answer: \n",
+ "The first line defines a function \"check_array\" for the parameter input. Then the loop starts by iterating over a sequence of numbers from 0 to the length of the object \"input\" that serves as a placeholder for the data. Then the condition is to check if the first element of the list (in this case input), which in Python is (0), contains the character \"1\", then if this is true the input is NONE. Then it returns the value replaced from the string that contained character \"1\" to \"None\". \n",
+ "\n",
+ "Line 7 defines a list named \"original array\" with two elements that are strings: \"1_3\" and \"5_2\".\n",
+ "Lastly, the \"new array\" object calls the previously defined function \"check array\" with \"original array\" as the argument. \n",
+ "\n",
+ "The \"print\" function simply prints the content of the original array and the new array.\n",
+ "\n",
+ "To understand the output of this function we have to use the pointers concept in data structures. \n",
+ "\n",
+ "When we define a list it is important to remember that are mutable in Python, so when they are called in a function they are passed by referencing their address where the content of this particular list is stored. So any changes or modifications to the \"input\" argument in the function will directly change the \"original array\" object since they are both using the same memory addresses for each element they contain. \n",
+ "\n",
+ "Now, when we create the \"original_array\" what is happening is the creation and storage of this list content in specific memory slots. \n",
+ "\n",
+ "So, when the \"new_array\" object is created then it tells Python to point to the memory addresses in which the original array is stored. Then since the pointers only reference the memory addresses, not their content, when the function is applied it changes the content in the memory slots but not their addresses. So, in the end, both objects are changed and the output for both will be. \n",
+ " print ( original_array )[\"None\", \"5,2\"]. \n",
+ " print ( new_array )[\"None\", \"5,2\"]. "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "9aa34f2b-9078-4a81-a4e4-fc59b8a06601",
+ "metadata": {},
+ "source": [
+ "## Question 3\n",
+ "(2 points) Consider the following algorithm:\r\n",
+ "1 max_sum = None\r\n",
+ "2 for i in range (len ( array ) ) :\r\n",
+ "3 sum_subarray = 0\r\n",
+ "4 for j in range (i , len( array ) ) :\r\n",
+ "5 sum_subarray += array [ j ]\r\n",
+ "6 if max_sum is None or max_sum < sum_subarray :\r\n",
+ "7 max_sum = sum_subarray\r\n",
+ "8 print ( m\n",
+ "\n",
+ "This algorithm takes an array (assumed to be non-empty) named array and finds the subarray\r\n",
+ "(contiguous elements of that array of any size) with the largest sum and outputs that sum.\r\n",
+ "Is the algorithm defined above ”efficient” in the sense we defined in lecture 5 (slide 30)? That\r\n",
+ "is, is its runtime polynomial in the size of the array? If not, explain why not. Ivet is, githe\r\n",
+ "its runtime in Big-O notation with the smallest d( ^uh th ^One. true.ax_sum )"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3c027dc3-52c6-4248-8708-b1e2968459a7",
+ "metadata": {},
+ "source": [
+ "### Answer: \n",
+ "The described algorithm needs to find all possible subarrays within an array. The formula first has to find all possible subarrays within an array n*(n+1)/2 and then add their elements to find the one subarray/s with the largest sum.\n",
+ "The Big O notation for the first operation is O(n^2). Since the algorithms' runtime complexity does not depend on the size of n, although the absolute running time will still depend on the size of n, then n^2 still represents the runtime since it is the same from its size (it is constant and does change whether n is small or big). \n",
+ "The second operation for the subarray entails a sum of the inner elements from this subarray, thus this means that the runtime is restricted by the size of this subarray relative to the array, so the length is n/2. Since the upper bound is still dominated by n^2m the Big O notation remains O(n^2). \n",
+ "\n",
+ "So, under this analysis, the algorithm is efficient since n^2 is polynomial becasue all these operations occur at a constant time regardless of the size of n, . \n"
+ ]
+ }
+ ],
+ "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.11.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}