Last active
May 1, 2019 17:59
-
-
Save liketaurus/e0f548e40b3a88507e991d1900cfe797 to your computer and use it in GitHub Desktop.
Azure Function to get a number from a Fibonacci sequence. Sample usage: https://functionName.azurewebsites.net/fibonacci?index=14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"bindings": [ | |
{ | |
"authLevel": "anonymous", | |
"name": "req", | |
"type": "httpTrigger", | |
"direction": "in", | |
"methods": [ | |
"get", | |
"post" | |
] | |
}, | |
{ | |
"name": "$return", | |
"type": "http", | |
"direction": "out" | |
} | |
], | |
"disabled": false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "2.0", | |
"extensions": { | |
"http": { | |
"routePrefix": "" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "Newtonsoft.Json" | |
using System.Net; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Primitives; | |
using Newtonsoft.Json; | |
public static async Task<IActionResult> Run(HttpRequest req, ILogger log) | |
{ | |
string index = req.Query["index"]; | |
int n; | |
bool res = int.TryParse(index, out n); | |
return res == true && n>=0 && n<=40 | |
? (ActionResult)new OkObjectResult($"{Fibonacci(n)}") | |
: new BadRequestObjectResult("Please provide a number between 0 and 40"); | |
} | |
public static int Fibonacci(int n) | |
{ | |
if (n == 0) return 0; | |
else if (n == 1) return 1; | |
else | |
{ | |
return Fibonacci(n - 1) + Fibonacci(n - 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment