I recently started learning python.
There is something I want to do using python, but I can't find a way to do it because I don't know the keyword.
Here's what I want to do:
What you want to do: Suppose there are three taxi companies in an area.(Company A, Company B, Company C) The number of taxis owned by each company is [Company A: 2] [Company B: 2] [Company C: 1]
Taxi callers are supposed to be in buildings in the area, and the distance from the taxi company to each building in the area is pre-created in a separate csv file.
If someone calls a taxi or repeats sending a taxi from a company close to the building where the taxi driver is located, there may be a situation where all the taxis leave. In such a case, what kind of function should Python be used to send a taxi from the building where the taxi driver is located?
Number of taxis in 3 companies
If you want to build this in python, what keywords should you search for and what syntax should you use?
I'm sorry to ask you such a rudimentary question, but I appreciate your cooperation.
As for the algorithm, I felt that you wanted to solve the car allocation problem with Greedy method.
Specifically, abstract the problem as follows:
resource[i]: Number of taxis owned by company i
cost[i][j] —Distance from building i to company j
jobs[i] —The building to which the i-th call was called
The procedure is to assign "Company with the shortest distance (Company with cost[i][j]>0
)" in turn (Company with the lowest distance (Company with cost[i]][j]
)"
This is what it looks like when you write a pseudo program:
result=[-1]*len(jobs)
for i, job in enumerate (jobs):
# find the shortest company
nearest=-1
min_cost=float("inf")
for company, lin enumerate (resource):
if r>0 and cost [job][company]<min_cost:
nearest=company
min_cost=cost[job][company]
if nearest==-1:
# There are no more taxis left.
break
# assign to the company one finds
resource [nearest] - = 1
result[i] = nearest
Perhaps you should learn about class and object orientation.
Search for things like "How to use Python class"
What Daaaaai 39 wants to do is create a "class" of taxi company A, taxi company B, and taxi company C (to be exact, create a taxi company class and initialize it with the values of each company A, B, and C) so that it can be solved in a very easy-to-understand and applicable way.
"I think it's probably a snake's foot, but other than that, it is necessary to calculate the two-dimensional vector, so it would be good to search by ""two-dimensional vector distance""."Such calculations should be implemented in the class as a method.
© 2024 OneMinuteCode. All rights reserved.