diff --git a/Solution-Prasanth/__pycache__/codechallenge1.cpython-39.pyc b/Solution-Prasanth/__pycache__/codechallenge1.cpython-39.pyc new file mode 100644 index 0000000..0813918 Binary files /dev/null and b/Solution-Prasanth/__pycache__/codechallenge1.cpython-39.pyc differ diff --git a/Solution-Prasanth/__pycache__/codechallenge2.cpython-39.pyc b/Solution-Prasanth/__pycache__/codechallenge2.cpython-39.pyc new file mode 100644 index 0000000..4f1461e Binary files /dev/null and b/Solution-Prasanth/__pycache__/codechallenge2.cpython-39.pyc differ diff --git a/Solution-Prasanth/codechallenge1.py b/Solution-Prasanth/codechallenge1.py new file mode 100644 index 0000000..f4fce76 --- /dev/null +++ b/Solution-Prasanth/codechallenge1.py @@ -0,0 +1,74 @@ +""" Problem Statement 1 + +- Given a list of purchases, each has an item and its quantity. +- Find the vendor for each purchase where cost is minimum (primary objective) and time-efficient (secondary objective). Do not assign multiple vendors to a single purchase. +- If a purchase is not possible, mark that purchase as not possible. +- Quantity will always be in KG. +- Use the data given in `vendors.csv`. + +**Input**: A CSV file `input.csv`. Each row contains the purchase id, item, and required quantity. + +**Expected Output**: A CSV file `output.csv`. Each row contains the purchase id, item, required quantity, indication if a purchase is possible (true/false), vendor, total cost, and delivery time. +""" + +import pandas as pd +df = pd.read_csv("C:/Users/3108p/code_challenge/vendors.csv") + +from flask import Flask, request, jsonify +app = Flask(__name__) + +purchases = [] +result = [] + +@app.route("/purchases", methods=["GET"]) +def get_purchase(): + return jsonify(purchases) + +@app.route("/purchases", methods=["POST"]) +def computing_result(): + result_dictionary={} + if request.is_json: + purchase = request.get_json() + purchases.append(purchase) + cost = 1000000 + mintime = 10000 + isavail = "false" + vendor = " " + total = 0 + time = 0 + for j in range(df.shape[0]): + + if purchase["item"] == df["Item"][j]: + + if df["Cost Per KG"][j] < cost: + cost = int(df["Cost Per KG"][j]) + mintime = int(df["Time to Deliver"][j]) + isavail = "true" + vendor = df["Vendor"][j] + total = int(df["Cost Per KG"][j])*int(purchase["Req. Quantity"]) + time = mintime + + elif int(df["Cost Per KG"][j]) == cost: + + if int(df["Time to Deliver"][j]) < mintime: + + mintime = int(df["Time to Deliver"][j]) + isavail = "true" + vendor = df["Vendor"][j] + total = int(df["Cost Per KG"][j])*int(purchase["Req. Quantity"]) + time = mintime + + result_dictionary["Purchase Id"] = purchase["Pur_id"] + result_dictionary["Item"] = purchase["item"] + result_dictionary["Quantity"] = purchase["Req. Quantity"] + result_dictionary["IsAvail"] = isavail + result_dictionary["Vendor"] = vendor + result_dictionary["Total cost"] = total + result_dictionary["Time"] = time + result.append(result_dictionary) + return result_dictionary,201 + return {"error": "Request must be JSON"}, 415 + +@app.route("/results", methods=["GET"]) +def get_result(): + return jsonify(result) \ No newline at end of file diff --git a/Solution-Prasanth/codechallenge2.py b/Solution-Prasanth/codechallenge2.py new file mode 100644 index 0000000..858dd67 --- /dev/null +++ b/Solution-Prasanth/codechallenge2.py @@ -0,0 +1,87 @@ +""" + +We have provided `vendor_capacities.csv` which contains vendor, item, cost, delivery time, quantity limit, and additional cost. + +- Given a list of purchases, each has an item and its quantity. +- Find the vendor for each purchase where cost is minimum (primary objective) and time-efficient (secondary objective). Do not assign multiple vendors to a single purchase. +- Consider the quantity limit and additional cost if a purchase quantity exceeds the quantity limit. If a purchase is not possible, mark that purchase impossible. +- Quantity will always be in KG. +- Use the data given in `vendor_capacities.csv`. + +**Input**: A CSV file input-capacities.csv. Each row contains the purchase id, item, and required quantity. + +**Expected Output**: Same as Problem statement 1. + +""" + +import pandas as pd +df = pd.read_csv("C:/Users/3108p/code_challenge/vendor-capacities.csv") + +from flask import Flask, request, jsonify +app = Flask(__name__) + +purchases = [] +result = [] + +@app.route("/purchases", methods=["GET"]) +def get_purchase(): + return jsonify(purchases) + +@app.route("/purchases", methods=["POST"]) +def computing_result(): + result_dictionary={} + if request.is_json: + purchase = request.get_json() + purchases.append(purchase) + mincost = 100000 + mintime = 1000 + isavail = "false" + vendor = " " + for j in range(df.shape[0]): + + if purchase["item"] == df["Item"][j]: + + if int(df["Default Quantity Limit in KG"][j]) >= int(purchase["Req. Quantity"]): + + cost = int(df["Cost Per KG"][j])*int(purchase["Req. Quantity"]) + + else: + + additional = int(df["Cost Per KG"][j]) + int(df["Additional Cost Per KG"][j]) + addtocost = ((int(purchase["Req. Quantity"])-int(df["Default Quantity Limit in KG"][j])) * additional) + cost = (int(df["Cost Per KG"][j]) * int(df["Default Quantity Limit in KG"][j])) + addtocost + + if cost < mincost: + mincost = int(cost) + mintime = int(df["Time to Deliver"][j]) + isavail = "true" + vendor = df["Vendor"][j] + + elif cost == mincost: + + if df["Time to Deliver"][j] < mintime: + + mintime = int(df["Time to Deliver"][j]) + isavail = "true" + vendor = df["Vendor"][j] + + result_dictionary["Purchase Id"] = purchase["Pur_id"] + result_dictionary["Item"] = purchase["item"] + result_dictionary["Quantity"] = purchase["Req. Quantity"] + result_dictionary["IsAvail"] = isavail + result_dictionary["Vendor"] = vendor + if mincost == 100000 and mintime == 1000: + total=0 + time=0 + else: + total=mincost + time=mintime + result_dictionary["Total cost"] = total + result_dictionary["Time"] = time + result.append(result_dictionary) + return result_dictionary,201 + return {"error": "Request must be JSON"}, 415 + +@app.route("/results", methods=["GET"]) +def get_result(): + return jsonify(result) \ No newline at end of file