Skip to content

Commit da4de9f

Browse files
committed
WIP
1 parent 3a857c6 commit da4de9f

File tree

12 files changed

+198
-171
lines changed

12 files changed

+198
-171
lines changed

.clang-format

Lines changed: 45 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,51 @@
1-
---
2-
Language: Cpp
3-
AccessModifierOffset: -3
4-
AlignAfterOpenBracket: Align
5-
AlignConsecutiveMacros: true
6-
AlignConsecutiveAssignments: false
7-
AlignConsecutiveDeclarations: false
8-
AlignEscapedNewlines: Left
9-
AlignOperands: true
10-
AlignTrailingComments: true
11-
AllowAllArgumentsOnNextLine: true
12-
AllowAllConstructorInitializersOnNextLine: true
13-
AllowAllParametersOfDeclarationOnNextLine: true
14-
AllowShortBlocksOnASingleLine: Never
15-
AllowShortCaseLabelsOnASingleLine: false
16-
AllowShortFunctionsOnASingleLine: All
17-
AllowShortLambdasOnASingleLine: All
18-
AllowShortIfStatementsOnASingleLine: Never
19-
AllowShortLoopsOnASingleLine: true
20-
AlwaysBreakAfterDefinitionReturnType: None
21-
AlwaysBreakAfterReturnType: None
22-
AlwaysBreakBeforeMultilineStrings: true
23-
AlwaysBreakTemplateDeclarations: Yes
24-
BinPackArguments: true
25-
BinPackParameters: true
1+
# .clang-format
2+
BasedOnStyle: Google # Use Google C++ style guide as the base
3+
IndentWidth: 4 # Set the number of spaces per indentation level
4+
TabWidth: 4 # Define tab width
5+
UseTab: Never # Always use spaces instead of tabs
6+
ColumnLimit: 100 # Maximum characters per line
7+
AllowShortIfStatementsOnASingleLine: false # Always put 'if' statements on separate lines
8+
AllowShortFunctionsOnASingleLine: InlineOnly # Only inline short functions on a single line
9+
BreakBeforeBraces: Allman # Use Allman style for braces (open braces on a new line)
10+
SpaceBeforeParens: ControlStatements # Add space before parentheses in control statements
11+
AlignConsecutiveAssignments: true # Align variable assignments for readability
12+
AlignConsecutiveDeclarations: true # Align declarations for consistency
13+
SortIncludes: true # Sort `#include` statements
14+
PointerAlignment: Left # Place the '*' next to the type (e.g., `int* ptr` instead of `int *ptr`)
15+
Standard: Cpp11 # Use C++11 as the standard
16+
17+
# Customize comments styling
18+
CommentPragmas: '^ IWYU pragma:' # Ignore 'Include What You Use' pragmas
19+
20+
# Customize brace wrapping
2621
BraceWrapping:
27-
AfterCaseLabel: false
28-
AfterClass: false
29-
AfterControlStatement: false
30-
AfterEnum: false
31-
AfterFunction: false
32-
AfterNamespace: false
33-
AfterObjCDeclaration: false
34-
AfterStruct: false
35-
AfterUnion: false
36-
AfterExternBlock: false
37-
BeforeCatch: false
38-
BeforeElse: false
39-
IndentBraces: false
22+
AfterClass: true
23+
AfterControlStatement: true
24+
AfterEnum: true
25+
AfterFunction: true
26+
AfterNamespace: true
27+
AfterStruct: true
4028
SplitEmptyFunction: true
4129
SplitEmptyRecord: true
4230
SplitEmptyNamespace: true
43-
BreakBeforeBinaryOperators: None
44-
BreakBeforeBraces: Attach
45-
BreakBeforeInheritanceComma: false
46-
BreakInheritanceList: BeforeColon
47-
BreakBeforeTernaryOperators: true
48-
BreakConstructorInitializersBeforeComma: false
49-
BreakConstructorInitializers: BeforeColon
50-
BreakAfterJavaFieldAnnotations: false
51-
BreakStringLiterals: true
52-
ColumnLimit: 80
53-
CommentPragmas: '^ IWYU pragma:'
54-
CompactNamespaces: false
55-
ConstructorInitializerAllOnOneLineOrOnePerLine: true
31+
32+
# Make constructor initializer lists more readable
5633
ConstructorInitializerIndentWidth: 4
57-
ContinuationIndentWidth: 4
58-
Cpp11BracedListStyle: true
59-
DeriveLineEnding: true
60-
DerivePointerAlignment: true
61-
DisableFormat: false
62-
ExperimentalAutoDetectBinPacking: false
63-
FixNamespaceComments: true
64-
ForEachMacros:
65-
- foreach
66-
- Q_FOREACH
67-
- BOOST_FOREACH
68-
IncludeBlocks: Regroup
69-
IncludeCategories:
70-
- Regex: '^<ext/.*\.h>'
71-
Priority: 2
72-
SortPriority: 0
73-
- Regex: '^<.*\.h>'
74-
Priority: 1
75-
SortPriority: 0
76-
- Regex: '^<.*'
77-
Priority: 2
78-
SortPriority: 0
79-
- Regex: '.*'
80-
Priority: 3
81-
SortPriority: 0
82-
IncludeIsMainRegex: '([-_](test|unittest))?$'
83-
IncludeIsMainSourceRegex: ''
84-
IndentCaseLabels: true
85-
IndentGotoLabels: true
86-
IndentPPDirectives: None
87-
IndentWidth: 4
88-
IndentWrappedFunctionNames: false
89-
JavaScriptQuotes: Leave
90-
JavaScriptWrapImports: true
91-
KeepEmptyLinesAtTheStartOfBlocks: false
92-
MacroBlockBegin: ''
93-
MacroBlockEnd: ''
94-
MaxEmptyLinesToKeep: 1
95-
NamespaceIndentation: None
96-
ObjCBinPackProtocolList: Never
97-
ObjCBlockIndentWidth: 2
98-
ObjCSpaceAfterProperty: false
99-
ObjCSpaceBeforeProtocolList: true
100-
PenaltyBreakAssignment: 2
101-
PenaltyBreakBeforeFirstCallParameter: 1
102-
PenaltyBreakComment: 300
103-
PenaltyBreakFirstLessLess: 120
104-
PenaltyBreakString: 1000
105-
PenaltyBreakTemplateDeclaration: 10
106-
PenaltyExcessCharacter: 1000000
107-
PenaltyReturnTypeOnItsOwnLine: 200
108-
PointerAlignment: Left
109-
RawStringFormats:
110-
- Language: Cpp
111-
Delimiters:
112-
- cc
113-
- CC
114-
- cpp
115-
- Cpp
116-
- CPP
117-
- 'c++'
118-
- 'C++'
119-
CanonicalDelimiter: ''
120-
BasedOnStyle: google
121-
- Language: TextProto
122-
Delimiters:
123-
- pb
124-
- PB
125-
- proto
126-
- PROTO
127-
EnclosingFunctions:
128-
- EqualsProto
129-
- EquivToProto
130-
- PARSE_PARTIAL_TEXT_PROTO
131-
- PARSE_TEST_PROTO
132-
- PARSE_TEXT_PROTO
133-
- ParseTextOrDie
134-
- ParseTextProtoOrDie
135-
CanonicalDelimiter: ''
136-
BasedOnStyle: google
137-
ReflowComments: true
138-
SortIncludes: true
139-
SortUsingDeclarations: true
140-
SpaceAfterCStyleCast: false
141-
SpaceAfterLogicalNot: false
142-
SpaceAfterTemplateKeyword: true
143-
SpaceBeforeAssignmentOperators: true
144-
SpaceBeforeCpp11BracedList: false
145-
SpaceBeforeCtorInitializerColon: true
146-
SpaceBeforeInheritanceColon: true
147-
SpaceBeforeParens: ControlStatements
148-
SpaceBeforeRangeBasedForLoopColon: true
149-
SpaceInEmptyBlock: false
150-
SpaceInEmptyParentheses: false
151-
SpacesBeforeTrailingComments: 2
152-
SpacesInAngles: false
153-
SpacesInConditionalStatement: false
154-
SpacesInContainerLiterals: true
155-
SpacesInCStyleCastParentheses: false
156-
SpacesInParentheses: false
157-
SpacesInSquareBrackets: false
158-
SpaceBeforeSquareBrackets: false
159-
Standard: Auto
160-
StatementMacros:
161-
- Q_UNUSED
162-
- QT_REQUIRE_VERSION
163-
TabWidth: 4
164-
UseCRLF: false
165-
UseTab: Never
166-
...
34+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
35+
36+
# Format macros correctly
37+
AlwaysBreakBeforeMultilineStrings: true
38+
AllowAllArgumentsOnNextLine: true
16739

