File size: 859 Bytes
a213de4
 
 
57476c2
a213de4
 
 
 
 
57476c2
a213de4
 
57476c2
a213de4
 
57476c2
a213de4
 
 
 
 
 
57476c2
a213de4
 
 
 
57476c2
 
a213de4
 
 
57476c2
a213de4
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List

app = FastAPI(
    title="Brand Sort API",
    description="Sorts a list of brands alphabetically and removes duplicates.",
    version="1.0.0"
)

class SortRequest(BaseModel):
    items: List[str]

class SortResponse(BaseModel):
    sorted: List[str]

@app.get("/")
def root():
    return {
        "message": "Brand Sort API is running.",
        "endpoint": "/sort"
    }

@app.post("/sort", response_model=SortResponse)
def sort_brands(request: SortRequest):
    # Remove duplicates while preserving first occurrence
    unique_items = list(dict.fromkeys(request.items))

    # Sort alphabetically (case-insensitive)
    sorted_items = sorted(unique_items, key=str.lower)

    return {"sorted": sorted_items}

@app.get("/health")
def health():
    return {"status": "ok"}