Skip to content

Uploaded Python solution files by Prasanth #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
74 changes: 74 additions & 0 deletions Solution-Prasanth/codechallenge1.py
Original file line number Diff line number Diff line change
@@ -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)
87 changes: 87 additions & 0 deletions Solution-Prasanth/codechallenge2.py
Original file line number Diff line number Diff line change
@@ -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)