Skip to content

GH-111: Solve problem dsas_aog_week1_graph_decomposition1_1_finding_exit_from_maze #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ A data structure is a data organization, management, and storage format that is
<tr>
<td>Bitwise</td>
<td>
<a href="/concepts/python/bitwise.md"><code>py🐍</code></a>
<a href="/concepts/cpp/bitwise.md"><code>cpp🐀</code></a>
<a href="/concepts/python/bitwise.md"><code>py🐍</code></a>
</td>
</tr>
<tr>
Expand All @@ -53,7 +53,7 @@ A data structure is a data organization, management, and storage format that is
<tr>
<td>Linked List</td>
<td>
<a href="/concepts/python/linked-list.md"><code>py</code></a>
<a href="/concepts/python/linked-list.md"><code>py🐍</code></a>
<a href="/concepts/typescript/linked-list.md"><code>ts</code></a>
</td>
<tr>
Expand Down Expand Up @@ -168,10 +168,15 @@ A data structure is a data organization, management, and storage format that is

## 🔆 Collections

Events of competitive programming
**Competitive Programming Events**

* 🎄 Advent of Code ([2022](collections/advent-of-code-2022/))
* 🔰 Google Code Jam ([2022](collections/codejam-2022/))

**Courses & Specialization**

* [🍨 Data Structures and Algorithms Specialization](collections/datastructures-and-algorithms-specialization/)

* [🎃 Advent of Code 2022](collections/advent-of-code-2022/)
* [🔰 Google Code Jam 2022](collections/codejam-2022/)

## Contributors

Expand Down
2 changes: 1 addition & 1 deletion collections/advent-of-code-2022/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 🎃 Advent of Code 2022
# 🎄 Advent of Code 2022

> Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like. People use them as interview prep, company training, university coursework, practice problems, a speed contest, or to challenge each other.

Expand Down
6 changes: 2 additions & 4 deletions collections/codejam-2022/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# 🎃 Google Code Jam 2022
# 🔰 Google Code Jam 2022

* Website: https://codingcompetitions.withgoogle.com/codejam/archive/2022

## Problems

Qualification Round
### Qualification Round

<table>
<thead>
Expand Down
38 changes: 38 additions & 0 deletions collections/datastructures-and-algorithms-specialization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 🍨 Data Structures and Algorithms Specialization

> Master Algorithmic Programming Techniques. Advance your Software Engineering or Data Science Career by Learning Algorithms through Programming and Puzzle Solving. Ace coding interviews by implementing each algorithmic challenge in this Specialization. Apply the newly-learned algorithmic techniques to real-life problems, such as analyzing a huge social network or sequencing a genome of a deadly pathogen.

* Website: https://www.coursera.org/specializations/data-structures-algorithms
* Offered By: University of California San Diego

### [Algorithmic Toolbox](https://www.coursera.org/learn/data-structures?specialization=data-structures-algorithms)

### [Data Structures](https://www.coursera.org/learn/data-structures?specialization=data-structures-algorithms)

### [Algorithms on Graphs](https://www.coursera.org/learn/algorithms-on-graphs?specialization=data-structures-algorithms)

<table>
<thead>
<th></th>
<th>Statement</th>
<th>Code</th>
</thead>
<tbody>
<tr>
<td colspan="3" align="left"><b>Week 1: Graph Decomposition</b></td>
</tr>
<tr>
<td>1. Finding Exit From Maze</td>
<td><a href="../../problems/dsas_aog_week1_graph_decomposition1_1_finding_exit_from_maze/doc/week1_graph_decomposition1.pdf">Link</a></td>
<td>
<a href="../../problems/dsas_aog_week1_graph_decomposition1_1_finding_exit_from_maze/src/main/reachability.cpp"><code>cpp🐀</code></a>
</td>
</tr>
</tbody>
</table>

### [Algorithms on Strings](https://www.coursera.org/learn/algorithms-on-strings?specialization=data-structures-algorithms)

### [Advanced Algorithms and Complexity](https://www.coursera.org/learn/advanced-algorithms-and-complexity?specialization=data-structures-algorithms)

### [Genome Assembly Programming Challenge](https://www.coursera.org/learn/assembling-genomes?specialization=data-structures-algorithms)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solution
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DSAS Problem

## Usage

Run program with an example

```
bazel run src/main:reachability < tests/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
```
Binary file not shown.
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 = "reachability",
srcs = ["reachability.cpp"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include <queue>
#include <vector>

using std::pair;
using std::vector;

#define MAX_N 1000

using namespace std;

// connected components
int cc[MAX_N];
bool visited[MAX_N];

void dfs(vector<vector<int>> adj, int i, int cci) {
visited[i] = true;
cc[i] = cci;
for (auto j : adj[i]) {
if (!visited[j]) {
dfs(adj, j, cci);
}
}
}

int reach(vector<vector<int>> adj, int x, int y, int n) {
int cci = 1;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
dfs(adj, i, cci++);
}
}
return cc[x] == cc[y];
}

int main() {
size_t n, m;
std::cin >> n >> m;
vector<vector<int>> adj(n, vector<int>());
for (size_t i = 0; i < m; i++) {
int x, y;
std::cin >> x >> y;
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
int x, y;
std::cin >> x >> y;
std::cout << reach(adj, x - 1, y - 1, n);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>

using namespace std;

int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while(tt--){
cout << tt << '\n';
}
return 0;
}
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"]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4 4
1 2
3 2
4 3
1 4
1 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
3
2
1
0
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
4 2
1 2
3 2
1 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
9
8
7
6
5
4
3
2
1
0
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();
}
7 changes: 4 additions & 3 deletions tools/craft/craft.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ def task_rename_files(self):
print("Boilderplate to generate workspace for solving problem.\n")
print("Usage: python crape.py domain problem_id\n\n")
print("Examples:\n")
print("\tpython crapy.py codeforces 100A\n")
print("\tpython crapy.py aoc 2022day2\n")
print("\tpython crapy.py codejam 2022PunchedCards\n")
print("\tpython craft.py codeforces 100A")
print("\tpython craft.py aoc 2022day2")
print("\tpython craft.py codejam 2022PunchedCards")
print("\tpython craft.py dsas _aog_week1_graph_decomposition1_1_finding_exit_from_maze")
sys.exit(1)
domain = sys.argv[1]
problem_id = sys.argv[2]
Expand Down
1 change: 1 addition & 0 deletions tools/craft/templates/dsas/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
solution
19 changes: 19 additions & 0 deletions tools/craft/templates/dsas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DSAS Problem

## Usage

Run program with an example

```
bazel run src/main:solution < tests/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
```
7 changes: 7 additions & 0 deletions tools/craft/templates/dsas/src/main/BUILD
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"],
visibility = ["//visibility:public"],
)
Loading