Skip to content

Commit 7763659

Browse files
authored
GH-309: Solve leetcode 9 (#310)
1 parent 74d0804 commit 7763659

File tree

17 files changed

+278
-1
lines changed

17 files changed

+278
-1
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<td>6</td>
1818
<td>7</td>
1919
<td>8</td>
20-
<td>9</td>
20+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode9'>9</a></td>
2121
<td>10</td>
2222
<tr>
2323
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode11'>11</a></td>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ problems:
22
- name: 1
33
languages: cpp
44
level: easy
5+
- name: 9
6+
languages: cpp
7+
level: easy
58
- name: 11
69
languages: cpp
710
level: medium

problems/leetcode9/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
solution
2+
*.dSYM
3+
python/main/__pycache__

problems/leetcode9/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```

problems/leetcode9/cpp/main/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp", "solution.h", "helpers.h"],
6+
visibility = ["//visibility:public"],
7+
)

problems/leetcode9/cpp/main/helpers.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <iostream>
6+
#include <stack>
7+
#include <queue>
8+
9+
using namespace std;
10+
11+
/* Temmplate class T should be a Nary Tree Node
12+
* See leetcode 589 (https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/)
13+
* class Node {
14+
* public:
15+
* int val;
16+
* vector<Node*> children;
17+
*
18+
* Node() {}
19+
*
20+
* Node(int _val) { val = _val; }
21+
*
22+
* Node(int _val, vector<Node*> _children) {
23+
* val = _val;
24+
* children = _children;
25+
* }
26+
* };
27+
*/
28+
template <typename T> T* parse_nary_tree(vector<string> s) {
29+
T* fake = new T();
30+
T* current = fake;
31+
queue<T*> q;
32+
for(string e: s){
33+
if(e == "null"){
34+
current = q.front();
35+
q.pop();
36+
} else {
37+
T* child = new T(stoi(e));
38+
q.push(child);
39+
current->children.push_back(child);
40+
}
41+
}
42+
return fake->children[0];
43+
}
44+
45+
vector<string> parse_line(string line) {
46+
vector<string> v;
47+
string s;
48+
for (char c : line) {
49+
if (c == ' ') {
50+
v.push_back(s);
51+
s = "";
52+
} else {
53+
s += c;
54+
}
55+
}
56+
v.push_back(s);
57+
return v;
58+
}
59+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "solution.h"
2+
#include "helpers.h"
3+
4+
#include <algorithm>
5+
#include <cmath>
6+
#include <iostream>
7+
#include <map>
8+
#include <string>
9+
#include <vector>
10+
#include <fstream>
11+
#include <filesystem>
12+
13+
using namespace std;
14+
15+
int main() {
16+
ios::sync_with_stdio(false);
17+
cin.tie(0);
18+
19+
bool is_use_file = false;
20+
21+
filesystem::path filepath =
22+
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
23+
ifstream file(filepath);
24+
25+
if (is_use_file) {
26+
cin.rdbuf(file.rdbuf()); // redirect cin to file
27+
}
28+
29+
// get input
30+
int n;
31+
cin >> n;
32+
33+
Solution solution;
34+
bool output = solution.isPalindrome(n);
35+
36+
// print output
37+
cout << output << endl;
38+
39+
return 0;
40+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <map>
4+
#include <set>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <vector>
8+
#include <stack>
9+
#include <queue>
10+
#include <climits>
11+
12+
using namespace std;
13+
14+
class Solution {
15+
public:
16+
bool isPalindrome(int x) {
17+
if(x < 0) return false;
18+
string s = to_string(x);
19+
string s1 = to_string(x);
20+
reverse(s1.begin(), s1.end());
21+
return s == s1;
22+
}
23+
};

problems/leetcode9/cpp/tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforcesAA/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include "gtest/gtest.h"
10+
11+
namespace fs = std::filesystem;
12+
13+
using namespace std;
14+
15+
std::string TEST_ID = "";
16+
17+
vector<string> split(string s, string delimiter) {
18+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
19+
string token;
20+
vector<string> res;
21+
22+
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
23+
token = s.substr(pos_start, pos_end - pos_start);
24+
pos_start = pos_end + delim_len;
25+
res.push_back(token);
26+
}
27+
28+
res.push_back(s.substr(pos_start));
29+
return res;
30+
}
31+
32+
std::string GetHello(std::string_view in) {
33+
if (in.size() == 0) {
34+
return std::string("hello, word");
35+
} else {
36+
return std::string("Hello, ") + in.data();
37+
}
38+
}
39+
40+
string ReadFile(const std::string &filename) {
41+
std::ifstream f(filename);
42+
string s((std::istreambuf_iterator<char>(f)),
43+
std::istreambuf_iterator<char>());
44+
return s;
45+
}
46+
47+
vector<string> RunProgram(string fileid) {
48+
vector<string> v;
49+
string program = "problems/codeforcesAA/src/main/solution";
50+
string test_folder = "problems/codeforcesAA/tests/data/";
51+
string command = program + " < " + test_folder + fileid + ".in > output.txt";
52+
std::system(command.c_str());
53+
string actual = ReadFile("output.txt");
54+
string expectedOutputFile = test_folder + fileid + ".out";
55+
string expected = ReadFile(expectedOutputFile);
56+
v.push_back(actual);
57+
v.push_back(expected);
58+
return v;
59+
}
60+
61+
TEST(RunTest, AllTestCases) {
62+
if (TEST_ID != "") {
63+
std::cout << "TEST_ID = " << TEST_ID << std::endl;
64+
} else {
65+
std::cout << "RUN ALL TESTS" << std::endl;
66+
}
67+
68+
std::string test_data_folder = "problems/codeforcesAA/tests/data";
69+
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
70+
string filename = split(entry.path(), "/").back();
71+
vector<string> v = split(filename, ".");
72+
string fileid = v[0];
73+
string extension = v[1];
74+
if (extension != "in"){
75+
continue;
76+
}
77+
if (TEST_ID != ""){
78+
if (fileid != TEST_ID){
79+
continue;
80+
}
81+
}
82+
// Run Test Case
83+
vector<string> output = RunProgram(fileid);
84+
string actual = output[0];
85+
string expected = output[1];
86+
string message = "❌ FAIL CASE: " + fileid;
87+
ASSERT_EQ(actual, expected) << message;
88+
}
89+
}
90+
91+
int main(int argc, char **argv) {
92+
::testing::InitGoogleTest(&argc, argv);
93+
if (argc == 2) {
94+
TEST_ID = argv[1];
95+
}
96+
return RUN_ALL_TESTS();
97+
}

problems/leetcode9/data/1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
121

problems/leetcode9/data/1.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

problems/leetcode9/data/2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-121

problems/leetcode9/data/2.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

problems/leetcode9/data/3.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10

problems/leetcode9/data/3.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

problems/leetcode9/problem_infos.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Leetcode 78. Subsets",
3+
"link": "https://leetcode.com/problems/subsets/",
4+
"tags": [
5+
"Array",
6+
"Backtracking",
7+
"Bit Manipulation"
8+
]
9+
}

0 commit comments

Comments
 (0)