Created
April 14, 2014 03:35
-
-
Save eroth/10614031 to your computer and use it in GitHub Desktop.
C++ program I wrote when participating in a recent coding challenge. The object was to read in a text file and follow specified logic.
This file contains 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
#include <iostream> | |
#include <cstdio> | |
#include <algorithm> | |
#include <fstream> | |
using namespace std; | |
struct Legit { | |
string city; | |
string color; | |
int legitScore; | |
}; | |
int main() { | |
ifstream infile("/Users/eroth/Dropbox/googlegames/google1/sample.txt"); | |
int numTestCases; // Total number of test cases per file | |
int numPantsToCompare; // Number of pants per case | |
int legitTime = 0; // Keeps track of number of legit time per case | |
bool legitInSameCity = false; // Keeps track of whether we already have a legit match in the same city | |
bool legitSoFar = false; // Running counter of whether we're legit so far; i.e., if we have a unique case | |
infile >> numTestCases; | |
// Execute loop numTestCases amount of times | |
for (int x = 0; x < numTestCases; x++) { | |
infile >> numPantsToCompare; | |
// Create array of Legit objects equal in size to numPantsToCompare for each test case | |
Legit *test[numPantsToCompare]; | |
// Create an instance of a new legit object for each index, read a line from the file into it, and store it in our array | |
for (int i = 0; i < numPantsToCompare; i++) { | |
Legit *legit = new Legit(); | |
infile >> legit->city >> legit->color >> legit->legitScore; | |
test[i] = legit; | |
} | |
// Comparison algorithm | |
for (int i = 0; i < numPantsToCompare; i++) { | |
for (int k = 0; k < numPantsToCompare; k++) { | |
// We don't want to compare a case to itself, so if the indices are equal increment k, except if k = numPantsToCompare | |
if (i == k) { | |
k++; | |
if (k == numPantsToCompare) break; | |
} | |
# pragma mark debug | |
// Outputs comparison cases | |
cout << "comparing i:" << i << " " << test[i]->city << " " << test[i]->color << " " << test[i]->legitScore << endl; | |
cout << "to k:" << k << " " << test[k]->city << " " << test[k]->color << " " << test[k]->legitScore << endl; | |
if (test[i]->color == test[k]->color) { | |
if ((test[i]->city != test[k]->city) && (test[i]->legitScore == test[k]->legitScore)) { | |
// If the cities aren't equal it means we won't have any legitTime, so set it to false and break | |
legitSoFar = false; | |
break; | |
} | |
else if ((test[i]->city == test[k]->city) && (test[i]->legitScore == test[k]->legitScore) && !legitInSameCity) { | |
// Addresses case we have a legit match in the same city | |
legitInSameCity = true; | |
break; | |
} | |
} | |
else { | |
// If we've come this far, we're at the end of a comparison loop and we're legit | |
legitSoFar = true; | |
} | |
} | |
if (legitSoFar && !legitInSameCity) { | |
// If we're legitSoFar and we don't have a match in the same city already, increment our legitTime | |
legitTime++; | |
legitSoFar = false; | |
} | |
legitInSameCity = false; | |
} | |
// Output our legit time for this case | |
cout << "Case #" << x+1 << ": " << legitTime << endl; | |
// Reset everything for the next loop | |
legitInSameCity = false; | |
legitTime = 0; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment