-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
278 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
solution | ||
*.dSYM | ||
python/main/__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Leetcode Problem | ||
|
||
## Usage | ||
|
||
Run program with an example | ||
|
||
``` | ||
bazel run cpp/main:solution < data/1.in | ||
``` | ||
|
||
Test program | ||
|
||
``` | ||
# Run all tests | ||
bazel test --test_output=all tests:solution_test | ||
bazel test --test_output=all --cache_test_results=no tests:solution_test | ||
# Run test with pecific test id | ||
bazel test --test_output=all tests:solution_test --test_arg=1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_binary") | ||
|
||
cc_binary( | ||
name = "solution", | ||
srcs = ["solution.cpp", "solution.h", "helpers.h"], | ||
visibility = ["//visibility:public"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <iostream> | ||
#include <stack> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
/* Temmplate class T should be a Nary Tree Node | ||
* See leetcode 589 (https://leetcode.com/problems/n-ary-tree-preorder-traversal/description/) | ||
* class Node { | ||
* public: | ||
* int val; | ||
* vector<Node*> children; | ||
* | ||
* Node() {} | ||
* | ||
* Node(int _val) { val = _val; } | ||
* | ||
* Node(int _val, vector<Node*> _children) { | ||
* val = _val; | ||
* children = _children; | ||
* } | ||
* }; | ||
*/ | ||
template <typename T> T* parse_nary_tree(vector<string> s) { | ||
T* fake = new T(); | ||
T* current = fake; | ||
queue<T*> q; | ||
for(string e: s){ | ||
if(e == "null"){ | ||
current = q.front(); | ||
q.pop(); | ||
} else { | ||
T* child = new T(stoi(e)); | ||
q.push(child); | ||
current->children.push_back(child); | ||
} | ||
} | ||
return fake->children[0]; | ||
} | ||
|
||
vector<string> parse_line(string line) { | ||
vector<string> v; | ||
string s; | ||
for (char c : line) { | ||
if (c == ' ') { | ||
v.push_back(s); | ||
s = ""; | ||
} else { | ||
s += c; | ||
} | ||
} | ||
v.push_back(s); | ||
return v; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "solution.h" | ||
#include "helpers.h" | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
#include <fstream> | ||
#include <filesystem> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
ios::sync_with_stdio(false); | ||
cin.tie(0); | ||
|
||
bool is_use_file = false; | ||
|
||
filesystem::path filepath = | ||
filesystem::current_path().parent_path().parent_path() / "data" / "1.in"; | ||
ifstream file(filepath); | ||
|
||
if (is_use_file) { | ||
cin.rdbuf(file.rdbuf()); // redirect cin to file | ||
} | ||
|
||
// get input | ||
int n; | ||
cin >> n; | ||
|
||
Solution solution; | ||
bool output = solution.isPalindrome(n); | ||
|
||
// print output | ||
cout << output << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <map> | ||
#include <set> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <vector> | ||
#include <stack> | ||
#include <queue> | ||
#include <climits> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool isPalindrome(int x) { | ||
if(x < 0) return false; | ||
string s = to_string(x); | ||
string s1 = to_string(x); | ||
reverse(s1.begin(), s1.end()); | ||
return s == s1; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test") | ||
|
||
cc_test( | ||
name = "solution_test", | ||
srcs = ["solution_test.cpp"], | ||
deps = [ | ||
"//problems/codeforcesAA/src/main:solution", | ||
"@com_google_googletest//:gtest_main", | ||
], | ||
data = ["data"] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include <cstdlib> | ||
#include <filesystem> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
using namespace std; | ||
|
||
std::string TEST_ID = ""; | ||
|
||
vector<string> split(string s, string delimiter) { | ||
size_t pos_start = 0, pos_end, delim_len = delimiter.length(); | ||
string token; | ||
vector<string> res; | ||
|
||
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) { | ||
token = s.substr(pos_start, pos_end - pos_start); | ||
pos_start = pos_end + delim_len; | ||
res.push_back(token); | ||
} | ||
|
||
res.push_back(s.substr(pos_start)); | ||
return res; | ||
} | ||
|
||
std::string GetHello(std::string_view in) { | ||
if (in.size() == 0) { | ||
return std::string("hello, word"); | ||
} else { | ||
return std::string("Hello, ") + in.data(); | ||
} | ||
} | ||
|
||
string ReadFile(const std::string &filename) { | ||
std::ifstream f(filename); | ||
string s((std::istreambuf_iterator<char>(f)), | ||
std::istreambuf_iterator<char>()); | ||
return s; | ||
} | ||
|
||
vector<string> RunProgram(string fileid) { | ||
vector<string> v; | ||
string program = "problems/codeforcesAA/src/main/solution"; | ||
string test_folder = "problems/codeforcesAA/tests/data/"; | ||
string command = program + " < " + test_folder + fileid + ".in > output.txt"; | ||
std::system(command.c_str()); | ||
string actual = ReadFile("output.txt"); | ||
string expectedOutputFile = test_folder + fileid + ".out"; | ||
string expected = ReadFile(expectedOutputFile); | ||
v.push_back(actual); | ||
v.push_back(expected); | ||
return v; | ||
} | ||
|
||
TEST(RunTest, AllTestCases) { | ||
if (TEST_ID != "") { | ||
std::cout << "TEST_ID = " << TEST_ID << std::endl; | ||
} else { | ||
std::cout << "RUN ALL TESTS" << std::endl; | ||
} | ||
|
||
std::string test_data_folder = "problems/codeforcesAA/tests/data"; | ||
for (const auto &entry : fs::directory_iterator(test_data_folder)) { | ||
string filename = split(entry.path(), "/").back(); | ||
vector<string> v = split(filename, "."); | ||
string fileid = v[0]; | ||
string extension = v[1]; | ||
if (extension != "in"){ | ||
continue; | ||
} | ||
if (TEST_ID != ""){ | ||
if (fileid != TEST_ID){ | ||
continue; | ||
} | ||
} | ||
// Run Test Case | ||
vector<string> output = RunProgram(fileid); | ||
string actual = output[0]; | ||
string expected = output[1]; | ||
string message = "❌ FAIL CASE: " + fileid; | ||
ASSERT_EQ(actual, expected) << message; | ||
} | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
if (argc == 2) { | ||
TEST_ID = argv[1]; | ||
} | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
121 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-121 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "Leetcode 78. Subsets", | ||
"link": "https://leetcode.com/problems/subsets/", | ||
"tags": [ | ||
"Array", | ||
"Backtracking", | ||
"Bit Manipulation" | ||
] | ||
} |