2
2
#include < iostream>
3
3
#include < vector>
4
4
5
- using namespace std ;
6
-
7
5
/* *
8
6
* Function to generate Gray code sequence for 'n' bits.
9
7
*
10
8
* @param n - The number of bits for which to generate Gray code.
11
9
* @return A vector of strings representing the Gray code sequence.
12
10
*/
13
- vector<string> generateGrayCode (int n) {
11
+
12
+ std::vector<std::string> generateGrayCode (int n) {
13
+ // Base case
14
14
if (n == 1 ) {
15
- vector<string> grayCode;
15
+ std:: vector<std:: string> grayCode;
16
16
grayCode.push_back (" 0" );
17
17
grayCode.push_back (" 1" );
18
18
return grayCode;
19
19
}
20
- vector<string> prevGrayCode = generateGrayCode (n - 1 );
20
+ std:: vector<std:: string> prevGrayCode = generateGrayCode (n - 1 );
21
21
22
- vector<string> grayCode;
22
+ std:: vector<std:: string> grayCode;
23
23
for (int i = 0 ; i < prevGrayCode.size (); i++) {
24
24
grayCode.push_back (" 0" + prevGrayCode[i]);
25
25
}
@@ -39,18 +39,19 @@ vector<string> generateGrayCode(int n) {
39
39
*/
40
40
41
41
void testGrayCodeGeneration () {
42
- vector<pair<int , vector<string>>> testCases = {
42
+ std:: vector<std:: pair<int , std:: vector<std:: string>>> testCases = {
43
43
{1 , {" 0" , " 1" }},
44
44
{2 , {" 00" , " 01" , " 11" , " 10" }},
45
45
{3 , {" 000" , " 001" , " 011" , " 010" , " 110" , " 111" , " 101" , " 100" }},
46
+ // Add more test cases as needed
46
47
};
47
48
48
49
for (const auto & testCase : testCases) {
49
50
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;
54
55
}
55
56
}
56
57
0 commit comments