Skip to content

Commit

Permalink
Format files with Google style clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanilov committed Jul 16, 2022
1 parent ad32426 commit 0fddfc0
Show file tree
Hide file tree
Showing 42 changed files with 491 additions and 293 deletions.
226 changes: 226 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveMacros: None
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: true
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: NextLine
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<ext/.*\.h>'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: '^<.*'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '([-_](test|unittest))?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequires: false
IndentWidth: 2
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PenaltyIndentedWhitespace: 0
PointerAlignment: Left
PPIndentWidth: -1
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- 'c++'
- 'C++'
CanonicalDelimiter: ''
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Cracking the Coding Interview C++

![CI](https://github.com/mdanilov/cracking-coding/workflows/CI/badge.svg?branch=master)

Solutions for [Cracking the Coding Interview 6th Edition](http://www.amazon.com/Cracking-Coding-Interview-6th-Edition/dp/0984782850) by [Gayle Laakmann McDowell](http://www.gayle.com/) in C++ programming language.

## Getting Started

For each book chapter, there is a folder with the same name. Each question and solution for it located at the own CPP file inside the chapter folders. There is also a trivial test for each solution located at the end of the file. Execute the following commands to clone, build and run the tests:
```

``` none
git clone --recurse-submodules https://github.com/mdanilov/cracking-coding.git
cd cracking-coding
mkdir build && cd build && cmake ..
Expand Down
5 changes: 3 additions & 2 deletions include/utils/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <type_traits>

namespace utils {
template <class T> inline T logicalShift(T t1, T t2) {
template <class T>
inline T logicalShift(T t1, T t2) {
return static_cast<typename std::make_unsigned<T>::type>(t1) >> t2;
}
} // namespace utils
} // namespace utils
12 changes: 7 additions & 5 deletions include/utils/forward_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
#include <initializer_list>
#include <memory>

template <typename T> class ForwardList {
public:
template <typename T1 = T> class Node {
public:
template <typename T>
class ForwardList {
public:
template <typename T1 = T>
class Node {
public:
std::shared_ptr<Node<T1>> next = nullptr;
T1 data;

Expand Down Expand Up @@ -55,4 +57,4 @@ template <typename T> class ForwardList {
}

NodePtr head = nullptr;
};
};
10 changes: 5 additions & 5 deletions include/utils/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Node;
using NodePtr = std::shared_ptr<Node>;

class Node {
public:
public:
enum class State { kUnvisited, kVisited, kVisiting };

Node(int index) : index(index), state(State::kUnvisited) {}
Expand All @@ -20,7 +20,7 @@ class Node {

State state;

private:
private:
int index;
std::vector<NodePtr> nodes;
};
Expand All @@ -31,7 +31,7 @@ struct Edge {
};

class Graph {
public:
public:
Graph(const std::vector<Edge> &edges, int N) {
nodes.resize(N);
for (int i = 0; i < N; ++i) {
Expand All @@ -47,8 +47,8 @@ class Graph {

std::vector<NodePtr> &getNodes() { return nodes; }

private:
private:
std::vector<NodePtr> nodes;
};

} // namespace utils
} // namespace utils
2 changes: 1 addition & 1 deletion include/utils/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ struct TreeNode {

using TreeNodePtr = std::shared_ptr<TreeNode>;

} // namespace utils
} // namespace utils
1 change: 0 additions & 1 deletion src/01_arrays_and_strings/check_permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ bool permutation_2(const string &s, const string &t) {
#include "gtest/gtest.h"

TEST(CheckPermutationTest, Trivial) {

EXPECT_TRUE(permutation_1("abcd", "dcba"));
EXPECT_FALSE(permutation_1("abcd", "daba"));

Expand Down
4 changes: 1 addition & 3 deletions src/01_arrays_and_strings/is_unique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ using namespace std;
// ------------------------------------------------------------------------------------------------

bool isUniqueChars_1(const string& str) {
if (str.length() > 128)
return false;
if (str.length() > 128) return false;

bool char_set[128] = {0};
for (int i = 0; i < str.length(); ++i) {
Expand Down Expand Up @@ -43,7 +42,6 @@ bool isUniqueChars_2(const string& str) {
#include "gtest/gtest.h"

TEST(IsUniqueTest, Trivial) {

EXPECT_TRUE(isUniqueChars_1("abcd"));
EXPECT_FALSE(isUniqueChars_1("abcc"));

Expand Down
8 changes: 3 additions & 5 deletions src/01_arrays_and_strings/one_away.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,16 @@ bool oneEditAway_2(const string &first, const string &second) {
while (index2 < s2.length() && index1 < s1.length()) {
if (s1[index1] != s2[index2]) {
/* Ensure that this is the first difference found.*/
if (foundDifference)
return false;
if (foundDifference) return false;
foundDifference = true;
if (s1.length() == s2.length()) {
// On replace, move shorter pointer
index1++;
}
} else {
index1++; // If matching, move shorter pointer
index1++; // If matching, move shorter pointer
}
index2++; // Always move pointer for longer string
index2++; // Always move pointer for longer string
}
return true;
}
Expand All @@ -99,7 +98,6 @@ bool oneEditAway_2(const string &first, const string &second) {
#include "gtest/gtest.h"

TEST(OneAwayTest, Trivial) {

EXPECT_TRUE(oneEditAway_1("pale", "ple"));
EXPECT_TRUE(oneEditAway_1("pales", "pale"));
EXPECT_TRUE(oneEditAway_1("pale", "bale"));
Expand Down
3 changes: 1 addition & 2 deletions src/01_arrays_and_strings/palindrome_permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ bool isPermutationOfPalindrome_2(const string &phrase) {

/* Toggle the ith bit in the integer. */
int toggle(int bitVector, int index) {
if (index < 0)
return bitVector;
if (index < 0) return bitVector;
int mask = 1 << index;
if ((bitVector & mask) == 0) {
bitVector |= mask;
Expand Down
Loading

0 comments on commit 0fddfc0

Please sign in to comment.