#include using namespace std; string ltrim(const string &); string rtrim(const string &); vector split(const string &); enum struct InputMode { HackerRank, Linux }; InputMode inputMode = InputMode::Linux; using TestCasesMap = map>; TestCasesMap testCases = { // test values expected result // ---- ------------------ --------------- { "5", {"10 40 30 50 20", "14.1"}}, }; TestCasesMap::const_iterator testCase = testCases.find("5"); /* * Complete the 'stdDev' function below. * * The function accepts INTEGER_ARRAY arr as parameter. */ void stdDev(vector arr) { // Print your answers to 1 decimal place within this function double sum = accumulate(arr.begin(), arr.end(), 0); double mean = sum/arr.size(); double stdDeviation = 0; for (size_t i = 0; i < arr.size(); i++) { stdDeviation += pow(arr[i] - mean, 2); } stdDeviation = sqrt(stdDeviation/arr.size()); cout << "expected: " << get<1>(testCase->second) << " actual: " << fixed << setprecision(1) << stdDeviation << endl; cout << fixed << setprecision(1) << stdDeviation << endl; } int main() { string n_temp; if (inputMode == InputMode::HackerRank) getline(cin, n_temp); else n_temp = testCase->first; int n = stoi(ltrim(rtrim(n_temp))); string vals_temp_temp; if (inputMode == InputMode::HackerRank) getline(cin, vals_temp_temp); else vals_temp_temp = get<0>(testCase->second); vector vals_temp = split(rtrim(vals_temp_temp)); vector vals(n); for (int i = 0; i < n; i++) { int vals_item = stoi(vals_temp[i]); vals[i] = vals_item; } stdDev(vals); return 0; } std::function f_isspace = [](int x){ return isspace(x); }; string ltrim(const string &str) { string s(str); s.erase( s.begin(), find_if(s.begin(), s.end(), not_fn(f_isspace)) ); return s; } string rtrim(const string &str) { string s(str); s.erase( find_if(s.rbegin(), s.rend(), not_fn(f_isspace)).base(), s.end() ); return s; } vector split(const string &str) { vector tokens; string::size_type start = 0; string::size_type end = 0; while ((end = str.find(" ", start)) != string::npos) { tokens.push_back(str.substr(start, end - start)); start = end + 1; } tokens.push_back(str.substr(start)); return tokens; }