40+
# Enable include sorting with quotation marks before angle brackets
41+
IncludeBlocks: Regroup
42+
IncludeIsMainRegex: '(Test)?$'
43+
IncludeCategories:
44+
- Regex: '^<.*\.h>'
45+
Priority: 1
46+
- Regex: '^".*\.h"'
47+
Priority: 2
48+
- Regex: '^<.*>'
49+
Priority: 3
50+
- Regex: '^".*"'
51+
Priority: 4

.github/workflows/ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: C++ CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
# Step 1: Checkout the code
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
# Step 2: Install CMake and dependencies
21+
- name: Install dependencies
22+
run: sudo apt-get update && sudo apt-get install -y cmake g++
23+
24+
# Step 3: Configure the project with CMake
25+
- name: Configure CMake
26+
run: cmake -B build -S .
27+
28+
# Step 4: Build the project
29+
- name: Build project
30+
run: cmake --build build
31+
32+
# Step 5: Run the application (optional)
33+
- name: Run tests (or main app)
34+
run: ./build/problems/max_unique_split && ./build/arrays/array # todo

CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ project(
1010
find_library(MATH_LIBRARY m)
1111

1212
set(CMAKE_CXX_STANDARD 17)
13-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14-
include_directories(./include)
13+
set(CMAKE_CXX_STANDARD_REQUIRED True)
14+
15+
include_directories(include)
1516

1617
add_subdirectory(arrays)
18+
add_subdirectory(problems)
1719

1820
cmake_policy(SET CMP0054 NEW)
1921
cmake_policy(SET CMP0057 NEW)
@@ -37,3 +39,16 @@ endif ()
3739
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
3840
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
3941
include(CPack)
42+
43+
include(FetchContent)
44+
FetchContent_Declare(
45+
googletest
46+
GIT_REPOSITORY https://github.com/google/googletest.git
47+
GIT_TAG v1.12.x
48+
)
49+
50+
FetchContent_MakeAvailable(googletest)
51+
52+
enable_testing()
53+
54+
add_subdirectory(tests)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# brew install clang-format
22
format:
3-
clang-format -i *.cpp
3+
clang-format -i utils/*.hpp && clang-format -i problems/*.cpp

problems/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_executable(max_unique_split max_unique_split.cpp)

problems/longest_palindrome.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//#include "../utils/common.hpp"
2+
//
3+
//class Solution {
4+
//public:
5+
// string longestPalindrome(string s) {
6+
//
7+
// }
8+
//};

problems/max_unique_split.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "../utils/common.hpp"
2+
3+
class MaxUniqueSubstringSplitter
4+
{
5+
public:
6+
int maxUniqueCount;
7+
8+
void findMaxUniqueSplit(int startIndex, const std::string &input,
9+
std::set<std::string> &usedSubstrings, int currentCount)
10+
{
11+
if (startIndex == input.size())
12+
{
13+
maxUniqueCount = std::max(maxUniqueCount, currentCount);
14+
return;
15+
}
16+
17+
std::string substring;
18+
for (int i = startIndex; i < input.size(); ++i)
19+
{
20+
substring.push_back(input[i]);
21+
if (usedSubstrings.find(substring) == usedSubstrings.end())
22+
{
23+
usedSubstrings.insert(substring);
24+
findMaxUniqueSplit(i + 1, input, usedSubstrings, currentCount + 1);
25+
usedSubstrings.erase(substring); // Backtrack
26+
}
27+
}
28+
}
29+
30+
int getMaxUniqueSplit(const std::string &input)
31+
{
32+
std::set<std::string> usedSubstrings;
33+
maxUniqueCount = 0;
34+
findMaxUniqueSplit(0, input, usedSubstrings, 0);
35+
return maxUniqueCount;
36+
}
37+
};
38+
39+
void runTestCase(const std::string &input, int expected)
40+
{
41+
MaxUniqueSubstringSplitter splitter;
42+
int result = splitter.getMaxUniqueSplit(input);
43+
std::cout << "Input: \"" << input << "\", Expected: " << expected << ", Result: " << result
44+
<< (result == expected ? " [PASS]" : " [FAIL]") << std::endl;
45+
}
46+
47+
int main()
48+
{
49+
runTestCase("ababccc", 5);
50+
runTestCase("aba", 2);
51+
runTestCase("aa", 1);
52+
53+
return 0;
54+
}

tests/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
set(
2+
TEST_SOURCES test_main.cpp
3+
${PROJECT_SOURCE_DIR}/arrays/array.cpp
4+
${PROJECT_SOURCE_DIR}/problems/max_unique_split.cpp
5+
${PROJECT_SOURCE_DIR}/problems/longest_palindrome.cpp
6+
)
7+
8+
add_executable(
9+
test_main
10+
${TEST_SOURCES}
11+
)
12+
13+
target_include_directories(test_main PRIVATE ${PROJECT_SOURCE_DIR})
14+
15+
target_link_libraries(
16+
test_main
17+
gtest_main
18+
gtest
19+
pthread
20+
)
21+
22+
include(GoogleTest)
23+
gtest_discover_tests(test_main)

tests/arrays_tests.cpp

Whitespace-only changes.

tests/test_main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//#include <gtest/gtest.h>
2+
//
3+
//// Main function for running the tests
4+
//int main(int argc, char **argv) {
5+
//
6+
//}

0 commit comments

Comments
 (0)