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

[cpp] Challenge 2 (Pending) #450

Merged
merged 9 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions challenge_0/cpp/dewie102/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Hello World
###### C++ 11

### 1. Approch to solving the probelm

Pretty self explainitory, just getting the basics down and making sure everything is correct.

### 2. How to compile and run this

In Windows - I used the Visual Studio C++ compiler, move to the proper directory and run:

```
cl.exe src/HelloWorld.cpp
HelloWorld.exe
```

### 3. How this program works

The program just outputs "Hello, World!" on the main output stream.
6 changes: 6 additions & 0 deletions challenge_0/cpp/dewie102/src/HelloWorld.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

int main(int argv, char** argc) {
std::cout << "Hello, World!" << std::endl;
return 0;
}
21 changes: 21 additions & 0 deletions challenge_2/cpp/dewie102/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Single Number
###### C++ 11

### 1. Approch to solving the problem

Traverse the array, with each number/ character search a map of all previously checked items. If the number/ character is present, increase the count. If it is not then add it to the map. Once complete iterate over the map to find the count that is equal to 1 and output that number/ character.

### 2. How to compile and run this

In Windows - I used the Visual Studio C++ compiler, move to the proper directory and run:

```
cl.exe /EHsc src/SingleNumber.cpp
SingleNumber.exe
```

You can edit the .cpp file and change up the array or change the CountArray parameter to a different array you want to test. You have to rerun the code above to recompile and run the program.

### 3. How this program works

Given the hardcoded array, I stored it as a vector of strings in order to be able to do both numbers and characters. The function stores how many of the same character appears in the array using a map. It then iterates the map searching for the key that has a 1 mapped to it, signifying only one instance.
39 changes: 39 additions & 0 deletions challenge_2/cpp/dewie102/src/SingleNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

vector<string> input = {"2", "3", "4", "2", "3", "5", "4", "6", "4", "6", "9", "10", "9", "8", "7", "8", "10", "7"};
vector<string> extra = {"2", "a", "l", "3", "l", "4", "k", "2", "3", "4", "a", "6", "c", "4", "m", "6", "m", "k", "9", "10", "9", "8", "7", "8", "10", "7"};
map<string, int> table; // First = number/ character, Second = count

// Counts how many times each number/ character appears by placing it in the map or incrementing the count
void CountArray(vector<string> array){
for(int i = 0; i < array.size(); i++){
auto it = table.find(array[i]);
if(it != table.end()){ // If the number/ character is found, increment count
it->second++;
} else{
table.emplace(array[i], 1); // Else add it to the map
}
}
}

// Iterate through map to find where the count = 0
string FindSingle(){
for(auto it = table.begin(); it != table.end(); it++){
if(it->second == 1){
return it->first;
}
}

return NULL;
}

int main(int argc, char** argv){
CountArray(input);
cout << "The number/ character that appears once is: " << FindSingle() << endl;
return 0;
}