Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nr. 1 finished #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions .ipynb_checkpoints/PS_2-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "2f7f52aa-92b7-4fd5-8fe9-06595d36f633",
"metadata": {},
"outputs": [
{
"ename": "IndentationError",
"evalue": "expected an indented block after 'for' statement on line 2 (4110982641.py, line 3)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[2], line 3\u001b[0;36m\u001b[0m\n\u001b[0;31m if input [idx ][0] == \"1\":\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block after 'for' statement on line 2\n"
]
}
],
"source": [
"def check_array ( input ):\n",
" for idx in range (len( input )):\n",
" if input [idx ][0] == \"1\":\n",
" input [idx] = None\n",
" return input\n",
"\n",
"original_array = [\"1_3\", \"5_2\"]\n",
"\n",
" new_array = check_array ( original_array )\n",
"\n",
" print ( original_array )\n",
" print ( new_array )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e146596b-d2eb-4979-97db-ce5cbb8e4353",
"metadata": {},
"outputs": [],
"source": [
"max_sum = None\n",
" for i in range (len ( array )):\n",
" sum_subarray = 0\n",
" for j in range (i, len( array )):\n",
" sum_subarray += array [j]\n",
" if max_sum is None or max_sum < sum_subarray :\n",
" max_sum = sum_subarray\n",
" print ( max_sum )"
]
}
],
"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
}
9 changes: 9 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Calculation for area and permiter
import math
class Circle:
def __init__(self, radius: float):
self.radius = radius
def perimeter_calculation(self) -> float: ## Perimeter Method
return 2 * math.pi * self.radius
def area_calculation(self) -> float: ## Area Method
return math.pi * self.radius ** 2
27 changes: 26 additions & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask, render_template, request

from helper import perform_calculation, convert_to_float
## We need this package in order to perform the circle calculations
from circle import Circle

app = Flask(__name__) # create the instance of the flask class

Expand Down Expand Up @@ -38,3 +39,27 @@ def calculate():
return render_template('calculator.html', printed_result="You cannot divide by zero")

return render_template('calculator.html')


### Aranxa Márquez: PS_2

@app.route('/circle', methods=['GET', 'POST'])
def circle():
printed_result = None # Initialize printed_result variable

if request.method == 'POST':
radius = float(request.form['radius']) # Correctly extract radius from form data
operation = request.form['operation']

circle = Circle(radius) # Create a Circle instance with the given radius

if operation == 'perimeter':
result = circle.perimeter_calculation()
printed_result = f"The perimeter of the circle is: {result}"
elif operation == 'area':
result = circle.area_calculation()
printed_result = f"The area of the circle is: {result}"
else:
printed_result = 'Operation must be one of "area", "perimeter"'

return render_template('circle.html', printed_result=printed_result)
19 changes: 19 additions & 0 deletions templates/circle.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle</h1>
<form method="post" action="/circle">
<input type="text" name="radius" placeholder="Enter the radius" required="required" />

<label for="operation">Operation</label>
<select id="operation" name="operation">
<option value="area">Area</option>
<option value="perimeter">Perimeter</option>
</select>

<button type="submit">Calculate</button>
</form>

{% if printed_result %}
<p>{{ printed_result }}</p>
{% endif %}
{% endblock %}
29 changes: 16 additions & 13 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">
</head>
<body>
<body>
<header>
<div id="navbar">
<h1 class="logo"><a href="{{ url_for('home') }}">Home</a></h1>
<strong><nav>
<ul class="menu">
<li><a href="{{ url_for('calculate') }}">General Calculator</a></li>
</ul>
</nav></strong>
</div>
<div id="navbar">
<h1 class="logo"><a href="{{ url_for('home') }}">Home</a></h1>
<strong>
<nav>
<ul class="menu">
<li><a href="{{ url_for('calculate') }}">General Calculator</a></li>
<li><a href="{{ url_for('circle') }}">Circle Calculator</a></li> <!-- Add this line -->
</ul>
</nav>
</strong>
</div>
</header>
<div class="container">
{% block content %}
{% endblock %}
<div class="container">
{% block content %}
{% endblock %}
</div>
</body>
</body>
</html>
20 changes: 20 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from circle import Circle
import math
def test_perimeter_calculation():
# Create an instance of the Circle class with radius 1
circle = Circle(1)

# Call the perimeter_calculation method
perimeter = circle.perimeter_calculation()

# Assert the result rounded to 8 decimal places
assert round(perimeter, 8) == round(2 * math.pi, 8)
def test_area_calculation():
# Create an instance of the Circle class with radius 1
circle = Circle(1)

# Call the area_calculation method
area = circle.area_calculation()

# Assert the result rounded to 8 decimal places
assert round(area, 8) == round(math.pi, 8)