using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapPost("/FormatNumberRange", (int[] numbers) =>
return Results.BadRequest();
var tracker = new FormatNumberRangeModel();
tracker.Low = numbers[0];
tracker.High = tracker.Low;
List<string> elements = new List<string>();
for (int i = 1; i < numbers.Length; i++)
if (numbers[i] == tracker.High + 1)
tracker.High = numbers[i];
if (tracker.Low == tracker.High)
elements.Add($"{tracker.Low}");
elements.Add($"{tracker.Low}-{tracker.High}");
tracker.Low = numbers[i];
tracker.High = tracker.Low;
if (tracker.Low == tracker.High)
elements.Add($"{tracker.Low}");
elements.Add($"{tracker.Low}-{tracker.High}");
string result = string.Join(",", elements);
return Results.Json(result);
char[] alphabet = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', };
Dictionary<char, int> alphabetIndex = new Dictionary<char, int>();
for (int i = 0; i < alphabet.Length; i++)
alphabetIndex.Add(alphabet[i], i);
app.MapGet("/Excel", (string column) =>
if (column == null || column == "")
char rightMostChar = column[column.Length - 1];
result += alphabetIndex[rightMostChar] + 1;
for (int i = column.Length - 2; i >= 0; i--)
result += (int)Math.Pow(alphabet.Length, position) * (alphabetIndex[c] + 1);
return Results.Json(result);
app.MapPost("/Pagination", (PaginationRequest request) =>
List<int> slice = new List<int>();
int maxPossiblePageIndex = request.PageSize > 0 ? request.AllRows.Length / request.PageSize : 0;
int pageIndex = Math.Min(request.PageIndex, maxPossiblePageIndex);
int index = request.PageSize * pageIndex;
int upperBound = Math.Min(index + request.PageSize, request.AllRows.Length);
while (index < upperBound)
slice.Add(request.AllRows[index]);
PaginationResponse result = new PaginationResponse();
result.Total = request.AllRows.Length;
result.CurrentPageRows = new int[slice.Count];
for (int i = 0; i < slice.Count; i++)
result.CurrentPageRows[i] = slice[i];
return Results.Json(result);
app.MapPost("/MatchingBraces", ([FromBody] string text) =>
return Results.Json(result);
Stack<char> stack = new Stack<char>();
HashSet<char> bracketOpener = new HashSet<char>
HashSet<char> bracketCloser = new HashSet<char>
Dictionary<char, char> bracketMatchingPair = new Dictionary<char, char>
if (bracketOpener.Contains(c))
else if (bracketCloser.Contains(c))
if (!stack.TryPeek(out var opener))
if (bracketMatchingPair[c] != opener)
app.MapGet("/URI", (string uri) =>
URIResponse result = ParseURI();
return Results.Json(result);
URIResponse result = new URIResponse();
int schemeTokenPosition = FindSectionPosition(str, new string[] { "://" });
if (schemeTokenPosition > 0)
result.Scheme = str.Substring(0, schemeTokenPosition);
str = str.Substring(schemeTokenPosition + 3);
int userInfoTokenPosition = FindSectionPosition(str, new string[] { "@" });
if (userInfoTokenPosition > 0)
result.UserInfo = str.Substring(0, userInfoTokenPosition);
str = str.Substring(userInfoTokenPosition + 1);
int portOrPathSeparatorPosition = FindSectionPosition(str, new string[] { ":", "/" });
if (portOrPathSeparatorPosition > 0)
result.Host = str.Substring(0, portOrPathSeparatorPosition);
str = str.Substring(portOrPathSeparatorPosition);
int pathSeparatorPosition = FindSectionPosition(str, new string[] { "/" });
if (pathSeparatorPosition > 0)
result.Port = str.Substring(1, pathSeparatorPosition - 1);
str = str.Substring(pathSeparatorPosition);
int queryOrFragmentTokenPosition = FindSectionPosition(str, new string[] { "?", "#" });
if (queryOrFragmentTokenPosition > 0)
result.Path = str.Substring(1, queryOrFragmentTokenPosition - 1);
str = str.Substring(queryOrFragmentTokenPosition);
result.Path = str.Substring(1);
int fragmentTokenPosition = FindSectionPosition(str, new string[] { "#" });
if (fragmentTokenPosition > 0)
result.Query = str.Substring(0, fragmentTokenPosition);
str = str.Substring(fragmentTokenPosition);
else if (fragmentTokenPosition == -1)
int FindSectionPosition(string source, string[] stopTokens)
int[] positions = new int[stopTokens.Length];
for (int i = 0; i < stopTokens.Length; i++)
positions[i] = source.IndexOf(stopTokens[i], StringComparison.InvariantCulture);
int minIndex = FindPositiveMinIndex(positions);
return minIndex >= 0 ? positions[minIndex] : -1;
int FindPositiveMinIndex(int[] input)
if (input == null || input.Length == 0)
for (int i = 0; i < input.Length; i++)
else if (input[i] < input[minIndex])
app.MapPost("/Directory", (string[] files) =>
DirectoryResponse result = new DirectoryResponse();
for (int i = 0; i < files.Length; i++)
FilePathData filePathData = GetFilePathData(files[i]);
DirectoryResponse parent = result;
for (int j = 0; j < filePathData.ParentDirectoryLevel.Count; j++)
int index = FindIndex(parent.SubDirectories, filePathData.ParentDirectoryLevel[j]);
parent = parent.SubDirectories[index];
var dir = new DirectoryResponse();
dir.Name = filePathData.ParentDirectoryLevel[j];
parent.SubDirectories.Add(dir);
parent.Files.Add(filePathData.FileName);
return Results.Json(result);
FilePathData GetFilePathData(string file)
FilePathData result = new FilePathData();
string[] split = file.Split("\\");
for (; i < split.Length - 1; i++)
result.ParentDirectoryLevel.Add(split[i]);
result.FileName = split[i];
int FindIndex(List<DirectoryResponse> list, string name)
for (int i = 0; i < list.Count; i++)
if (list[i].Name == name)
app.MapPost("/FindExclusive", (int[][] numbers) =>
int[] left = new int[numbers.Length];
int[] right = new int[numbers.Length];
for (int i = 0;i < numbers.Length; i++)
var element = numbers[i];
Dictionary<int, int> rightNumbers = new Dictionary<int, int>();
for (int i = 0; i < right.Length; i++)
if (rightNumbers.ContainsKey(right[i]))
rightNumbers.Add(right[i], 1);
for (int i = 0; i < left.Length; i++)
if (rightNumbers.ContainsKey(left[i]))
rightNumbers.Remove(left[i]);
int[] result = rightNumbers.Keys.ToArray();
return Results.Json(result);
app.MapPost("/FindCommon", (int[][] numbers) =>
int[] left = new int[numbers.Length];
int[] right = new int[numbers.Length];
for (int i = 0; i < numbers.Length; i++)
var element = numbers[i];
Dictionary<int, int> keyReducer = new Dictionary<int, int>();
for (int i = 0; i < right.Length; i++)
if (!keyReducer.ContainsKey(right[i]))
keyReducer.Add(right[i], 0);
for (int i = 0; i < left.Length; i++)
if (keyReducer.ContainsKey(left[i]))
int[] keys = keyReducer.Keys.ToArray();
List<int> intersectionResult = new List<int>();
for (int i = 0; i < keys.Length; i++)
intersectionResult.Add(key);
int[] result = new int[intersectionResult.Count];
for (int i = 0; i < intersectionResult.Count; i++)
result[i] = intersectionResult[i];
return Results.Json(result);
app.MapPost("/FindAll", (int[][] numbers) =>
int[] left = new int[numbers.Length];
int[] right = new int[numbers.Length];
for (int i = 0; i < numbers.Length; i++)
var element = numbers[i];
Dictionary<int, int> keyAccumulator = new Dictionary<int, int>();
for (int i = 0; i < right.Length; i++)
if (keyAccumulator.ContainsKey(right[i]))
keyAccumulator.Add(right[i], 1);
for (int i = 0; i < left.Length; i++)
if (keyAccumulator.ContainsKey(left[i]))
keyAccumulator.Add(left[i], 1);
int[] result = keyAccumulator.Keys.ToArray();
return Results.Json(result);
class FormatNumberRangeModel
public int Low { get; set; }
public int High { get; set; }
public int[] AllRows { get; set; } = new int[0];
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int[] CurrentPageRows { get; set; } = new int[0];
public int From { get; set; }
public int To { get; set; }
public int Total { get; set; }
public string Scheme { get; set; } = string.Empty;
public string UserInfo { get; set; } = string.Empty;
public string Host { get; set; } = string.Empty;
public string Port { get; set; } = string.Empty;
public string Path { get; set; } = string.Empty;
public string Query { get; set; } = string.Empty;
public string Fragment { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public List<DirectoryResponse> SubDirectories { get; set; } = new List<DirectoryResponse>();
public List<string> Files { get; set; } = new List<string>();
public List<string> ParentDirectoryLevel { get; set; } = new List<string>();
public string FileName { get; set; } = string.Empty;