Skip to content

Commit 012dc82

Browse files
Merge branch 'master' into myContribution
2 parents e89b5bf + 47badd9 commit 012dc82

File tree

4 files changed

+116
-57
lines changed

4 files changed

+116
-57
lines changed

bit_manipulation/count_of_set_bits.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@ namespace count_of_set_bits {
3636
* @returns total number of set-bits in the binary representation of number `n`
3737
*/
3838
std::uint64_t countSetBits(
39-
std ::int64_t n) { // int64_t is preferred over int so that
39+
std ::uint64_t n) { // uint64_t is preferred over int so that
4040
// no Overflow can be there.
41+
//It's preferred over int64_t because it Guarantees that inputs are always non-negative,
42+
//which matches the algorithmic problem statement.
43+
//set bit counting is conceptually defined only for non-negative numbers.
44+
//Provides a type Safety: Using an unsigned type helps prevent accidental negative values,
45+
46+
std::uint64_t count = 0; // "count" variable is used to count number of set-bits('1')
47+
// in binary representation of number 'n'
48+
//Count is uint64_t because it Prevents theoretical overflow if someone passes very large integers.
49+
// Behavior stays the same for all normal inputs.
50+
// Safer for edge cases.
4151

42-
int count = 0; // "count" variable is used to count number of set-bits('1')
43-
// in binary representation of number 'n'
4452
while (n != 0) {
4553
++count;
4654
n = (n & (n - 1));

math/number_of_positive_divisors.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
**/
2424

2525
#include <cassert>
26-
#include <iostream>
2726

2827
/**
2928
* Function to compute the number of positive divisors.
@@ -80,13 +79,5 @@ void tests() {
8079
*/
8180
int main() {
8281
tests();
83-
int n;
84-
std::cin >> n;
85-
if (n == 0) {
86-
std::cout << "All non-zero numbers are divisors of 0 !" << std::endl;
87-
} else {
88-
std::cout << "Number of positive divisors is : ";
89-
std::cout << number_of_positive_divisors(n) << std::endl;
90-
}
9182
return 0;
9283
}

others/postfix_evaluation.cpp

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
* When the expression is ended, the number in the stack is the final answer
1313
*/
1414
#include <algorithm> // for all_of
15-
#include <array> // for array
1615
#include <cassert> // for assert
1716
#include <iostream> // for io operations
1817
#include <stack> // for std::stack
1918
#include <string> // for stof
19+
#include <vector> // for std::vector
2020

2121
/**
2222
* @namespace others
@@ -77,37 +77,43 @@ void evaluate(float a, float b, const std::string &operation,
7777
}
7878
}
7979

80+
namespace {
81+
float remove_from_stack(std::stack<float> &stack) {
82+
if (stack.empty()) {
83+
throw std::invalid_argument("Not enough operands");
84+
}
85+
const auto res = stack.top();
86+
stack.pop();
87+
return res;
88+
}
89+
} // namespace
90+
8091
/**
8192
* @brief Postfix Evaluation algorithm to compute the value from given input
8293
* array
83-
* @tparam N number of array size
84-
* @param input Array of characters consisting of numbers and operations
94+
* @param input vector of strings consisting of numbers and operations
8595
* @returns stack[stackTop] returns the top value from the stack
8696
*/
87-
template <std::size_t N>
88-
float postfix_evaluation(std::array<std::string, N> input) {
97+
float postfix_evaluation(const std::vector<std::string> &input) {
8998
std::stack<float> stack;
90-
int j = 0;
9199

92-
while (j < N) {
93-
std::string scan = input[j];
100+
for (const auto &scan : input) {
94101
if (is_number(scan)) {
95102
stack.push(std::stof(scan));
96103

97104
} else {
98-
const float op2 = stack.top();
99-
stack.pop();
100-
const float op1 = stack.top();
101-
stack.pop();
105+
const auto op2 = remove_from_stack(stack);
106+
const auto op1 = remove_from_stack(stack);
102107

103108
evaluate(op1, op2, scan, stack);
104109
}
105-
j++;
106110
}
107111

108-
std::cout << stack.top() << "\n";
109-
110-
return stack.top();
112+
const auto res = remove_from_stack(stack);
113+
if (!stack.empty()) {
114+
throw std::invalid_argument("Too many operands");
115+
}
116+
return res;
111117
}
112118
} // namespace postfix_expression
113119
} // namespace others
@@ -118,7 +124,7 @@ float postfix_evaluation(std::array<std::string, N> input) {
118124
* @returns none
119125
*/
120126
static void test_function_1() {
121-
std::array<std::string, 7> input = {"2", "3", "1", "*", "+", "9", "-"};
127+
std::vector<std::string> input = {"2", "3", "1", "*", "+", "9", "-"};
122128

123129
float answer = others::postfix_expression::postfix_evaluation(input);
124130

@@ -131,15 +137,15 @@ static void test_function_1() {
131137
* @returns none
132138
*/
133139
static void test_function_2() {
134-
std::array<std::string, 9> input = {"100", "200", "+", "2", "/",
135-
"5", "*", "7", "+"};
140+
std::vector<std::string> input = {"100", "200", "+", "2", "/",
141+
"5", "*", "7", "+"};
136142
float answer = others::postfix_expression::postfix_evaluation(input);
137143

138144
assert(answer == 757);
139145
}
140146

141147
static void test_function_3() {
142-
std::array<std::string, 43> input = {
148+
std::vector<std::string> input = {
143149
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
144150
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1",
145151
"+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
@@ -149,6 +155,46 @@ static void test_function_3() {
149155
assert(answer == 22);
150156
}
151157

158+
static void test_single_input() {
159+
std::vector<std::string> input = {"1"};
160+
float answer = others::postfix_expression::postfix_evaluation(input);
161+
162+
assert(answer == 1);
163+
}
164+
165+
static void test_not_enough_operands() {
166+
std::vector<std::string> input = {"+"};
167+
bool throws = false;
168+
try {
169+
others::postfix_expression::postfix_evaluation(input);
170+
} catch (std::invalid_argument &) {
171+
throws = true;
172+
}
173+
assert(throws);
174+
}
175+
176+
static void test_not_enough_operands_empty_input() {
177+
std::vector<std::string> input = {};
178+
bool throws = false;
179+
try {
180+
others::postfix_expression::postfix_evaluation(input);
181+
} catch (std::invalid_argument &) {
182+
throws = true;
183+
}
184+
assert(throws);
185+
}
186+
187+
static void test_too_many_operands() {
188+
std::vector<std::string> input = {"1", "2"};
189+
bool throws = false;
190+
try {
191+
others::postfix_expression::postfix_evaluation(input);
192+
} catch (std::invalid_argument &) {
193+
throws = true;
194+
}
195+
assert(throws);
196+
}
197+
152198
/**
153199
* @brief Main function
154200
* @returns 0 on exit
@@ -157,6 +203,10 @@ int main() {
157203
test_function_1();
158204
test_function_2();
159205
test_function_3();
206+
test_single_input();
207+
test_not_enough_operands();
208+
test_not_enough_operands_empty_input();
209+
test_too_many_operands();
160210

161211
std::cout << "\nTest implementations passed!\n";
162212

scripts/file_linter.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,44 @@
66
with open("git_diff.txt") as in_file:
77
modified_files = sorted(in_file.read().splitlines())
88
print("{} files were modified.".format(len(modified_files)))
9-
9+
1010
cpp_exts = tuple(".c .c++ .cc .cpp .cu .cuh .cxx .h .h++ .hh .hpp .hxx".split())
1111
cpp_files = [file for file in modified_files if file.lower().endswith(cpp_exts)]
1212
print(f"{len(cpp_files)} C++ files were modified.")
1313
if not cpp_files:
1414
sys.exit(0)
1515

16-
subprocess.run(["clang-tidy", "--fix", "-p=build", "--extra-arg=-std=c++11", *cpp_files, "--"],
17-
check=True, text=True, stderr=subprocess.STDOUT)
18-
19-
subprocess.run(["clang-format", "-i", "-style=file", *cpp_files],
20-
check=True, text=True, stderr=subprocess.STDOUT)
21-
22-
upper_files = [file for file in cpp_files if file != file.lower()]
23-
if upper_files:
24-
print(f"{len(upper_files)} files contain uppercase characters:")
25-
print("\n".join(upper_files) + "\n")
26-
27-
space_files = [file for file in cpp_files if " " in file or "-" in file]
28-
if space_files:
29-
print(f"{len(space_files)} files contain space or dash characters:")
30-
print("\n".join(space_files) + "\n")
31-
32-
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
33-
if nodir_files:
34-
print(f"{len(nodir_files)} files are not in one and only one directory:")
35-
print("\n".join(nodir_files) + "\n")
36-
37-
bad_files = len(upper_files + space_files + nodir_files)
38-
if bad_files:
39-
sys.exit(bad_files)
16+
subprocess.run(
17+
[
18+
"clang-tidy",
19+
"--fix",
20+
"-p=build",
21+
"--extra-arg=-std=c++11",
22+
*cpp_files,
23+
"--",
24+
],
25+
check=True,
26+
text=True,
27+
stderr=subprocess.STDOUT,
28+
)
29+
subprocess.run(
30+
["clang-format", "-i", "-style=file", *cpp_files],
31+
check=True,
32+
text=True,
33+
stderr=subprocess.STDOUT,
34+
)
35+
upper_files = [file for file in cpp_files if file != file.lower()]
36+
if upper_files:
37+
print(f"{len(upper_files)} files contain uppercase characters:")
38+
print("\n".join(upper_files) + "\n")
39+
space_files = [file for file in cpp_files if " " in file or "-" in file]
40+
if space_files:
41+
print(f"{len(space_files)} files contain space or dash characters:")
42+
print("\n".join(space_files) + "\n")
43+
nodir_files = [file for file in cpp_files if file.count(os.sep) != 1]
44+
if nodir_files:
45+
print(f"{len(nodir_files)} files are not in one and only one directory:")
46+
print("\n".join(nodir_files) + "\n")
47+
bad_files = len(upper_files + space_files + nodir_files)
48+
if bad_files:
49+
sys.exit(bad_files)

0 commit comments

Comments
 (0)