@@ -127,135 +127,37 @@ run_test_case()`
127127
128128
129129 : `#include <bits/stdc++.h>
130- #include <filesystem>
131- #include <functional>
132- #include <iostream>
133- #include <sstream>
134- #include <string>
135- #include <vector>
136-
137130using namespace std;
138- namespace fs = std::filesystem;
139-
140- // Parse values from string
141- template <typename T>
142- T parseValue(const string& str) {
143- istringstream iss(str);
144- T value;
145- iss >> value;
146- return value;
147- }
148131
149- // Template specialization for string
150- template <>
151- string parseValue<string>(const string& str) {
152- // Remove quotes if present
153- if (str.size() >= 2 && str.front() == '"' && str.back() == '"') {
154- return str.substr(1, str.size() - 2);
155- }
156- return str;
157- }
132+ //write the code logic here
158133
159- // Parse arrays from string
160- template <typename T>
161- vector<T> parseArray(const string& str) {
162- vector<T> result;
163- string trimmedStr = str.substr(1, str.size() - 2); // Remove brackets
164- istringstream iss(trimmedStr);
165- string item;
166- while (getline(iss, item, ',')) {
167- result.push_back(parseValue<T>(item));
168- }
169- return result;
170- }
171134
172- // Function to detect data type from string
173- template <typename T>
174- T detectAndParseValue(const string& str) {
175- if (str.empty()) {
176- throw runtime_error("Empty input string");
177- }
178-
179- // Check if it's an array
180- if (str.front() == '[' && str.back() == ']') {
181- return parseArray<typename T::value_type>(str);
182- }
183-
184- // Check if it's a string (quoted)
185- if (str.front() == '"' && str.back() == '"') {
186- return parseValue<T>(str);
187- }
188-
189- // Otherwise treat as numeric or other type
190- return parseValue<T>(str);
191- }
135+ // Function to run the tes\ case
136+ void runTestCase(int n) {
137+ string filePath = "../test_cases/input_" + to_string(n) + ".txt";
138+ ifstream file(filePath);
192139
193- // Generic function to run test cases
194- template <typename T>
195- void run_test_case(int test_case_number, const function<void(const T&)>& solution_function) {
196- fs::path base_directory = fs::path(__FILE__).parent_path().parent_path();
197- fs::path file_path = base_directory / "test_cases" / ("input_" + to_string(test_case_number) + ".txt");
198-
199- try {
200- ifstream file(file_path);
201- if (!file.is_open()) {
202- throw runtime_error("File not found at " + file_path.string());
203- }
204-
205- string line;
206- vector<string> lines;
207- while (getline(file, line)) {
208- lines.push_back(line);
209- }
210-
211- if (lines.empty()) {
212- throw runtime_error("Invalid input format");
213- }
214-
215- // Parse input based on type T
216- T parsed_input = detectAndParseValue<T>(lines[0]);
217- solution_function(parsed_input);
218-
219- } catch (const exception& e) {
220- cerr << "Error: " << e.what() << endl;
140+ if (!file.is_open()) {
141+ cerr << "Error: File not found at " << filePath << ". Check the file path and try again." << endl;
142+ return;
221143 }
222- }
223144
224- // -------------------- SOLUTION TEMPLATE --------------------
225- // MODIFY THIS SECTION FOR YOUR SPECIFIC PROBLEM
145+ // if your input files has vector/array uncomment the below code
146+ // Read the array from the file
226147
227- void solution(const vector<string>& args) {
228- // Example parsing different types of inputs
229- // Modify according to your problem requirements
230-
231- // For array input: [1,2,3]
232- if (!args.empty() && args[0][0] == '[') {
233- auto arr = parseArray<int>(args[0]);
234-
235- }
236-
237- // For string input: "hello"
238- if (args.size() >= 2 && args[1][0] == '"') {
239- string str = args[1].substr(1, args[1].length() - 2);
240- }
241-
242- // For integer input: 42
243- if (args.size() >= 3) {
244- int num = parseValue<int>(args[2]);
245- }
246-
247- // YOUR SOLUTION LOGIC GOES HERE
248-
148+ // Call the function and print the result(pass all the varbles that you have read from the file)
149+ //<data type> result = <function_name>(<variables>);
249150
151+ cout << result << endl;
250152}
251153
252154int main() {
253- // Run single test case
254- run_test_case<vector<int>>(1, solution);
255-
256-
155+ // Example usage
156+ runTestCase(); // Adjust the number as needed for your actual test case
257157 return 0;
258- }` ;
158+ }
159+
160+ ` ;
259161
260162
261163
0 commit comments