Skip to content
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
Binary file added .DS_Store
Binary file not shown.
Binary file added DSA_homework2.pdf
Binary file not shown.
Binary file added DS_A_Assignment_2.pdf
Binary file not shown.
34 changes: 34 additions & 0 deletions circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# create helper functions for simple circular geometry calculations

from math import pi

class Circle:
def __init__ (self, radius):
self.radius = radius

def circle_calculation(self, operation:str) -> float:
"""""
Calculates perimeter/area of circle with given radius.

Parameters:
radius (float): radius of given circle

Returns:
float: The perimeter/area of the circle.
"""""
if not isinstance(self.radius, (int, float)):
return "Cannot perform operation with this input"

if self.radius < 0:
return "Invalid input for radius"

if operation == 'perimeter':
result = float(2 * pi * self.radius)

elif operation == 'area':
result = float(pi * self.radius ** 2)

else:
raise ValueError("Invalid operation. Please specify 'perimeter' or 'area'.")

return result
25 changes: 24 additions & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
@@ -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

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

return render_template('calculator.html')


@app.route('/calculate_circle', methods=['GET', 'POST'])
def calculate_circle():
if request.method == 'POST':
radius = request.form['radius']
operation = str(request.form['operation'])

try:
radius = convert_to_float(value=radius)
except ValueError:
return render_template('circle_calculator.html', printed_result="Cannot perform operation with this input")

circle = Circle(radius)
result = circle.circle_calculation(operation=operation)
return render_template('circle_calculator.html', printed_result=str(result))

return render_template('circle_calculator.html')



if __name__ == "__main__":
app.run(debug=True)
Binary file added questions 2_3.docx
Binary file not shown.
Binary file added static/.DS_Store
Binary file not shown.
41 changes: 32 additions & 9 deletions static/main.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
@import url('https://fonts.googleapis.com/css2?family=Comic+Neue&family=Papyrus&display=swap');


/* Global Styles */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f8f9fa;
color: #333;
font-family: 'Comic Neue', cursive, Tahoma, Geneva, Verdana, sans-serif;
font-size: 1em;
background-color: #2d7ccb;
color: #ffffff;
margin: 0;
padding: 0;
background-image: url('tie_dye.jpg');
background-size: cover
}

