-
Notifications
You must be signed in to change notification settings - Fork 160
[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
ManuelMeraz
wants to merge
14
commits into
YearOfProgramming:master
Choose a base branch
from
ManuelMeraz-PersonalProjects:cpp_10
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
361428f
new test
ManuelMeraz 95f43f5
fixed testfiles and readme
ManuelMeraz 9d9a3ee
Merge branch 'master' into testing
ManuelMeraz eaebd42
made testfiles
ManuelMeraz b911f4b
testfiles created
ManuelMeraz 67d99e5
solution
ManuelMeraz aaa9c2f
fixed testfiles
ManuelMeraz 7876160
challenge 10 completed
ManuelMeraz 1815213
Merge branch 'master' of github.com:YearOfProgramming/2017Challenges …
ManuelMeraz 38e82d0
Merge branch 'testing' of github.com:YearOfProgramming/2017Challenges…
ManuelMeraz 10cad72
modified testfile
ManuelMeraz 9bbed5a
removed junk file
ManuelMeraz 3525060
Merge branch 'master' into cpp_10
ManuelMeraz 5c24422
fixed bug in code
ManuelMeraz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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['(']--; | ||
} 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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 "{ [ } ]" ?