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,128 @@
|
|
|
1
|
+
"""
|
|
2
|
+
DanceAlgorithm - Instant DAA Algorithms, Lab Problem Sheets, Datasets & Code Vault.
|
|
3
|
+
All 24 Practical Exam Problem Sheets & Algorithm Notes Bundled & Instantly Importable.
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any, Dict, List, Optional, Union
|
|
11
|
+
|
|
12
|
+
__version__ = "0.1.0"
|
|
13
|
+
|
|
14
|
+
# Import bundled assignments registry and sheets
|
|
15
|
+
from dancealgorithm.assignments import ASSIGNMENTS_DIR, REGISTRY, SHEETS
|
|
16
|
+
|
|
17
|
+
# Expose assignments mapping (e.g. dancealgorithm.assignments[1])
|
|
18
|
+
assignments: Dict[Any, Any] = REGISTRY
|
|
19
|
+
|
|
20
|
+
for sheet in SHEETS:
|
|
21
|
+
title = getattr(sheet, "TITLE", None)
|
|
22
|
+
if not title:
|
|
23
|
+
continue
|
|
24
|
+
|
|
25
|
+
sheet_aliases = {title}
|
|
26
|
+
# Sanitized alias
|
|
27
|
+
sheet_aliases.add(title.replace(" ", "_").replace("-", "_").replace("—", "_"))
|
|
28
|
+
sheet_aliases.add(title.replace(" ", "").replace("-", "").replace("—", ""))
|
|
29
|
+
|
|
30
|
+
for alias in sheet_aliases:
|
|
31
|
+
setattr(sys.modules[__name__], alias, sheet)
|
|
32
|
+
sys.modules[f"dancealgorithm.{alias}"] = sheet
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get(identifier: Union[int, str]) -> Any:
|
|
36
|
+
"""
|
|
37
|
+
Get a specific problem sheet module by number (1..24), ID (e.g. 'ps1', 'PS01'),
|
|
38
|
+
or keyword/name (e.g. 'Customer', 'MS - Customer', 'GC - Classroom').
|
|
39
|
+
"""
|
|
40
|
+
if identifier in REGISTRY:
|
|
41
|
+
return REGISTRY[identifier]
|
|
42
|
+
|
|
43
|
+
if isinstance(identifier, int):
|
|
44
|
+
if identifier in REGISTRY:
|
|
45
|
+
return REGISTRY[identifier]
|
|
46
|
+
|
|
47
|
+
norm = str(identifier).strip().lower().replace("-", "").replace("_", "").replace(" ", "").replace("—", "")
|
|
48
|
+
digits = "".join(filter(str.isdigit, norm))
|
|
49
|
+
if digits:
|
|
50
|
+
val = int(digits)
|
|
51
|
+
if val in REGISTRY:
|
|
52
|
+
return REGISTRY[val]
|
|
53
|
+
|
|
54
|
+
# Keyword search across sheet titles
|
|
55
|
+
for sheet in SHEETS:
|
|
56
|
+
clean_title = sheet.TITLE.lower().replace("-", "").replace("_", "").replace(" ", "").replace("—", "")
|
|
57
|
+
if norm in clean_title or clean_title in norm:
|
|
58
|
+
return sheet
|
|
59
|
+
|
|
60
|
+
raise KeyError(f"Problem sheet not found for '{identifier}'. Available: 1..24.")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
# Convenience alias: dancealgorithm.ps(1)
|
|
64
|
+
ps = get
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def list_assignments() -> List[Dict[str, Any]]:
|
|
68
|
+
"""
|
|
69
|
+
List all available DAA practical assignments with titles and paths.
|
|
70
|
+
"""
|
|
71
|
+
catalog = []
|
|
72
|
+
for sheet in SHEETS:
|
|
73
|
+
catalog.append({
|
|
74
|
+
"title": sheet.TITLE,
|
|
75
|
+
"path": sheet.PATH,
|
|
76
|
+
"has_solution": sheet.JAVA_SOLUTION.exists(),
|
|
77
|
+
"has_dataset": sheet.DATASET_PATH.exists(),
|
|
78
|
+
})
|
|
79
|
+
return catalog
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def extract(
|
|
83
|
+
sheet: Optional[Union[int, str]] = None,
|
|
84
|
+
destination: Optional[Union[str, Path]] = None,
|
|
85
|
+
overwrite: bool = True,
|
|
86
|
+
) -> List[Path]:
|
|
87
|
+
"""
|
|
88
|
+
Extract all problem sheets or a specific one to the destination directory.
|
|
89
|
+
Default destination is C:\\DAA_Assignments (or ~/DAA_Assignments).
|
|
90
|
+
"""
|
|
91
|
+
from dancealgorithm.config import get_destination
|
|
92
|
+
from dancealgorithm.downloader import extract_bundled
|
|
93
|
+
|
|
94
|
+
target = Path(destination) if destination else get_destination()
|
|
95
|
+
sub_filt = str(sheet) if sheet is not None else None
|
|
96
|
+
return extract_bundled(target, subject_filter=sub_filt, overwrite=overwrite)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def open_folder(destination: Optional[Union[str, Path]] = None) -> None:
|
|
100
|
+
"""
|
|
101
|
+
Open the assignments destination folder in Windows File Explorer.
|
|
102
|
+
"""
|
|
103
|
+
from dancealgorithm.config import get_destination
|
|
104
|
+
target = Path(destination) if destination else get_destination()
|
|
105
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
106
|
+
if sys.platform == "win32":
|
|
107
|
+
os.system(f'explorer "{target}"')
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def __getattr__(name: str) -> Any:
|
|
111
|
+
try:
|
|
112
|
+
return get(name)
|
|
113
|
+
except KeyError:
|
|
114
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
__all__ = [
|
|
118
|
+
"__version__",
|
|
119
|
+
"assignments",
|
|
120
|
+
"get",
|
|
121
|
+
"ps",
|
|
122
|
+
"list_assignments",
|
|
123
|
+
"extract",
|
|
124
|
+
"open_folder",
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Function
|
|
5
|
+
public class Solution {
|
|
6
|
+
static final int INF = 999999;
|
|
7
|
+
|
|
8
|
+
public static void main(String args[]) {
|
|
9
|
+
Scanner sc = new Scanner(System.in);
|
|
10
|
+
|
|
11
|
+
int iSize = 6;
|
|
12
|
+
|
|
13
|
+
String sNames[] = {
|
|
14
|
+
"PCCOE Main Server",
|
|
15
|
+
"PCCOE Computer Engineering",
|
|
16
|
+
"PCCOE IT Department",
|
|
17
|
+
"PCCOE Electronics Department",
|
|
18
|
+
"PCCOE Central Library",
|
|
19
|
+
"PCCOE Hostel"
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// 2. Create Adjacency Matrix
|
|
23
|
+
int iGraph[][] = {
|
|
24
|
+
{ 0, 4, 2, INF, INF, INF },
|
|
25
|
+
{ 4, 0, 1, 5, INF, INF },
|
|
26
|
+
{ 2, 1, 0, 8, 10, INF },
|
|
27
|
+
{ INF, 5, 8, 0, 2, 6 },
|
|
28
|
+
{ INF, INF, 10, 2, 0, 3 },
|
|
29
|
+
{ INF, INF, INF, 6, 3, 0 }
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// 3. Display Network Nodes
|
|
33
|
+
System.out.println("\nAvailable PCCOE Network Nodes:");
|
|
34
|
+
|
|
35
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
36
|
+
System.out.println(iCnt + " : " + sNames[iCnt]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 4. Get Source and Destination
|
|
40
|
+
System.out.print("\nEnter Source Node: ");
|
|
41
|
+
String sSource = sc.nextLine();
|
|
42
|
+
|
|
43
|
+
System.out.print("Enter Destination Node: ");
|
|
44
|
+
String sDestination = sc.nextLine();
|
|
45
|
+
|
|
46
|
+
int iSource = findNodeIndex(sSource, sNames, iSize);
|
|
47
|
+
int iDestination = findNodeIndex(sDestination, sNames, iSize);
|
|
48
|
+
|
|
49
|
+
if (iSource == -1 || iDestination == -1) {
|
|
50
|
+
System.out.println("Invalid Node!");
|
|
51
|
+
sc.close();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 5. Apply Dijkstra Algorithm
|
|
56
|
+
Dijkstra(iGraph, iSource, iDestination, iSize, sNames);
|
|
57
|
+
|
|
58
|
+
sc.close();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 6. Find Minimum Latency Node
|
|
62
|
+
static int MinLatency(int dDist[], boolean bVisited[], int iSize) {
|
|
63
|
+
int iMin = INF;
|
|
64
|
+
int iMinIndex = -1;
|
|
65
|
+
|
|
66
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
67
|
+
if (!bVisited[iCnt] && dDist[iCnt] < iMin) {
|
|
68
|
+
iMin = dDist[iCnt];
|
|
69
|
+
iMinIndex = iCnt;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return iMinIndex;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 7. Print Shortest Path
|
|
77
|
+
static void PrintPath(int iPrev[], int iTarget, String sNames[]) {
|
|
78
|
+
if (iPrev[iTarget] != -1) {
|
|
79
|
+
PrintPath(iPrev, iPrev[iTarget], sNames);
|
|
80
|
+
System.out.print(" -> ");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
System.out.print(sNames[iTarget]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 8. Dijkstra Algorithm
|
|
87
|
+
static void Dijkstra(int iGraph[][], int iSource,
|
|
88
|
+
int iDestination, int iSize, String sNames[]) {
|
|
89
|
+
int dDist[] = new int[iSize];
|
|
90
|
+
boolean bVisited[] = new boolean[iSize];
|
|
91
|
+
int iPrev[] = new int[iSize];
|
|
92
|
+
|
|
93
|
+
Arrays.fill(dDist, INF);
|
|
94
|
+
Arrays.fill(iPrev, -1);
|
|
95
|
+
|
|
96
|
+
dDist[iSource] = 0;
|
|
97
|
+
|
|
98
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
99
|
+
int iU = MinLatency(dDist, bVisited, iSize);
|
|
100
|
+
|
|
101
|
+
if (iU == -1) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
bVisited[iU] = true;
|
|
106
|
+
|
|
107
|
+
for (int iV = 0; iV < iSize; iV++) {
|
|
108
|
+
if (!bVisited[iV] &&
|
|
109
|
+
iGraph[iU][iV] != INF &&
|
|
110
|
+
dDist[iU] + iGraph[iU][iV] < dDist[iV]) {
|
|
111
|
+
dDist[iV] = dDist[iU] + iGraph[iU][iV];
|
|
112
|
+
iPrev[iV] = iU;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 9. Display Shortest Paths to All Nodes
|
|
118
|
+
System.out.println("\nMinimum Latency Routes:");
|
|
119
|
+
|
|
120
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
121
|
+
System.out.print("\n" + sNames[iCnt] + " : ");
|
|
122
|
+
|
|
123
|
+
if (dDist[iCnt] == INF) {
|
|
124
|
+
System.out.println("Unreachable");
|
|
125
|
+
} else {
|
|
126
|
+
System.out.print(dDist[iCnt] + " ms | Route: ");
|
|
127
|
+
PrintPath(iPrev, iCnt, sNames);
|
|
128
|
+
System.out.println();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 10. Display Selected Destination
|
|
133
|
+
System.out.println("\n----------------------------------------");
|
|
134
|
+
|
|
135
|
+
if (dDist[iDestination] == INF) {
|
|
136
|
+
System.out.println("Destination is unreachable.");
|
|
137
|
+
} else {
|
|
138
|
+
System.out.println("Target Node: " + sNames[iDestination]);
|
|
139
|
+
System.out.println("Minimum Latency: " + dDist[iDestination] + " ms");
|
|
140
|
+
|
|
141
|
+
System.out.print("Shortest Network Path: ");
|
|
142
|
+
PrintPath(iPrev, iDestination, sNames);
|
|
143
|
+
System.out.println();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
System.out.println("----------------------------------------");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Helper to resolve node by name, index, or "index : name"
|
|
150
|
+
static int findNodeIndex(String sInput, String sNames[], int iSize) {
|
|
151
|
+
if (sInput == null) {
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
154
|
+
sInput = sInput.trim();
|
|
155
|
+
if (sInput.isEmpty()) {
|
|
156
|
+
return -1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (sInput.contains(":")) {
|
|
160
|
+
String parts[] = sInput.split(":", 2);
|
|
161
|
+
int idx = findNodeIndex(parts[0], sNames, iSize);
|
|
162
|
+
if (idx != -1) return idx;
|
|
163
|
+
idx = findNodeIndex(parts[1], sNames, iSize);
|
|
164
|
+
if (idx != -1) return idx;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (sInput.contains("-")) {
|
|
168
|
+
String parts[] = sInput.split("-", 2);
|
|
169
|
+
int idx = findNodeIndex(parts[0], sNames, iSize);
|
|
170
|
+
if (idx != -1) return idx;
|
|
171
|
+
idx = findNodeIndex(parts[1], sNames, iSize);
|
|
172
|
+
if (idx != -1) return idx;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
int idx = Integer.parseInt(sInput);
|
|
177
|
+
if (idx >= 0 && idx < iSize) {
|
|
178
|
+
return idx;
|
|
179
|
+
}
|
|
180
|
+
} catch (NumberFormatException ignored) {
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
for (int i = 0; i < iSize; i++) {
|
|
184
|
+
if (sNames[i].equalsIgnoreCase(sInput)) {
|
|
185
|
+
return i;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (int i = 0; i < iSize; i++) {
|
|
190
|
+
if (sInput.toLowerCase().contains(sNames[i].toLowerCase()) ||
|
|
191
|
+
sNames[i].toLowerCase().contains(sInput.toLowerCase())) {
|
|
192
|
+
return i;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return -1;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -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 - Campus"
|
|
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,204 @@
|
|
|
1
|
+
|
|
2
|
+
import java.util.*;
|
|
3
|
+
|
|
4
|
+
// 1. Main Function
|
|
5
|
+
public class Solution {
|
|
6
|
+
static final int INF = 999999;
|
|
7
|
+
|
|
8
|
+
public static void main(String args[]) {
|
|
9
|
+
Scanner sc = new Scanner(System.in);
|
|
10
|
+
|
|
11
|
+
int iSize = 6;
|
|
12
|
+
|
|
13
|
+
String sNames[] = {
|
|
14
|
+
"Delhi",
|
|
15
|
+
"Mumbai",
|
|
16
|
+
"Ahmedabad",
|
|
17
|
+
"Hyderabad",
|
|
18
|
+
"Bengaluru",
|
|
19
|
+
"Chennai"
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// 2. Create Flight Adjacency Matrix
|
|
23
|
+
int iGraph[][] = {
|
|
24
|
+
{ 0, 130, 95, INF, INF, INF },
|
|
25
|
+
{ 130, 0, 75, 90, 105, INF },
|
|
26
|
+
{ 95, 75, 0, 100, INF, INF },
|
|
27
|
+
{ INF, 90, 100, 0, 75, 70 },
|
|
28
|
+
{ INF, 105, INF, 75, 0, 55 },
|
|
29
|
+
{ INF, INF, INF, 70, 55, 0 }
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// 3. Display Available Airports
|
|
33
|
+
System.out.println("\nAvailable Airports:");
|
|
34
|
+
|
|
35
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
36
|
+
System.out.println(iCnt + " : " + sNames[iCnt]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 4. Get Source and Destination
|
|
40
|
+
System.out.print("\nEnter Departure Airport: ");
|
|
41
|
+
String sSource = sc.nextLine();
|
|
42
|
+
|
|
43
|
+
System.out.print("Enter Arrival Airport: ");
|
|
44
|
+
String sDestination = sc.nextLine();
|
|
45
|
+
|
|
46
|
+
int iSource = findAirportIndex(sSource, sNames, iSize);
|
|
47
|
+
int iDestination = findAirportIndex(sDestination, sNames, iSize);
|
|
48
|
+
|
|
49
|
+
if (iSource == -1 || iDestination == -1) {
|
|
50
|
+
System.out.println("Invalid Airport!");
|
|
51
|
+
sc.close();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 5. Apply Dijkstra Algorithm
|
|
56
|
+
Dijkstra(iGraph, iSource, iDestination, iSize, sNames);
|
|
57
|
+
|
|
58
|
+
sc.close();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 6. Find Minimum Distance Vertex
|
|
62
|
+
static int MinDistance(int dDist[], boolean bVisited[], int iSize) {
|
|
63
|
+
int iMin = INF;
|
|
64
|
+
int iMinIndex = -1;
|
|
65
|
+
|
|
66
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
67
|
+
if (!bVisited[iCnt] && dDist[iCnt] < iMin) {
|
|
68
|
+
iMin = dDist[iCnt];
|
|
69
|
+
iMinIndex = iCnt;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return iMinIndex;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 7. Print Shortest Flight Path
|
|
77
|
+
static void PrintPath(int iPrev[], int iTarget, String sNames[]) {
|
|
78
|
+
if (iPrev[iTarget] != -1) {
|
|
79
|
+
PrintPath(iPrev, iPrev[iTarget], sNames);
|
|
80
|
+
System.out.print(" -> ");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
System.out.print(sNames[iTarget]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 8. Dijkstra Algorithm
|
|
87
|
+
static void Dijkstra(int iGraph[][], int iSource,
|
|
88
|
+
int iDestination, int iSize, String sNames[]) {
|
|
89
|
+
int dDist[] = new int[iSize];
|
|
90
|
+
boolean bVisited[] = new boolean[iSize];
|
|
91
|
+
int iPrev[] = new int[iSize];
|
|
92
|
+
|
|
93
|
+
Arrays.fill(dDist, INF);
|
|
94
|
+
Arrays.fill(iPrev, -1);
|
|
95
|
+
|
|
96
|
+
dDist[iSource] = 0;
|
|
97
|
+
|
|
98
|
+
for (int iCnt = 0; iCnt < iSize - 1; iCnt++) {
|
|
99
|
+
int iU = MinDistance(dDist, bVisited, iSize);
|
|
100
|
+
|
|
101
|
+
if (iU == -1) {
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
bVisited[iU] = true;
|
|
106
|
+
|
|
107
|
+
for (int iV = 0; iV < iSize; iV++) {
|
|
108
|
+
if (!bVisited[iV] &&
|
|
109
|
+
iGraph[iU][iV] != INF &&
|
|
110
|
+
dDist[iU] + iGraph[iU][iV] < dDist[iV]) {
|
|
111
|
+
dDist[iV] = dDist[iU] + iGraph[iU][iV];
|
|
112
|
+
iPrev[iV] = iU;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 9. Display Shortest Routes to All Airports
|
|
118
|
+
System.out.println("\nShortest Flight Routes:");
|
|
119
|
+
|
|
120
|
+
for (int iCnt = 0; iCnt < iSize; iCnt++) {
|
|
121
|
+
System.out.print("\n" + sNames[iCnt] + " : ");
|
|
122
|
+
|
|
123
|
+
if (dDist[iCnt] == INF) {
|
|
124
|
+
System.out.println("Unreachable");
|
|
125
|
+
} else {
|
|
126
|
+
System.out.print(dDist[iCnt] + " minutes | Route: ");
|
|
127
|
+
PrintPath(iPrev, iCnt, sNames);
|
|
128
|
+
System.out.println();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 10. Display Selected Destination
|
|
133
|
+
System.out.println("\n----------------------------------------");
|
|
134
|
+
|
|
135
|
+
if (dDist[iDestination] == INF) {
|
|
136
|
+
System.out.println("Destination is unreachable.");
|
|
137
|
+
} else {
|
|
138
|
+
System.out.println("Departure: " + sNames[iSource]);
|
|
139
|
+
System.out.println("Arrival: " + sNames[iDestination]);
|
|
140
|
+
|
|
141
|
+
System.out.println("Minimum Travel Time: " +
|
|
142
|
+
dDist[iDestination] + " minutes");
|
|
143
|
+
|
|
144
|
+
System.out.printf("Travel Time in Hours: %.2f hours%n",
|
|
145
|
+
dDist[iDestination] / 60.0);
|
|
146
|
+
|
|
147
|
+
System.out.print("Shortest Flight Route: ");
|
|
148
|
+
PrintPath(iPrev, iDestination, sNames);
|
|
149
|
+
System.out.println();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
System.out.println("----------------------------------------");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Helper to resolve airport by name, index, or "index : name"
|
|
156
|
+
static int findAirportIndex(String sInput, String sNames[], int iSize) {
|
|
157
|
+
if (sInput == null) {
|
|
158
|
+
return -1;
|
|
159
|
+
}
|
|
160
|
+
sInput = sInput.trim();
|
|
161
|
+
if (sInput.isEmpty()) {
|
|
162
|
+
return -1;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (sInput.contains(":")) {
|
|
166
|
+
String parts[] = sInput.split(":", 2);
|
|
167
|
+
int idx = findAirportIndex(parts[0], sNames, iSize);
|
|
168
|
+
if (idx != -1) return idx;
|
|
169
|
+
idx = findAirportIndex(parts[1], sNames, iSize);
|
|
170
|
+
if (idx != -1) return idx;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (sInput.contains("-")) {
|
|
174
|
+
String parts[] = sInput.split("-", 2);
|
|
175
|
+
int idx = findAirportIndex(parts[0], sNames, iSize);
|
|
176
|
+
if (idx != -1) return idx;
|
|
177
|
+
idx = findAirportIndex(parts[1], sNames, iSize);
|
|
178
|
+
if (idx != -1) return idx;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
try {
|
|
182
|
+
int idx = Integer.parseInt(sInput);
|
|
183
|
+
if (idx >= 0 && idx < iSize) {
|
|
184
|
+
return idx;
|
|
185
|
+
}
|
|
186
|
+
} catch (NumberFormatException ignored) {
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
for (int i = 0; i < iSize; i++) {
|
|
190
|
+
if (sNames[i].equalsIgnoreCase(sInput)) {
|
|
191
|
+
return i;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
for (int i = 0; i < iSize; i++) {
|
|
196
|
+
if (sInput.toLowerCase().contains(sNames[i].toLowerCase()) ||
|
|
197
|
+
sNames[i].toLowerCase().contains(sInput.toLowerCase())) {
|
|
198
|
+
return i;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return -1;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
@@ -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 - Flight"
|
|
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}")
|