Skip to content

Instantly share code, notes, and snippets.

@sreeprasad
Last active July 3, 2018 03:26
Show Gist options
  • Save sreeprasad/de53a95a1b26b4d8ac80abb2d9c1db6d to your computer and use it in GitHub Desktop.
Save sreeprasad/de53a95a1b26b4d8ac80abb2d9c1db6d to your computer and use it in GitHub Desktop.
From Programming for the Puzzled course from MIT
# include <iostream>
# include <vector>
using namespace std;
int main() {
char caps[] = {'F', 'F', 'B', 'B', 'B', 'F', 'B', 'B', 'B', 'F', 'F', 'B', 'F'};
int N = sizeof(caps)/sizeof(caps[0]);
char somearray [] = {'a','b'};
int m = sizeof(somearray)/sizeof(somearray[0]);
// I don't want to modify the original array so I am going to create a copy
// and add (N+1)TH element. The (N+1)th element will be the cap at position 0
vector<char> modifiedCaps(caps, caps+N);
// Copy the first cap to the last. This won't be compared but gives us cleaner code
modifiedCaps[N]= caps[0];
for(int i=1;i<(N+1);i++){
// if any current cap and previous cap are different
if (modifiedCaps[i] != modifiedCaps[i-1]) {
// and current cap is not same as first cap
if(modifiedCaps[i] != modifiedCaps[0]) {
// then all people from current position
cout<<"People in positions "<<i;
} else{
// up untill the the position where current cap is same as first cap FLIP
cout<<" through "<<(i-1)<<" flip your caps "<<endl;;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment