dancealgorithm 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dancealgorithm/__init__.py +128 -0
- dancealgorithm/__main__.py +8 -0
- dancealgorithm/assignments/DA - Campus/Solution.java +198 -0
- dancealgorithm/assignments/DA - Campus/__init__.py +34 -0
- dancealgorithm/assignments/DA - Flight/Solution.java +204 -0
- dancealgorithm/assignments/DA - Flight/__init__.py +34 -0
- dancealgorithm/assignments/DA - Smart/Solution.java +220 -0
- dancealgorithm/assignments/DA - Smart/__init__.py +34 -0
- dancealgorithm/assignments/DA.txt +29 -0
- dancealgorithm/assignments/FK - Advertisement/Solution.java +141 -0
- dancealgorithm/assignments/FK - Advertisement/__init__.py +34 -0
- dancealgorithm/assignments/FK - Electric/Solution.java +168 -0
- dancealgorithm/assignments/FK - Electric/__init__.py +34 -0
- dancealgorithm/assignments/FK - Emergency/Solution.java +159 -0
- dancealgorithm/assignments/FK - Emergency/__init__.py +34 -0
- dancealgorithm/assignments/FK.txt +26 -0
- dancealgorithm/assignments/GC - Classroom/Solution.java +87 -0
- dancealgorithm/assignments/GC - Classroom/__init__.py +34 -0
- dancealgorithm/assignments/GC - Interference/Solution.java +88 -0
- dancealgorithm/assignments/GC - Interference/__init__.py +34 -0
- dancealgorithm/assignments/GC - University/Solution.java +88 -0
- dancealgorithm/assignments/GC - University/__init__.py +34 -0
- dancealgorithm/assignments/GC.txt +25 -0
- dancealgorithm/assignments/K - Cloud/Solution.java +115 -0
- dancealgorithm/assignments/K - Cloud/__init__.py +34 -0
- dancealgorithm/assignments/K - Disaster/Solution.java +116 -0
- dancealgorithm/assignments/K - Disaster/__init__.py +34 -0
- dancealgorithm/assignments/K - Hospital/Solution.java +115 -0
- dancealgorithm/assignments/K - Hospital/__init__.py +34 -0
- dancealgorithm/assignments/K.txt +38 -0
- dancealgorithm/assignments/LC - Delivery/Solution.java +180 -0
- dancealgorithm/assignments/LC - Delivery/__init__.py +34 -0
- dancealgorithm/assignments/LC - Drilling/Solution.java +197 -0
- dancealgorithm/assignments/LC - Drilling/__init__.py +34 -0
- dancealgorithm/assignments/LC - Flight/Solution.java +180 -0
- dancealgorithm/assignments/LC - Flight/__init__.py +34 -0
- dancealgorithm/assignments/LC.txt +31 -0
- dancealgorithm/assignments/MS - Commerce/Solution.java +162 -0
- dancealgorithm/assignments/MS - Commerce/__init__.py +34 -0
- dancealgorithm/assignments/MS - Commerce/dataset.csv +16 -0
- dancealgorithm/assignments/MS - Customer/Solution.java +144 -0
- dancealgorithm/assignments/MS - Customer/__init__.py +34 -0
- dancealgorithm/assignments/MS - Customer/dataset.csv +16 -0
- dancealgorithm/assignments/MS - Library/Solution.java +162 -0
- dancealgorithm/assignments/MS - Library/__init__.py +34 -0
- dancealgorithm/assignments/MS - Library/dataset.csv +16 -0
- dancealgorithm/assignments/MS.txt +32 -0
- dancealgorithm/assignments/MSG - Emergency/Solution.java +211 -0
- dancealgorithm/assignments/MSG - Emergency/__init__.py +34 -0
- dancealgorithm/assignments/MSG - Logistics/Solution.java +189 -0
- dancealgorithm/assignments/MSG - Logistics/__init__.py +34 -0
- dancealgorithm/assignments/MSG - Skyconnect/Solution.java +204 -0
- dancealgorithm/assignments/MSG - Skyconnect/__init__.py +34 -0
- dancealgorithm/assignments/MSG.txt +28 -0
- dancealgorithm/assignments/QS - Commerce/Solution.java +147 -0
- dancealgorithm/assignments/QS - Commerce/__init__.py +34 -0
- dancealgorithm/assignments/QS - Commerce/dataset.csv +16 -0
- dancealgorithm/assignments/QS - Movie/Solution.java +148 -0
- dancealgorithm/assignments/QS - Movie/__init__.py +34 -0
- dancealgorithm/assignments/QS - Movie/dataset.csv +16 -0
- dancealgorithm/assignments/QS - Online/Solution.java +147 -0
- dancealgorithm/assignments/QS - Online/__init__.py +34 -0
- dancealgorithm/assignments/QS - Online/dataset.csv +16 -0
- dancealgorithm/assignments/QS.txt +33 -0
- dancealgorithm/assignments/__init__.py +65 -0
- dancealgorithm/cli.py +349 -0
- dancealgorithm/config.py +125 -0
- dancealgorithm/downloader.py +330 -0
- dancealgorithm-0.1.0.dist-info/METADATA +199 -0
- dancealgorithm-0.1.0.dist-info/RECORD +74 -0
- dancealgorithm-0.1.0.dist-info/WHEEL +5 -0
- dancealgorithm-0.1.0.dist-info/entry_points.txt +3 -0
- dancealgorithm-0.1.0.dist-info/licenses/LICENSE +22 -0
- dancealgorithm-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Function
|
|
5
|
+
public class Solution {
|
|
6
|
+
static final double INF = 999999;
|
|
7
|
+
|
|
8
|
+
public static void main(String args[]) {
|
|
9
|
+
Scanner sc = new Scanner(System.in);
|
|
10
|
+
|
|
11
|
+
int iSize = 9;
|
|
12
|
+
|
|
13
|
+
String sNames[] = {
|
|
14
|
+
"Delhi (Ambulance)",
|
|
15
|
+
"Noida",
|
|
16
|
+
"Ghaziabad",
|
|
17
|
+
"Gurugram",
|
|
18
|
+
"Faridabad",
|
|
19
|
+
"Meerut",
|
|
20
|
+
"AIIMS Hospital",
|
|
21
|
+
"Safdarjung Hospital",
|
|
22
|
+
"Apollo Hospital"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// 2. Create Adjacency Matrix
|
|
26
|
+
double dGraph[][] = new double[iSize][iSize];
|
|
27
|
+
|
|
28
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
29
|
+
Arrays.fill(dGraph[iCnt], INF);
|
|
30
|
+
dGraph[iCnt][iCnt] = 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 3. Add Roads and Travel Times
|
|
34
|
+
dGraph[0][1] = dGraph[1][0] = 4;
|
|
35
|
+
dGraph[0][2] = dGraph[2][0] = 2;
|
|
36
|
+
dGraph[1][3] = dGraph[3][1] = 5;
|
|
37
|
+
dGraph[2][1] = dGraph[1][2] = 1;
|
|
38
|
+
dGraph[2][4] = dGraph[4][2] = 8;
|
|
39
|
+
dGraph[2][3] = dGraph[3][2] = 7;
|
|
40
|
+
dGraph[3][5] = dGraph[5][3] = 3;
|
|
41
|
+
dGraph[4][5] = dGraph[5][4] = 2;
|
|
42
|
+
dGraph[3][6] = dGraph[6][3] = 6;
|
|
43
|
+
dGraph[5][7] = dGraph[7][5] = 4;
|
|
44
|
+
dGraph[5][8] = dGraph[8][5] = 5.5;
|
|
45
|
+
|
|
46
|
+
// 4. Display Locations
|
|
47
|
+
System.out.println("\nAvailable Locations:");
|
|
48
|
+
|
|
49
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
50
|
+
System.out.println(iCnt + " : " + sNames[iCnt]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 5. Get Source and Destination
|
|
54
|
+
System.out.print("\nEnter Ambulance Location: ");
|
|
55
|
+
String sSource = sc.nextLine();
|
|
56
|
+
|
|
57
|
+
System.out.print("Enter Destination Hospital: ");
|
|
58
|
+
String sDestination = sc.nextLine();
|
|
59
|
+
|
|
60
|
+
int iSource = findLocationIndex(sSource, sNames, iSize);
|
|
61
|
+
int iDestination = findLocationIndex(sDestination, sNames, iSize);
|
|
62
|
+
|
|
63
|
+
if (iSource == -1 || iDestination == -1) {
|
|
64
|
+
System.out.println("Invalid Location!");
|
|
65
|
+
sc.close();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 6. Apply Dijkstra Algorithm
|
|
70
|
+
Dijkstra(dGraph, iSource, iSize, sNames);
|
|
71
|
+
|
|
72
|
+
sc.close();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 7. Find Minimum Distance Vertex
|
|
76
|
+
static int MinDistance(double dDist[], boolean bVisited[], int iSize) {
|
|
77
|
+
double dMin = INF;
|
|
78
|
+
int iMinIndex = -1;
|
|
79
|
+
|
|
80
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
81
|
+
if (!bVisited[iCnt] && dDist[iCnt] < dMin) {
|
|
82
|
+
dMin = dDist[iCnt];
|
|
83
|
+
iMinIndex = iCnt;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return iMinIndex;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 8. Print Shortest Path
|
|
91
|
+
static void PrintPath(int iPrev[], int iTarget, String sNames[]) {
|
|
92
|
+
if (iPrev[iTarget] != -1) {
|
|
93
|
+
PrintPath(iPrev, iPrev[iTarget], sNames);
|
|
94
|
+
System.out.print(" -> ");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
System.out.print(sNames[iTarget]);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// 9. Dijkstra Function
|
|
101
|
+
static void Dijkstra(double dGraph[][], int iSource, int iSize, String sNames[]) {
|
|
102
|
+
double dDist[] = new double[iSize];
|
|
103
|
+
boolean bVisited[] = new boolean[iSize];
|
|
104
|
+
int iPrev[] = new int[iSize];
|
|
105
|
+
|
|
106
|
+
Arrays.fill(dDist, INF);
|
|
107
|
+
Arrays.fill(iPrev, -1);
|
|
108
|
+
|
|
109
|
+
dDist[iSource] = 0;
|
|
110
|
+
|
|
111
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
112
|
+
int iU = MinDistance(dDist, bVisited, iSize);
|
|
113
|
+
|
|
114
|
+
if (iU == -1) {
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
bVisited[iU] = true;
|
|
119
|
+
|
|
120
|
+
for (int iV = 0; iV < iSize; iV++) {
|
|
121
|
+
if (!bVisited[iV] &&
|
|
122
|
+
dGraph[iU][iV] != INF &&
|
|
123
|
+
dDist[iU] + dGraph[iU][iV] < dDist[iV]) {
|
|
124
|
+
dDist[iV] = dDist[iU] + dGraph[iU][iV];
|
|
125
|
+
iPrev[iV] = iU;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 10. Display Shortest Routes
|
|
131
|
+
System.out.println("\nShortest Routes from Ambulance:");
|
|
132
|
+
|
|
133
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
134
|
+
System.out.print("\n" + sNames[iCnt] + " : ");
|
|
135
|
+
|
|
136
|
+
if (dDist[iCnt] == INF) {
|
|
137
|
+
System.out.println("Unreachable");
|
|
138
|
+
} else {
|
|
139
|
+
System.out.printf("%.2f minutes | Route: ", dDist[iCnt]);
|
|
140
|
+
PrintPath(iPrev, iCnt, sNames);
|
|
141
|
+
System.out.println();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 11. Find Nearest Hospital
|
|
146
|
+
int iHospitals[] = { 6, 7, 8 };
|
|
147
|
+
int iBestHospital = -1;
|
|
148
|
+
double dMinTime = INF;
|
|
149
|
+
|
|
150
|
+
for (int iCnt = 0; iCnt < iHospitals.length; iCnt++) {
|
|
151
|
+
int iH = iHospitals[iCnt];
|
|
152
|
+
|
|
153
|
+
if (dDist[iH] < dMinTime) {
|
|
154
|
+
dMinTime = dDist[iH];
|
|
155
|
+
iBestHospital = iH;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
System.out.println("\n--------------------------------------");
|
|
160
|
+
|
|
161
|
+
if (iBestHospital != -1) {
|
|
162
|
+
System.out.println("Nearest Hospital: " + sNames[iBestHospital]);
|
|
163
|
+
System.out.printf("Minimum Travel Time: %.2f minutes%n", dMinTime);
|
|
164
|
+
} else {
|
|
165
|
+
System.out.println("No reachable hospital found.");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
System.out.println("--------------------------------------");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Helper to resolve location by name, index, or "index : name"
|
|
172
|
+
static int findLocationIndex(String sInput, String sNames[], int iSize) {
|
|
173
|
+
if (sInput == null) {
|
|
174
|
+
return -1;
|
|
175
|
+
}
|
|
176
|
+
sInput = sInput.trim();
|
|
177
|
+
if (sInput.isEmpty()) {
|
|
178
|
+
return -1;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (sInput.contains(":")) {
|
|
182
|
+
String parts[] = sInput.split(":", 2);
|
|
183
|
+
int idx = findLocationIndex(parts[0], sNames, iSize);
|
|
184
|
+
if (idx != -1) return idx;
|
|
185
|
+
idx = findLocationIndex(parts[1], sNames, iSize);
|
|
186
|
+
if (idx != -1) return idx;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (sInput.contains("-")) {
|
|
190
|
+
String parts[] = sInput.split("-", 2);
|
|
191
|
+
int idx = findLocationIndex(parts[0], sNames, iSize);
|
|
192
|
+
if (idx != -1) return idx;
|
|
193
|
+
idx = findLocationIndex(parts[1], sNames, iSize);
|
|
194
|
+
if (idx != -1) return idx;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
try {
|
|
198
|
+
int idx = Integer.parseInt(sInput);
|
|
199
|
+
if (idx >= 0 && idx < iSize) {
|
|
200
|
+
return idx;
|
|
201
|
+
}
|
|
202
|
+
} catch (NumberFormatException ignored) {
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (int i = 0; i < iSize; i++) {
|
|
206
|
+
if (sNames[i].equalsIgnoreCase(sInput)) {
|
|
207
|
+
return i;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
for (int i = 0; i < iSize; i++) {
|
|
212
|
+
if (sInput.toLowerCase().contains(sNames[i].toLowerCase()) ||
|
|
213
|
+
sNames[i].toLowerCase().contains(sInput.toLowerCase())) {
|
|
214
|
+
return i;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return -1;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "DA - Smart"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
==================================================
|
|
2
|
+
4. DIJKSTRA'S ALGORITHM
|
|
3
|
+
==================================================
|
|
4
|
+
|
|
5
|
+
ALGORITHM:
|
|
6
|
+
1. Initialize all distances to INF.
|
|
7
|
+
2. Set source distance = 0.
|
|
8
|
+
3. Mark all vertices as unvisited.
|
|
9
|
+
4. Select the unvisited vertex with minimum distance.
|
|
10
|
+
5. Mark the selected vertex as visited.
|
|
11
|
+
6. Check all adjacent vertices.
|
|
12
|
+
7. Update distance if a shorter path is found.
|
|
13
|
+
8. Repeat until all reachable vertices are processed.
|
|
14
|
+
9. Use prev[] to reconstruct the shortest path.
|
|
15
|
+
|
|
16
|
+
FORMULA:
|
|
17
|
+
dist[v] = min(dist[v], dist[u] + weight[u][v])
|
|
18
|
+
|
|
19
|
+
RECURRENCE:
|
|
20
|
+
Not recursive. Uses Greedy technique.
|
|
21
|
+
|
|
22
|
+
TIME COMPLEXITY (ADJACENCY MATRIX):
|
|
23
|
+
Best Case : O(V^2)
|
|
24
|
+
Average Case : O(V^2)
|
|
25
|
+
Worst Case : O(V^2)
|
|
26
|
+
Space : O(V) auxiliary space
|
|
27
|
+
|
|
28
|
+
IMPORTANT:
|
|
29
|
+
1. Dijkstra requires non-negative edge weights.
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Function
|
|
5
|
+
public class Solution
|
|
6
|
+
{
|
|
7
|
+
public static void main(String args[])
|
|
8
|
+
{
|
|
9
|
+
Scanner sc = new Scanner(System.in);
|
|
10
|
+
|
|
11
|
+
System.out.print("Enter total commercial airtime (minutes): ");
|
|
12
|
+
double dCapacity = sc.nextDouble();
|
|
13
|
+
|
|
14
|
+
System.out.print("Enter number of ad requests: ");
|
|
15
|
+
int iSize = sc.nextInt();
|
|
16
|
+
|
|
17
|
+
AdRequest ads[] = new AdRequest[iSize];
|
|
18
|
+
|
|
19
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++)
|
|
20
|
+
{
|
|
21
|
+
sc.nextLine();
|
|
22
|
+
|
|
23
|
+
System.out.println("\nEnter details for Ad " + (iCnt + 1));
|
|
24
|
+
|
|
25
|
+
System.out.print("Campaign Name: ");
|
|
26
|
+
String sName = sc.nextLine();
|
|
27
|
+
|
|
28
|
+
System.out.print("Duration (minutes): ");
|
|
29
|
+
double dDuration = sc.nextDouble();
|
|
30
|
+
|
|
31
|
+
System.out.print("Revenue (Rs.): ");
|
|
32
|
+
double dRevenue = sc.nextDouble();
|
|
33
|
+
|
|
34
|
+
System.out.print("Divisible? (1 = Yes, 0 = No): ");
|
|
35
|
+
int iChoice = sc.nextInt();
|
|
36
|
+
|
|
37
|
+
ads[iCnt] = new AdRequest(sName, dDuration, dRevenue, iChoice == 1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
Sort(ads, iSize);
|
|
41
|
+
AllocateAdSlots(ads, dCapacity);
|
|
42
|
+
|
|
43
|
+
sc.close();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 2. Ad Request Entity
|
|
47
|
+
static class AdRequest
|
|
48
|
+
{
|
|
49
|
+
String sName;
|
|
50
|
+
double dDuration, dRevenue, dRatio;
|
|
51
|
+
boolean bDivisible;
|
|
52
|
+
|
|
53
|
+
AdRequest(String sName, double dDuration, double dRevenue, boolean bDivisible)
|
|
54
|
+
{
|
|
55
|
+
this.sName = sName;
|
|
56
|
+
this.dDuration = dDuration;
|
|
57
|
+
this.dRevenue = dRevenue;
|
|
58
|
+
this.dRatio = dRevenue / dDuration;
|
|
59
|
+
this.bDivisible = bDivisible;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// 3. Swap Function
|
|
64
|
+
static void Swap(AdRequest ads[], int i, int j)
|
|
65
|
+
{
|
|
66
|
+
AdRequest temp = ads[i];
|
|
67
|
+
ads[i] = ads[j];
|
|
68
|
+
ads[j] = temp;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 4. Sort Function - Descending Order of Revenue/Duration
|
|
72
|
+
static void Sort(AdRequest ads[], int iSize)
|
|
73
|
+
{
|
|
74
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++)
|
|
75
|
+
{
|
|
76
|
+
for (int j = 0; j < iSize - iCnt - 1; j++)
|
|
77
|
+
{
|
|
78
|
+
if (ads[j].dRatio < ads[j + 1].dRatio)
|
|
79
|
+
{
|
|
80
|
+
Swap(ads, j, j + 1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 5. Allocate Advertisement Slots
|
|
87
|
+
static void AllocateAdSlots(AdRequest ads[], double dCapacity)
|
|
88
|
+
{
|
|
89
|
+
double dTotalRevenue = 0;
|
|
90
|
+
double dAllocatedTime = 0;
|
|
91
|
+
|
|
92
|
+
System.out.println("\nAdvertisement Slot Allocation:");
|
|
93
|
+
|
|
94
|
+
System.out.printf("%-22s %-15s %-15s %-15s%n",
|
|
95
|
+
"Campaign", "Duration", "Revenue", "Airtime Taken");
|
|
96
|
+
|
|
97
|
+
for (int iCnt = 0; iCnt < ads.length; iCnt++)
|
|
98
|
+
{
|
|
99
|
+
if (dCapacity <= 0)
|
|
100
|
+
{
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (ads[iCnt].dDuration <= dCapacity)
|
|
105
|
+
{
|
|
106
|
+
dCapacity -= ads[iCnt].dDuration;
|
|
107
|
+
dAllocatedTime += ads[iCnt].dDuration;
|
|
108
|
+
dTotalRevenue += ads[iCnt].dRevenue;
|
|
109
|
+
|
|
110
|
+
System.out.printf("%-22s %-15.2f %-15.2f 100%%%n",
|
|
111
|
+
ads[iCnt].sName,
|
|
112
|
+
ads[iCnt].dDuration,
|
|
113
|
+
ads[iCnt].dRevenue);
|
|
114
|
+
}
|
|
115
|
+
else if (ads[iCnt].bDivisible)
|
|
116
|
+
{
|
|
117
|
+
double dFraction = dCapacity / ads[iCnt].dDuration;
|
|
118
|
+
double dPartialRevenue = dFraction * ads[iCnt].dRevenue;
|
|
119
|
+
|
|
120
|
+
dAllocatedTime += dCapacity;
|
|
121
|
+
dTotalRevenue += dPartialRevenue;
|
|
122
|
+
|
|
123
|
+
System.out.printf("%-22s %-15.2f %-15.2f %.2f%%%n",
|
|
124
|
+
ads[iCnt].sName,
|
|
125
|
+
dCapacity,
|
|
126
|
+
dPartialRevenue,
|
|
127
|
+
dFraction * 100);
|
|
128
|
+
|
|
129
|
+
dCapacity = 0;
|
|
130
|
+
}
|
|
131
|
+
else
|
|
132
|
+
{
|
|
133
|
+
System.out.println(ads[iCnt].sName + " skipped (Indivisible)");
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
System.out.println("-----------------------------------------------");
|
|
138
|
+
System.out.printf("Total Airtime Used: %.2f minutes%n", dAllocatedTime);
|
|
139
|
+
System.out.printf("Total Revenue: Rs. %.2f%n", dTotalRevenue);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "FK - Advertisement"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Class
|
|
5
|
+
|
|
6
|
+
class Solution {
|
|
7
|
+
// 2. Main Function
|
|
8
|
+
|
|
9
|
+
public static void main(String A[]) {
|
|
10
|
+
Scanner sobj = new Scanner(System.in);
|
|
11
|
+
|
|
12
|
+
double dCapacity;
|
|
13
|
+
int iSize;
|
|
14
|
+
|
|
15
|
+
// 6. Accept Total Energy Capacity
|
|
16
|
+
|
|
17
|
+
System.out.print("Enter total grid capacity (kWh): ");
|
|
18
|
+
dCapacity = sobj.nextDouble();
|
|
19
|
+
|
|
20
|
+
// 7. Accept Number of EV Requests
|
|
21
|
+
|
|
22
|
+
System.out.print("Enter number of EV requests: ");
|
|
23
|
+
iSize = sobj.nextInt();
|
|
24
|
+
|
|
25
|
+
EVRequest Arr[] = new EVRequest[iSize];
|
|
26
|
+
|
|
27
|
+
// 8. Accept EV Request Details
|
|
28
|
+
|
|
29
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
30
|
+
String sVehicleID;
|
|
31
|
+
double dEnergy;
|
|
32
|
+
double dRevenue;
|
|
33
|
+
int iChoice;
|
|
34
|
+
|
|
35
|
+
System.out.println("\nEnter details of vehicle " + (iCnt + 1));
|
|
36
|
+
|
|
37
|
+
System.out.print("Vehicle ID / Model: ");
|
|
38
|
+
sVehicleID = sobj.next();
|
|
39
|
+
|
|
40
|
+
System.out.print("Energy required (kWh): ");
|
|
41
|
+
dEnergy = sobj.nextDouble();
|
|
42
|
+
|
|
43
|
+
System.out.print("Revenue (Rs.): ");
|
|
44
|
+
dRevenue = sobj.nextDouble();
|
|
45
|
+
|
|
46
|
+
System.out.print("Divisible? (1 = Yes, 0 = No): ");
|
|
47
|
+
iChoice = sobj.nextInt();
|
|
48
|
+
|
|
49
|
+
Arr[iCnt] = new EVRequest(sVehicleID, dEnergy, dRevenue, iChoice == 1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 9. Call Fractional Knapsack
|
|
53
|
+
|
|
54
|
+
AllocateChargingSlots(Arr, dCapacity);
|
|
55
|
+
|
|
56
|
+
sobj.close();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 3. EV Request Entity
|
|
60
|
+
// Stores vehicle ID, energy, revenue and charging type.
|
|
61
|
+
|
|
62
|
+
static class EVRequest {
|
|
63
|
+
String sVehicleID;
|
|
64
|
+
double dEnergy;
|
|
65
|
+
double dRevenue;
|
|
66
|
+
double dRatio;
|
|
67
|
+
boolean bDivisible;
|
|
68
|
+
|
|
69
|
+
EVRequest(String sVehicleID, double dEnergy, double dRevenue, boolean bDivisible) {
|
|
70
|
+
this.sVehicleID = sVehicleID;
|
|
71
|
+
this.dEnergy = dEnergy;
|
|
72
|
+
this.dRevenue = dRevenue;
|
|
73
|
+
this.bDivisible = bDivisible;
|
|
74
|
+
this.dRatio = dRevenue / dEnergy;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 4. Swap Function
|
|
79
|
+
// Exchanges two EV requests.
|
|
80
|
+
|
|
81
|
+
public static void Swap(EVRequest Arr[], int iFirst, int iSecond) {
|
|
82
|
+
EVRequest temp = Arr[iFirst];
|
|
83
|
+
|
|
84
|
+
Arr[iFirst] = Arr[iSecond];
|
|
85
|
+
|
|
86
|
+
Arr[iSecond] = temp;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 5. Sort Function
|
|
90
|
+
// Sorts EV requests in descending order of revenue-to-energy ratio.
|
|
91
|
+
|
|
92
|
+
public static void Sort(EVRequest Arr[], int iSize) {
|
|
93
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
94
|
+
for (int jCnt = 0; jCnt < iSize - iCnt - 1; jCnt++) {
|
|
95
|
+
if (Arr[jCnt].dRatio < Arr[jCnt + 1].dRatio) {
|
|
96
|
+
Swap(Arr, jCnt, jCnt + 1);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 10. Allocate Charging Slots
|
|
103
|
+
// Allocates available energy to maximize total revenue.
|
|
104
|
+
|
|
105
|
+
public static void AllocateChargingSlots(EVRequest Arr[], double dCapacity) {
|
|
106
|
+
double dTotalRevenue = 0;
|
|
107
|
+
double dTotalEnergy = 0;
|
|
108
|
+
|
|
109
|
+
// 10.1 Sort Requests by Ratio
|
|
110
|
+
|
|
111
|
+
Sort(Arr, Arr.length);
|
|
112
|
+
|
|
113
|
+
// 10.2 Allocate Energy
|
|
114
|
+
|
|
115
|
+
System.out.println("\nEV Charging Slot Allocation:");
|
|
116
|
+
|
|
117
|
+
System.out.printf("%-18s %-15s %-15s %-12s%n",
|
|
118
|
+
"Vehicle ID", "Energy", "Revenue", "Allocated");
|
|
119
|
+
|
|
120
|
+
System.out.println("------------------------------------------------------------");
|
|
121
|
+
|
|
122
|
+
for (int iCnt = 0; iCnt < Arr.length; iCnt++) {
|
|
123
|
+
if (dCapacity <= 0) {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (Arr[iCnt].dEnergy <= dCapacity) {
|
|
128
|
+
dCapacity = dCapacity - Arr[iCnt].dEnergy;
|
|
129
|
+
|
|
130
|
+
dTotalEnergy = dTotalEnergy + Arr[iCnt].dEnergy;
|
|
131
|
+
|
|
132
|
+
dTotalRevenue = dTotalRevenue + Arr[iCnt].dRevenue;
|
|
133
|
+
|
|
134
|
+
System.out.printf("%-18s %-15.2f %-15.2f 100%%%n",
|
|
135
|
+
Arr[iCnt].sVehicleID,
|
|
136
|
+
Arr[iCnt].dEnergy,
|
|
137
|
+
Arr[iCnt].dRevenue);
|
|
138
|
+
} else if (Arr[iCnt].bDivisible) {
|
|
139
|
+
double dFraction = dCapacity / Arr[iCnt].dEnergy;
|
|
140
|
+
|
|
141
|
+
double dPartialRevenue = dFraction * Arr[iCnt].dRevenue;
|
|
142
|
+
|
|
143
|
+
dTotalEnergy = dTotalEnergy + dCapacity;
|
|
144
|
+
|
|
145
|
+
dTotalRevenue = dTotalRevenue + dPartialRevenue;
|
|
146
|
+
|
|
147
|
+
System.out.printf("%-18s %-15.2f %-15.2f %.2f%%%n",
|
|
148
|
+
Arr[iCnt].sVehicleID,
|
|
149
|
+
dCapacity,
|
|
150
|
+
dPartialRevenue,
|
|
151
|
+
dFraction * 100);
|
|
152
|
+
|
|
153
|
+
dCapacity = 0;
|
|
154
|
+
} else {
|
|
155
|
+
System.out.println(Arr[iCnt].sVehicleID +
|
|
156
|
+
" - Skipped (Full charging required)");
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 10.3 Display Final Result
|
|
161
|
+
|
|
162
|
+
System.out.println("------------------------------------------------------------");
|
|
163
|
+
|
|
164
|
+
System.out.printf("Total Energy Allocated : %.2f kWh%n", dTotalEnergy);
|
|
165
|
+
|
|
166
|
+
System.out.printf("Total Revenue Earned : Rs. %.2f%n", dTotalRevenue);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import sys
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
PATH = Path(__file__).resolve().parent
|
|
7
|
+
DIR = PATH
|
|
8
|
+
JAVA_SOLUTION = PATH / "Solution.java"
|
|
9
|
+
DATASET_PATH = PATH / "dataset.csv"
|
|
10
|
+
TITLE = "FK - Electric"
|
|
11
|
+
|
|
12
|
+
def get_code() -> str:
|
|
13
|
+
if JAVA_SOLUTION.exists():
|
|
14
|
+
return JAVA_SOLUTION.read_text(encoding="utf-8")
|
|
15
|
+
return ""
|
|
16
|
+
|
|
17
|
+
def extract(destination: Optional[str | Path] = None, overwrite: bool = True) -> Path:
|
|
18
|
+
from dancealgorithm.downloader import extract_bundled
|
|
19
|
+
from dancealgorithm.config import get_destination
|
|
20
|
+
target = Path(destination) if destination else get_destination()
|
|
21
|
+
extract_bundled(target, subject_filter=TITLE, overwrite=overwrite)
|
|
22
|
+
return target / PATH.name
|
|
23
|
+
|
|
24
|
+
def open_folder() -> None:
|
|
25
|
+
import os
|
|
26
|
+
if sys.platform == "win32":
|
|
27
|
+
os.system(f'explorer "{PATH}"')
|
|
28
|
+
|
|
29
|
+
def __getattr__(name: str):
|
|
30
|
+
if name in ("code", "java_code", "solution_code"):
|
|
31
|
+
return get_code()
|
|
32
|
+
if name in ("path", "folder", "dir"):
|
|
33
|
+
return PATH
|
|
34
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|