Skip to content

Commit ea1544d

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank]: Warmup: Simple Array Sum. Static check issue solved.
-> src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp:24:19: style: Consider using std::accumulate algorithm instead of a raw loop. [useStlAlgorithm] accum += i; ^
1 parent 9f35a59 commit ea1544d

File tree

3 files changed

+4
-18
lines changed

3 files changed

+4
-18
lines changed

src/lib/exercises/include/exercises/hackerrank/warmup/simple_array_sum.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <iostream>
21
#include <vector>
32

43
#pragma once

src/lib/exercises/src/hackerrank/warmup/simple_array_sum.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@
44
* @link Problem definition [[docs/hackerrank/warmup/simple_array_sum.md]]
55
*/
66

7-
#include <iostream>
8-
#include <cmath>
7+
#include <numeric>
98
#include <vector>
109

11-
using namespace std;
12-
13-
string ltrim(const string &);
14-
string rtrim(const string &);
15-
vector<string> split(const string &);
16-
17-
1810
namespace hackerrank::warmup {
1911

2012
int simpleArraySum(const std::vector<int>& ar) {
21-
int accum = 0;
22-
23-
for(const int i : ar) {
24-
accum += i;
25-
}
26-
return accum;
13+
const int INIT_VALUE = 0;
14+
return std::accumulate(ar.begin(), ar.end(), INIT_VALUE);
2715
}
2816

2917
}

src/tests/unit/lib/hackerrank/warmup/simple_array_sum.test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#include <catch2/catch_test_macros.hpp>
22

33
#include <exercises/hackerrank/warmup/simple_array_sum.hpp>
4-
#include <iostream>
54
#include <vector>
6-
75
#include <filesystem>
86
#include <fstream>
97
#include <nlohmann/json.hpp>
8+
109
using json = nlohmann::json;
1110

1211
TEST_CASE("simpleArraySum", "[warmup]")

0 commit comments

Comments
 (0)