.container {
Expand All @@ -13,6 +19,14 @@ body {
padding: 20px;
}

h1 {
font-family: 'Comic Sans MS', 'Comic Sans', cursive;
font-size: 4em;
color: #FFFFFF;
font-weight: bold;
padding: 20px;
}

/* Header Styles */
header {
background-color: #343a40; /* Dark neutral color for header */
Expand Down Expand Up @@ -56,8 +70,12 @@ h1 a {

h1 a:hover {
color: #ffafcc;
text-decoration: underline; /* re-adds underline on hover */
}




.menu li a:hover {
color: #bde0fe; /* Light pastel color on hover */
}
Expand All @@ -67,7 +85,7 @@ h1 {
text-align: center;
margin-top: 0;
font-size: 36px;
color: #343a40; /* Dark neutral color for heading */
color: #ffffff; /* Dark neutral color for heading */
}

form {
Expand All @@ -86,14 +104,19 @@ button {
}

button {
background-color: #343a40; /* Dark neutral color for button */
color: #fff;
background-color: #FF69B4;
color: #FFFFFF;
font-family: 'Comic Neue', cursive;
border: none;
padding: 10px 20px;
text-align: center;
display: inline-block;
margin: 4px 2px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease; /* Smooth transition for background color change */
border-radius: 12px;
transition: background-color 0.3s ease;
}

button:hover {
background-color: #23272b; /* Slightly darker color on hover */
background-color: #FFB6C1;
}
Binary file added static/tie_dye.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion templates/calculator.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Calculator</h1>
<h1>General Calculator</h1>
<form method="post">
<input type="text" name="value1" placeholder="Enter the first number" required="required" />
<input type="text" name="value2" placeholder="Enter the second number" required="required" />
Expand Down
20 changes: 20 additions & 0 deletions templates/circle_calculator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% extends 'layout.html' %}
{% block content %}
<h1>Circle Calculator</h1>
<form method="post">
<input type="text" name="radius" placeholder="Input radius" required="required" />

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

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

<br>

{{ printed_result }}

{% endblock %}
1 change: 1 addition & 0 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <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('calculate_circle') }}">Circle Calculator</a></li>
</ul>
</nav></strong>
</div>
Expand Down
84 changes: 84 additions & 0 deletions test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

from circle import Circle
from math import isclose

# area tests
def test_circle_calculation_area_int():
circle = Circle(5)
actual_area_5 = circle.circle_calculation('area')
expected_area_5 = 78.53981633974483
assert isclose(actual_area_5, expected_area_5, rel_tol=1e-9), f"Expected {expected_area_5}, but got {actual_area_5}"

def test_circle_calculation_area_float():
circle = Circle(4.9)
actual_area_4_9 = circle.circle_calculation('area')
expected_area_4_9 = 75.429639612691
assert isclose(actual_area_4_9, expected_area_4_9, rel_tol=1e-9), f"Expected {expected_area_4_9}, but got {actual_area_4_9}"

def test_circle_calculation_area_0():
circle = Circle(0)
assert circle.circle_calculation("area") == 0

def test_circle_calculation_area_neg():
circle = Circle(-1)
assert circle.circle_calculation("area") == "Invalid input for radius"

def test_circle_calculation_area_tiny():
circle = Circle(0.0001)
actual_area_tiny = circle.circle_calculation('area')
expected_area_tiny = 3.141592653589793e-08
assert isclose(actual_area_tiny, expected_area_tiny, rel_tol=1e-9), f"Expected {expected_area_tiny}, but got {actual_area_tiny}"

def test_circle_calculation_area_string():
circle = Circle("HelloWorld")
assert circle.circle_calculation("area") == "Cannot perform operation with this input"

# perimeter tests

def test_circle_calculation_perimeter_int():
circle = Circle(5)
actual_perimeter_5 = circle.circle_calculation('perimeter')
expected_perimeter_5 = 31.41592653589793
assert isclose(actual_perimeter_5, expected_perimeter_5, rel_tol=1e-9), f"Expected {expected_area_5}, but got {actual_perimeter_5}"

def test_circle_calculation_perimeter_float():
circle = Circle(4.9)
actual_perimeter_4_9 = circle.circle_calculation('perimeter')
expected_perimeter_4_9 = 30.78760800518
assert isclose(actual_perimeter_4_9, expected_perimeter_4_9, rel_tol=1e-9), f"Expected {expected_perimeter_4_9}, but got {actual_perimeter_4_9}"

def test_circle_calculation_perimeter_0():
circle = Circle(0)
assert circle.circle_calculation("perimeter") == 0

def test_circle_calculation_perimeter_neg():
circle = Circle(-1)
assert circle.circle_calculation("perimeter") == "Invalid input for radius"

def test_circle_calculation_perimeter_tiny():
circle = Circle(0.0001)
actual_perimeter_tiny = circle.circle_calculation("perimeter")
expected_perimeter_tiny = 0.00062831853071796
assert isclose(actual_perimeter_tiny, expected_perimeter_tiny, rel_tol=1e-9), f"Expected {expected_perimeter_tiny}, but got {actual_perimeter_tiny}"

def test_circle_calculation_perimeter_string():
circle = Circle("HelloWorld")
assert circle.circle_calculation("area") == "Cannot perform operation with this input"

def run_tests():
test_circle_calculation_area_int()
test_circle_calculation_area_float()
test_circle_calculation_area_0()
test_circle_calculation_area_neg()
test_circle_calculation_area_tiny()
test_circle_calculation_area_string()
test_circle_calculation_perimeter_int()
test_circle_calculation_perimeter_float()
test_circle_calculation_perimeter_0()
test_circle_calculation_perimeter_neg()
test_circle_calculation_perimeter_tiny()
test_circle_calculation_perimeter_string()
print("All tests passed!") # if any assertions fail, AssertionError will be raised

if __name__ == "__main__":
run_tests()