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 +} diff --git a/circle.py b/circle.py new file mode 100644 index 0000000..a417d95 --- /dev/null +++ b/circle.py @@ -0,0 +1,10 @@ +class Circle: + def __init__(self, radius): + self.radius = radius + def calc_perimeter(self): + return 2*3.1416*self.radius + def calc_area(self): + return 3.1416*self.radius ** 2 + + + diff --git a/flask_app.py b/flask_app.py index 8897fa2..13b285e 100644 --- a/flask_app.py +++ b/flask_app.py @@ -1,6 +1,6 @@ from flask import Flask, render_template, request - from helper import perform_calculation, convert_to_float +from circle import Circle app = Flask(__name__) # create the instance of the flask class @@ -38,3 +38,21 @@ def calculate(): return render_template('calculator.html', printed_result="You cannot divide by zero") return render_template('calculator.html') + +@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) + + 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/helper.py b/helper.py index 31e82d6..3bfbc60 100644 --- a/helper.py +++ b/helper.py @@ -44,4 +44,4 @@ def convert_to_float(value: str) -> float: float_value = float(value) - return float_value + return float_value \ No newline at end of file diff --git a/templates/circle_calculator.html b/templates/circle_calculator.html new file mode 100644 index 0000000..05217b5 --- /dev/null +++ b/templates/circle_calculator.html @@ -0,0 +1,25 @@ + +{% extends 'layout.html' %} +{% block content %} +

Circle Calculator

+
+ + + +
+ +
+{% if perimeter %} +

Perimeter: {{ perimeter }}

+ {% endif %} + {% if area %} +

Area: {{ area }}

+ {% endif %} + {% if error %} +

{{ error }}

+ {% endif %} + +{% endblock %} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index 8de2e62..914127e 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -12,6 +12,7 @@

Home

diff --git a/test_circle.py b/test_circle.py new file mode 100644 index 0000000..87e559e --- /dev/null +++ b/test_circle.py @@ -0,0 +1,14 @@ +from circle import Circle + +def test_calc_perimeter(): + circle = Circle(5) + assert circle.calc_perimeter() == 2 * 3.1416 * 5 + + circle = Circle(2) + assert circle.calc_perimeter() == 2 * 3.1416 * 2 +def test_calc_area(): + circle = Circle(2) + assert circle.calc_area() == 3.1416* 2 ** 2 + + circle = Circle(4) + assert circle.calc_area() == 3.1416* 4 ** 2 \ No newline at end of file