Skip to content

Commit 08da689

Browse files
authored
Update gray_code.cpp
1 parent 21d3875 commit 08da689

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

bit_manipulation/gray_code.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
#include <iostream>
33
#include <vector>
44

5-
using namespace std;
6-
75
/**
86
* Function to generate Gray code sequence for 'n' bits.
97
*
108
* @param n - The number of bits for which to generate Gray code.
119
* @return A vector of strings representing the Gray code sequence.
1210
*/
13-
vector<string> generateGrayCode(int n) {
11+
12+
std::vector<std::string> generateGrayCode(int n) {
13+
// Base case
1414
if (n == 1) {
15-
vector<string> grayCode;
15+
std::vector<std::string> grayCode;
1616
grayCode.push_back("0");
1717
grayCode.push_back("1");
1818
return grayCode;
1919
}
20-
vector<string> prevGrayCode = generateGrayCode(n - 1);
20+
std::vector<std::string> prevGrayCode = generateGrayCode(n - 1);
2121

22-
vector<string> grayCode;
22+
std::vector<std::string> grayCode;
2323
for (int i = 0; i < prevGrayCode.size(); i++) {
2424
grayCode.push_back("0" + prevGrayCode[i]);
2525
}
@@ -39,18 +39,19 @@ vector<string> generateGrayCode(int n) {
3939
*/
4040

4141
void testGrayCodeGeneration() {
42-
vector<pair<int, vector<string>>> testCases = {
42+
std::vector<std::pair<int, std::vector<std::string>>> testCases = {
4343
{1, {"0", "1"}},
4444
{2, {"00", "01", "11", "10"}},
4545
{3, {"000", "001", "011", "010", "110", "111", "101", "100"}},
46+
// Add more test cases as needed
4647
};
4748

4849
for (const auto& testCase : testCases) {
4950
int n = testCase.first;
50-
const vector<string>& expected = testCase.second;
51-
vector<string> result = generateGrayCode(n);
52-
assert(result == expected);
53-
cout << "Test for " << n << " bits passed!" << endl;
51+
const std::vector<std::string>& expected = testCase.second;
52+
std::vector<std::string> result = generateGrayCode(n);
53+
assert(result == expected);
54+
std::cout << "Test for " << n << " bits passed!" << std::endl;
5455
}
5556
}
5657

0 commit comments

Comments
 (0)