Skip to content

Commit dbccf79

Browse files
authored
GH-42: Solve codeforces 1762A (#89)
1 parent 3bf9f32 commit dbccf79

File tree

11 files changed

+212
-0
lines changed

11 files changed

+212
-0
lines changed

problems/codeforces1762A/.gitginore

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

problems/codeforces1762A/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run src/main:solution < tests/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/codeforces1762A/run_tests.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for f in $(cd tests/; ls *.in); do
2+
test_id=$(echo $f | cut -d'.' -f1)
3+
./solution < tests/$test_id.in | diff -aur tests/$test_id.out -
4+
done
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"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <cmath>
2+
#include <vector>
3+
#include <map>
4+
#include <algorithm>
5+
#include <string>
6+
#include <iostream>
7+
#include <climits>
8+
9+
using namespace std;
10+
11+
int const MAX_N = 50;
12+
13+
int solve(int* a, int n){
14+
int s;
15+
int m = INT_MAX;
16+
int k;
17+
18+
for(int i=0; i<n; i++){
19+
s += a[i];
20+
k = a[i];
21+
bool eveness = (k % 2 == 0);
22+
int mk = 0;
23+
bool result = eveness;
24+
while(result == eveness){
25+
k /= 2;
26+
result = (k % 2 == 0);
27+
mk++;
28+
}
29+
m = min(m, mk);
30+
}
31+
32+
if(s % 2 == 0){
33+
return 0;
34+
} else {
35+
return m;
36+
}
37+
}
38+
int main(){
39+
ios::sync_with_stdio(false);
40+
cin.tie(0);
41+
int tt;
42+
int n;
43+
int a[MAX_N];
44+
cin >> tt;
45+
while(tt--){
46+
cin >> n;
47+
for(int i=0; i<n; i++){
48+
cin >> a[i];
49+
}
50+
cout << solve(a, n) << endl;
51+
}
52+
return 0;
53+
}

problems/codeforces1762A/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/codeforces1762A/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
4
2+
4
3+
1 1 1 1
4+
2
5+
7 4
6+
3
7+
1 2 4
8+
1
9+
15
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
2
3+
1
4+
4
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2
2+
26
3+
635760 101863 552527 470921 686632 445638 649487 270124 388622 737558 461807 173225 68297 246962 330895 134483 470887 76503 98922 136859 883721 218428 101777 741178 16378 412054
4+
40
5+
524287 524288 524288 786432 327680 524288 917504 524287 917504 983039 393215 524288 786431 786432 524287 131071 196608 786432 131072 196608 786432 655359 720896 983040 524288 262144 65535 589824 655360 262144 524288 983039 524288 524288 393215 655360 786432 524287 262143 524288
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0
2+
16
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/codeforces1762A/src/main/solution";
50+
string test_folder = "problems/codeforces1762A/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/codeforces1762A/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+
}

0 commit comments

Comments
 (0)