diff --git a/_bin/test b/_bin/test index 2ed3f0db4..e2f970f96 100755 --- a/_bin/test +++ b/_bin/test @@ -91,7 +91,10 @@ for input in $test_dir/t*; do diff user_result $test_dir/o$count > comparison if [[ -s comparison ]] ; then + echo "----------------------------" echo "Incorrect for $count th example."; + cat comparison + echo "----------------------------" else echo "Correct for $count th example."; let corr++ diff --git a/challenge_10/cpp/manuel/Makefile b/challenge_10/cpp/manuel/Makefile new file mode 100644 index 000000000..f9c2b5238 --- /dev/null +++ b/challenge_10/cpp/manuel/Makefile @@ -0,0 +1,33 @@ +SRC := $(wildcard src/*.cpp) +HEADERS := $(wildcard $(addprefix src/include/, *.h)) + +OBJDIR := obj +BINDIR := bin +OBJECTS := $(addprefix $(OBJDIR)/, $(notdir src/ $(SRC:.cpp=.o))) + +EXE := solution.exe +CC := g++ +CFLAGS := -Wall -c -std=c++11 +LFLAGS := -Wall + +$(EXE) : $(BINDIR) $(OBJDIR) $(OBJECTS) + @$(CC) $(LFLAGS) -o $(BINDIR)/$@ $(OBJECTS) + +obj/%.o : src/%.cpp $(HEADERS) + @$(CC) $(CFLAGS) -o $@ $< + +.PHONY : clean test $(BINDIR) $(OBJDIR) + +clean : + @rm -rf $(BINDIR) $(OBJDIR) + +test: + @cd ../../../;\ + ./_bin/test 10 cpp manuel;\ + cd challenge_10/cpp/manuel/;\ + +$(BINDIR) : + @mkdir -p $(BINDIR) + +$(OBJDIR) : + @mkdir -p $(OBJDIR) diff --git a/challenge_10/cpp/manuel/README.md b/challenge_10/cpp/manuel/README.md new file mode 100644 index 000000000..bcf84f48d --- /dev/null +++ b/challenge_10/cpp/manuel/README.md @@ -0,0 +1,29 @@ +# Valid Closers +###### C++11 @manuel + +### 1. Approch to Solving the problem + +To solve this challenge I used a map to count the closers. + +If we had a left bracket we incremented the map to it by one. + +If we had a right bracket, we decremented the map to the corresponding +left bracket by one. + +If at any point, the counter fell below 0, then there was a right +bracket without a corresponding left bracket and the test was False; + +At the end of all the keys returna a value of 0, then all the brackets +were closed. + +### 2. How to compile and run this code + +``` +make +make test +make clean +``` + +### 3. How this program works + +Single line of input -> Single line of output diff --git a/challenge_10/cpp/manuel/src/include/tools.h b/challenge_10/cpp/manuel/src/include/tools.h new file mode 100644 index 000000000..99caa9de5 --- /dev/null +++ b/challenge_10/cpp/manuel/src/include/tools.h @@ -0,0 +1,11 @@ +#ifndef TOOLS_H +#define TOOLS_H + +namespace tools { + + std::map count(std::string); + bool verify_closers(std::map); + +} + +#endif diff --git a/challenge_10/cpp/manuel/src/main.cpp b/challenge_10/cpp/manuel/src/main.cpp new file mode 100644 index 000000000..9a28be8c0 --- /dev/null +++ b/challenge_10/cpp/manuel/src/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +#include "include/tools.h" + +int main(int argc, char **argv) { + + std::string input; + std::getline(std::cin, input); + + std::map counter = tools::count(input); + if (tools::verify_closers(counter)) { + std::cout << "True" << std::endl; + } else { + std::cout << "False" << std::endl; + } + + return 0; +} diff --git a/challenge_10/cpp/manuel/src/tools.cpp b/challenge_10/cpp/manuel/src/tools.cpp new file mode 100644 index 000000000..8045a8315 --- /dev/null +++ b/challenge_10/cpp/manuel/src/tools.cpp @@ -0,0 +1,45 @@ +#include +#include + +#include "include/tools.h" + +std::map tools::count (std::string input) { + + std::map counter; + int length = input.length(); + + for(int i = 0; i < length; i++) { + if(input[i] == '(' || input[i] == '{' || input[i] == '[' || input[i] == '<') { + // count all the openers + counter[input[i]]++; + + // count the closers + } else if(input[i] == ')') { + counter['(']--; + } else if(input[i] == '}') { + counter['{']--; + } else if(input[i] == ']') { + counter['[']--; + } else if(input[i] == '>') { + counter['<']--; + } + + if(counter['('] < 0 || counter['{'] < 0 || counter['['] < 0 || counter['<'] < 0) { + // Right side closer without a pair + + counter.clear(); + return counter; + } + } + + return counter; +} + +bool tools::verify_closers(std::map counter) { + + if(counter.empty() || counter ['<'] != 0 || counter['{'] != 0 || counter['('] != 0 || counter['['] != 0 ) { + return false; + } + + return true; +}