-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (48 loc) · 1.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from dotenv import load_dotenv
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
"""Returns a simple greeting."""
return "hello world"
@app.route("/add/<num1>/<num2>")
def add(num1: int, num2: int) -> str:
"""Adds two numbers together.
Args:
num1: The first number.
num2: The second number.
Returns:
The sum of the two numbers as a string.
"""
return str(int(num1) + int(num2))
def fibonacci(n: int) -> int | str:
"""Calculates the nth Fibonacci number.
Args:
n: The index of the desired Fibonacci number.
Returns:
The nth Fibonacci number, or an error message if the input is invalid.
"""
if n <= 0:
return "Invalid input"
elif n == 1:
return 0
elif n == 2:
return 1
else:
a, b = 0, 1
for _ in range(3, n + 1):
a, b = b, a + b
return b
@app.route("/fibonacci/<int:n>")
def get_fibonacci(n: int) -> str:
"""Gets the nth Fibonacci number via the fibonacci function.
Args:
n: The index of the desired Fibonacci number.
Returns:
The nth Fibonacci number as a string.
"""
return str(fibonacci(n))
if __name__ == "__main__":
load_dotenv()
app.run(debug=os.getenv("ENV") == "dev")