From a9c31157c067aaa002669ee74424d15b985dd70e Mon Sep 17 00:00:00 2001 From: Pawan Date: Fri, 31 Oct 2025 08:04:45 +0000 Subject: [PATCH] fix: harden electronics shop io parsing --- .../Electronics_Shop/Electronics_Shop.cpp | 169 ++++++++++++------ 1 file changed, 112 insertions(+), 57 deletions(-) diff --git a/code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.cpp b/code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.cpp index 2c9f18620e..96005393c2 100644 --- a/code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.cpp +++ b/code/online_challenges/src/hackerrank/Electronics_Shop/Electronics_Shop.cpp @@ -8,62 +8,128 @@ vector split_string(string); /* * Complete the getMoneySpent function below. */ -int getMoneySpent(vector keyboards, vector drives, int b) { - int i,sum=0,j,k=0,ans=-1; - sort(keyboards.begin(),keyboards.end()); - sort(drives.begin(),drives.end()); - for(i=0;i keyboards, vector drives, int b) +{ + int i, sum = 0, j, k = 0, ans = -1; + sort(keyboards.begin(), keyboards.end()); + sort(drives.begin(), drives.end()); + for (i = 0; i < keyboards.size(); i++) { - sum=keyboards[i]+drives[j]; - if(sum<=b&&sum>ans) - ans=sum; - - } + for (j = 0; j < drives.size(); j++) + { + sum = keyboards[i] + drives[j]; + if (sum <= b && sum > ans) + ans = sum; + } } return ans; } int main() { - ofstream fout(getenv("OUTPUT_PATH")); + ofstream fout; + ostream *out_stream = nullptr; + if (const char *output_path = getenv("OUTPUT_PATH")) + { + fout.open(output_path); + if (!fout) + { + cerr << "Failed to open OUTPUT_PATH for writing" << endl; + return 1; + } + out_stream = &fout; + } + else + { + out_stream = &cout; + } string bnm_temp; - getline(cin, bnm_temp); + if (!getline(cin, bnm_temp)) + { + (*out_stream) << -1 << "\n"; + if (fout.is_open()) + fout.close(); + return 0; + } vector bnm = split_string(bnm_temp); - int b = stoi(bnm[0]); - - int n = stoi(bnm[1]); + if (bnm.size() < 3) + { + // malformed first line: cannot parse budget, n, m + (*out_stream) << -1 << "\n"; + if (fout.is_open()) + fout.close(); + return 0; + } - int m = stoi(bnm[2]); + int b = 0, n = 0, m = 0; + try + { + b = stoi(bnm[0]); + n = stoi(bnm[1]); + m = stoi(bnm[2]); + } + catch (...) + { + // malformed numbers + (*out_stream) << -1 << "\n"; + if (fout.is_open()) + fout.close(); + return 0; + } string keyboards_temp_temp; - getline(cin, keyboards_temp_temp); + if (!getline(cin, keyboards_temp_temp)) + { + (*out_stream) << -1 << "\n"; + if (fout.is_open()) + fout.close(); + return 0; + } vector keyboards_temp = split_string(keyboards_temp_temp); - vector keyboards(n); - - for (int keyboards_itr = 0; keyboards_itr < n; keyboards_itr++) { - int keyboards_item = stoi(keyboards_temp[keyboards_itr]); - - keyboards[keyboards_itr] = keyboards_item; + // safely parse the keyboard prices; ignore malformed tokens + vector keyboards; + keyboards.reserve(keyboards_temp.size()); + for (const string &tok : keyboards_temp) + { + try + { + keyboards.push_back(stoi(tok)); + } + catch (...) + { + // skip malformed token + } } string drives_temp_temp; - getline(cin, drives_temp_temp); + if (!getline(cin, drives_temp_temp)) + { + (*out_stream) << -1 << "\n"; + if (fout.is_open()) + fout.close(); + return 0; + } vector drives_temp = split_string(drives_temp_temp); - vector drives(m); - - for (int drives_itr = 0; drives_itr < m; drives_itr++) { - int drives_item = stoi(drives_temp[drives_itr]); - - drives[drives_itr] = drives_item; + // safely parse the drive prices; ignore malformed tokens + vector drives; + drives.reserve(drives_temp.size()); + for (const string &tok : drives_temp) + { + try + { + drives.push_back(stoi(tok)); + } + catch (...) + { + // skip malformed token + } } /* @@ -72,38 +138,27 @@ int main() int moneySpent = getMoneySpent(keyboards, drives, b); - fout << moneySpent << "\n"; + (*out_stream) << moneySpent << "\n"; - fout.close(); + if (fout.is_open()) + fout.close(); return 0; } -vector split_string(string input_string) { - string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) { - return x == y and x == ' '; - }); - - input_string.erase(new_end, input_string.end()); - - while (input_string[input_string.length() - 1] == ' ') { - input_string.pop_back(); - } - +vector split_string(string input_string) +{ + // Use istringstream to safely split on whitespace. This handles empty strings + // and any amount of whitespace between tokens without manual indexing. vector splits; - char delimiter = ' '; - - size_t i = 0; - size_t pos = input_string.find(delimiter); + if (input_string.empty()) + return splits; - while (pos != string::npos) { - splits.push_back(input_string.substr(i, pos - i)); - - i = pos + 1; - pos = input_string.find(delimiter, i); + istringstream iss(input_string); + string token; + while (iss >> token) + { + splits.push_back(token); } - - splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1)); - return splits; }