Skip to content

[C++] Challenge 10 (Unreviewed) #375

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions _bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down
33 changes: 33 additions & 0 deletions challenge_10/cpp/manuel/Makefile
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions challenge_10/cpp/manuel/README.md
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions challenge_10/cpp/manuel/src/include/tools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef TOOLS_H
#define TOOLS_H

namespace tools {

std::map<char, int> count(std::string);
bool verify_closers(std::map<char, int>);

}

#endif
20 changes: 20 additions & 0 deletions challenge_10/cpp/manuel/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <string>
#include <map>

#include "include/tools.h"

int main(int argc, char **argv) {

std::string input;
std::getline(std::cin, input);

std::map<char, int> counter = tools::count(input);
if (tools::verify_closers(counter)) {
std::cout << "True" << std::endl;
} else {
std::cout << "False" << std::endl;
}

return 0;
}
45 changes: 45 additions & 0 deletions challenge_10/cpp/manuel/src/tools.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <map>
#include <string>

#include "include/tools.h"

std::map<char, int> tools::count (std::string input) {

std::map<char, int> 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['(']--;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work correctly for "{ [ } ]" ?

} 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<char, int> counter) {

if(counter.empty() || counter ['<'] != 0 || counter['{'] != 0 || counter['('] != 0 || counter['['] != 0 ) {
return false;
}

return true;
}