Skip to content
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

Implementing data validation #209

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
df9044c
Update copyright year
faheel Feb 14, 2018
d3f260d
Update contribution steps and fix links
faheel Feb 14, 2018
8cf792c
Move GitHub specific files under .github
faheel Feb 14, 2018
8b34d93
Update Catch to v2.1.2
faheel Feb 14, 2018
59b6cb8
Remove "Worst case: "
faheel Feb 14, 2018
f4fff71
Ignore files for IDE and code editors
faheel Feb 14, 2018
e6f75b3
Encapsulate N-Queens solving functions into a class
faheel Feb 15, 2018
dfaafc2
Remove main() and move sorting functions into header files
faheel Feb 16, 2018
3e27256
Update docs for adding unit tests
faheel Feb 16, 2018
e307f26
Fix possible overflow when calculating mid
faheel Feb 17, 2018
7cf7441
Use namespace std in tests
faheel Feb 17, 2018
913c5d1
Handle case when vector is empty
faheel Feb 17, 2018
50c0b2e
Move searching into algorithm
faheel Mar 11, 2018
a9369a8
Encapsulate matrix chain multiplication functions into a class
faheel Mar 11, 2018
ecf9124
Add documentation for matrix chain multiplication
faheel Mar 11, 2018
0afe13c
Use CMake for the build process
faheel Mar 11, 2018
c408b8e
Update install and build scripts
faheel Mar 11, 2018
392d800
Reorganise code for disjoint set
faheel Mar 11, 2018
8a97b95
Add script to run tests
faheel Mar 11, 2018
31d12c9
Add target to run tests
faheel Mar 11, 2018
3809990
Update build command
faheel Mar 12, 2018
0248d7f
Add include guard and move to algorithm folder
faheel Mar 12, 2018
331874b
Add description of variable
faheel Mar 12, 2018
c9c708d
Update N queens interface
faheel Mar 12, 2018
23e0db4
Add documentation for N queens
faheel Mar 12, 2018
6261bea
Update unit tests' filename pattern
faheel Sep 3, 2018
78d024d
Fix comment
faheel Sep 5, 2018
f6920ac
Update steps for making PRs to the `reorganise` branch (#198)
Sep 9, 2018
30b7321
Replace `using namespace std` with more specific directives (#200)
alxmjo Sep 9, 2018
5895a9b
Add edit distance algorithm (#193)
sumit9696 Sep 13, 2018
37342f2
Migrate greatest common divisor into a header file (#201)
alxmjo Sep 13, 2018
fc33738
Add 0-1 knapsack (#191)
Sep 13, 2018
3f4bb45
Migrate extended Euclidean algorithm to a header file (#202)
alxmjo Sep 14, 2018
3b3187f
Add docs for adding unit test in `CMakeLists.txt`
faheel Sep 16, 2018
3824e79
implement utility functions: issue #99
mrKappen Oct 2, 2018
662de4b
implement utility functions: issue #99
mrKappen Oct 2, 2018
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
5 changes: 2 additions & 3 deletions CODE_OF_CONDUCT.md → .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ members of the project's leadership.
## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
available at https://www.contributor-covenant.org/version/1/4

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
[homepage]: https://www.contributor-covenant.org/
37 changes: 26 additions & 11 deletions CONTRIBUTING.md → .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
# Contributing Guidelines

### Contents

* [General guidelines](#general-guidelines)
* [C++ coding guidelines](#c-coding-guidelines)
* [Pull requests](#pull-requests)
* [Code of conduct](#code-of-conduct)

## General guidelines
* When contributing to this repository, please first inform or discuss the change(s) you wish to make via an issue. This helps in letting others know what you're working on.

* For each algorithm, mention its **time complexity** and **space complexity** in the _"description comment"_ of its implementation. In case the average-case and worst-case complexities are different, mention both of them.
* When contributing to this repository, please first inform or discuss the
change(s) you wish to make via an issue. This helps in letting others know
what you're working on.

* For each algorithm, mention its **time complexity** and **space complexity**
in the _"description comment"_ of its implementation. In case the average-case
and worst-case complexities are different, mention both of them.

The format for the _"description comment"_ (which is written at the beginning) should be:
```
Expand All @@ -28,12 +34,14 @@
* Before you push your changes to GitHub, make sure that your code compiles and runs without any errors or warnings.

## C++ coding guidelines
If you are contributing C++ code to this repo, make sure to read the [C++ Coding Guidelines](C++/CODING_GUIDELINES.md).

If you are contributing C++ code to this repo, make sure to read the [C++ Coding Guidelines](../C++/CODING_GUIDELINES.md).

## Pull requests

Follow the steps below to contribute to the project:

1. [Fork](https://help.github.com/fork-a-repo/) the repo, clone your fork, and configure the remotes:
1. [Fork][fork-guide] the repo, clone your fork, and configure the remotes:

```bash
# Clone your fork of the repo into the current directory
Expand All @@ -47,23 +55,24 @@ Follow the steps below to contribute to the project:
2. If you cloned a while ago, get the latest changes from upstream:

```bash
git checkout master
git pull upstream master
git checkout reorganise
git pull upstream reorganise
```

3. Create a new branch (off the `master` branch) to contain your code for a specific algorithm or data structure:
3. Create a new branch (off the `reorganise` branch) to contain your code for a
specific algorithm or data structure:

```bash
git checkout -b <branch-name>
```

4. Commit your changes in logical chunks. Use Git's [interactive rebase](https://help.github.com/articles/interactive-rebase)
4. Commit your changes in logical chunks. Use Git's [interactive rebase][rebase-guide]
feature to tidy up your commits before making them public.

5. Locally merge (or rebase) the upstream development branch into your branch:

```bash
git pull [--rebase] upstream master
git pull [--rebase] upstream reorganise
```

6. Push your branch up to your fork:
Expand All @@ -72,8 +81,14 @@ Follow the steps below to contribute to the project:
git push origin <branch-name>
```

7. [Open a pull request](https://help.github.com/articles/about-pull-requests/)
with a clear title and description against the `master` branch.
7. [Open a pull request][pr-guide] with a clear title and description against the
`reorganise` branch.

## Code of Conduct

This project has a [Code of Conduct](CODE_OF_CONDUCT.md). Please follow it in all your interactions with the project.


[fork-guide]: https://help.github.com/fork-a-repo/
[rebase-guide]: https://help.github.com/articles/interactive-rebase
[pr-guide]: https://help.github.com/articles/about-pull-requests/
File renamed without changes.
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# C++
## Binaries
bin
# Binaries
bin/

## Intermediate build files
build
# Intermediate build files
build/

# Files related to IDE and code editors
.idea/
.vscode/
*.swp
16 changes: 11 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ matrix:
before_install:
- export CXX=${COMPILER}

install: true
install:
- mkdir -p tmp && cd tmp
- |
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
CMAKE_URL="https://cmake.org/files/v3.10/cmake-3.10.2-Linux-x86_64.tar.gz"
mkdir cmake && travis_retry wget --no-check-certificate --quiet -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C cmake
export PATH=tmp/cmake/bin:${PATH}
elif [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then
which cmake || brew install cmake;
fi

script:
- |
cd C++
make
make test
- cd ${TRAVIS_BUILD_DIR}/C++ && make && make test
85 changes: 85 additions & 0 deletions C++/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
cmake_minimum_required(VERSION 3.0)

project(Algos)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

include_directories(include)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-g -O0 -Wall -Wextra -pedantic-errors")

# Test runner
add_library(test_runner STATIC
test/test_runner.cpp)

# ============================================================================
# Algorithms
# ============================================================================

# ------
# String
# ------

# Edit distance Problem
add_executable(edit_distance
test/algorithm/string/edit_distance.cpp)
target_link_libraries(edit_distance test_runner)

# -------------------
# Dynamic programming
# -------------------

# Matrix chain multiplication
add_executable(matrix_chain_multiplication
test/algorithm/dynamic_programming/matrix_chain_multiplication.cpp)
target_link_libraries(matrix_chain_multiplication test_runner)

# 0-1 knapsack
add_executable(0_1_knapsack
test/algorithm/dynamic_programming/0_1_knapsack.cpp)
target_link_libraries(0_1_knapsack test_runner)

# -------------
# Number theory
# -------------

# Greatest common divisor
add_executable(greatest_common_divisor
test/algorithm/number_theory/greatest_common_divisor.cpp)
target_link_libraries(greatest_common_divisor test_runner)

# Extended Euclidean
add_executable(extended_euclidean
test/algorithm/number_theory/extended_euclidean.cpp)
target_link_libraries(extended_euclidean test_runner)

# ---------
# Searching
# ---------

# Binary search
add_executable(binary_search
test/algorithm/searching/binary_search.cpp)
target_link_libraries(binary_search test_runner)

# Linear search
add_executable(linear_search
test/algorithm/searching/linear_search.cpp)
target_link_libraries(linear_search test_runner)

# Ternary search
add_executable(ternary_search
test/algorithm/searching/ternary_search.cpp)
target_link_libraries(ternary_search test_runner)

# ============================================================================
# Data structures
# ============================================================================

# Disjoint set
add_executable(disjoint_set
test/data_structure/disjoint_set.cpp)
target_link_libraries(disjoint_set test_runner)
94 changes: 11 additions & 83 deletions C++/Makefile
Original file line number Diff line number Diff line change
@@ -1,94 +1,22 @@
# c++ version
CXXFLAGS += -std=c++14

# warning flags
CXXFLAGS += -Wall -Wextra -pedantic-errors

# flags for dependency generation
CXXFLAGS += -MMD -MP

# include directories
INCLUDES += -Iinclude -I.

PROG_DIRS := $(patsubst source/%, %, $(wildcard source/*))
TEST_DIRS := $(patsubst test/%, %, $(wildcard test/*))

PROG_SOURCES := $(wildcard source/*/*.cpp)
TEST_SOURCES := $(wildcard test/*/*.cpp)
SOURCES := $(PROG_SOURCES)
SOURCES += $(TEST_SOURCES)

PROG_OBJECTS := $(patsubst source/%.cpp, build/%.o, $(PROG_SOURCES))
TEST_OBJECTS := $(patsubst test/%.cpp, build/test/%.o, $(TEST_SOURCES))
OBJECTS := $(PROG_OBJECTS)
OBJECTS += $(TEST_OBJECTS)

PROG_EXECUTABLES := $(patsubst source/%.cpp, bin/%, $(PROG_SOURCES))
TEST_EXECUTABLES := $(patsubst test/%.cpp, bin/%, $(TEST_SOURCES))
EXECUTABLES := $(PROG_EXECUTABLES)
EXECUTABLES += $(TEST_EXECUTABLES)

PROG_DEPENDENCIES := $(patsubst source/%.cpp, build/%.d, $(PROG_SOURCES))
TEST_DEPENDENCIES := $(patsubst test/%.cpp, build/%.d, $(TEST_SOURCES))
DEPENDENCIES := $(PROG_DEPENDENCIES)
DEPENDENCIES += $(TEST_DEPENDENCIES)

# prevent printing of recipes
.SILENT:

# keep intermediate object files after compilation
.SECONDARY: $(OBJECTS)

# include generated dependencies
-include $(DEPENDENCIES)

.PHONY: all
all: dirs $(PROG_EXECUTABLES)
# compile tests using CMake
.PHONY: default
default: dirs
cd build && cmake .. && make

# run tests
.PHONY: test
test: test-dirs $(TEST_EXECUTABLES)
find * -name *.test -exec sh -c "echo {}:; ./{}" \;

# compile objects from source files
build/%.o: source/%.cpp
printf '\e[94m%s\e[0m%s' 'Compiling source' ': $@ ... '
$(CXX) -c $(CXXFLAGS) $(INCLUDES) -o $@ $<
printf '\e[92m%s\e[0m\n' 'Done!'

# create program binaries
bin/%: build/%.o
printf '\e[96m%s\e[0m%s' 'Creating binary ' ': $@ ... '
$(CXX) $(CXXFLAGS) -o $@ $<
printf '\e[92m%s\e[0m\n' 'Done!'

# compile object file for Catch test runner
build/test/test_runner.o:
printf '\e[94m%s\e[0m%s' 'Compiling test runner' ' ... '
$(CXX) -c $(CXXFLAGS) $(INCLUDES) test/test_runner.cpp -o build/test/test_runner.o
printf '\e[92m%s\e[0m\n' 'Done!'

# compile test objects from source files
build/test/%.o: test/%.cpp
printf '\e[94m%s\e[0m%s' 'Compiling test ' ': $@ ... '
$(CXX) -c $(CXXFLAGS) $(INCLUDES) -o $@ $<
printf '\e[92m%s\e[0m\n' 'Done!'

# create test binaries from compiled objects and the test runner
bin/%: build/test/%.o build/test/test_runner.o
printf '\e[96m%s\e[0m%s' 'Creating binary ' ': $@ ... '
$(CXX) $(CXXFLAGS) -o $@ $+
printf '\e[92m%s\e[0m\n' 'Done!'
test: dirs
scripts/run_tests.sh

# create bin and build directories
.PHONY: dirs
dirs:
mkdir -p $(patsubst %, build/%, $(PROG_DIRS)) $(patsubst %, bin/%, $(PROG_DIRS))

.PHONY: test-dirs
test-dirs:
mkdir -p $(patsubst %, build/test/%, $(TEST_DIRS)) $(patsubst %, bin/%, $(TEST_DIRS))
mkdir -p bin build

# clean build files
.PHONY: clean
clean:
printf "\e[91m%s\e[0m%s" 'Removing all binaries and build files' ' ... '
rm -f $(EXECUTABLES) $(OBJECTS) $(DEPENDENCIES)
printf '\e[92m%s\e[0m\n' 'Done!'
cd build && make clean
14 changes: 8 additions & 6 deletions C++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ Implementation of well-known (and some rare) algorithms, in C++.

### Algorithms
* Backtracking
* [N-Queens](source/backtracking/n_queens.cpp)
* [N-Queens](include/algorithms/backtracking/n_queens.hpp)
* Dynamic programming
* [Matrix chain multiplication](source/dynamic_programming/matrix_chain_mult.cpp) :white_check_mark:
* [0-1 Knapsack Problem](include/algorithm/dynamic_programming/0_1_knapsack.hpp) :white_check_mark:
* Maximum sum contiguous subarray: [Kadane's algorithm](source/maximum_subarray/kadane.cpp)
* Number theory
* [Binomial coefficient](source/number_theory/binomial_coefficient.cpp) :white_check_mark:
* Euclidean algorithms
* [Greatest common divisor](source/number_theory/gcd.cpp) (GCD)
* [Extended Euclidean algorithm](source/number_theory/extended_euclid.cpp) (Bézout coefficients and GCD)
* [Greatest common divisor](include/algorithm/number_theory/greatest_common_divisor.hpp) (GCD)
* [Extended Euclidean algorithm](include/algorithm/number_theory/extended_euclidean.hpp) (Bézout coefficients) :white_check_mark:
* [Fast exponentiation](source/number_theory/fast_exponentiation.cpp) :white_check_mark:
* Nth Fibonacci number
* [Linear time algorithm](source/number_theory/fibonacci.cpp) :white_check_mark:
Expand All @@ -25,9 +26,9 @@ Implementation of well-known (and some rare) algorithms, in C++.
* Prime numbers
* [Sieve of Eratosthenes (simple)](source/number_theory/sieve_of_eratosthenes.cpp)
* Searching
* [Binary search](source/searching/binary_search.cpp) :white_check_mark:
* [Linear search](source/searching/linear_search.cpp) :white_check_mark:
* [Ternary search](source/searching/ternary_search.cpp) :white_check_mark:
* [Binary search](include/algorithms/searching/binary_search.hpp) :white_check_mark:
* [Linear search](include/algorithms/searching/linear_search.hpp) :white_check_mark:
* [Ternary search](include/algorithms/searching/ternary_search.hpp) :white_check_mark:
* Sorting
* [Bubble sort](source/sorting/bubble_sort.cpp)
* [Counting sort (stable)](source/sorting/counting_sort.cpp)
Expand All @@ -42,6 +43,7 @@ Implementation of well-known (and some rare) algorithms, in C++.
* [Longest common subsequence](source/string/lcs.cpp)
* Searching (pattern matching)
* [Knuth-Morris-Pratt](source/string/kmp.cpp)
* [Edit Distance Problem](include/algorithm/string/edit_distance.hpp) :white_check_mark:

### Data structures
* [Binary search tree](include/data_structures/binary_search_tree.cpp)
Expand Down
Loading