Last active
January 26, 2017 14:46
-
-
Save tinvaan/30c2442ddc752f6850003b8fe7ab55e2 to your computer and use it in GitHub Desktop.
Store days
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
/** | |
Opening and closing hours for a store: | |
Given two arrays of si*ze seven that respectively indicate opening and closing hours over days of week for a store, print the store hours in friendly format. | |
Special Conditions: | |
1. If opening hours are greater than closing hours, the store is assumed closed on that day. | |
2. If opening/closing hours are greater than 2400 hours or for any other error condition, the store is assumed closed that day. | |
3. If opening hours are equal to closing hours, the store is open for 24 hours. | |
Example: | |
Open - [900,1000,1000,1000,2700,1200,800] | |
Close - [1700,1600,1600,1600,1600,1200,800] | |
// 900 1000 1000 1000 2700 1200 800 -1 | |
// 1700 1600 1600 1600 1600 1200 800 -1 | |
Output: | |
Mon: 900-1700 | |
Tue-Thur: 1000-1600 | |
Fri: Closed | |
Sat-Sun: Open 24 Hours | |
*/ | |
#include <vector> | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
// Global vector holding the days | |
vector<int> days; | |
vector<string> namedDays; | |
void initDays(vector<int> ); | |
void prettyPrint(vector<int> o, vector<int> c); | |
int consecutiveDays(vector<int> o, vector<int> c); | |
bool storesClosed(vector<int> o, vector<int> c, int i); | |
int main(int argc, char *argv[]) | |
{ | |
int time; | |
vector<int> open, close; | |
// Populate opening times | |
cout << "Enter time when stores open" << endl; | |
while(cin >> time && time != -1) { | |
open.push_back(time); | |
} | |
// Populate closing times | |
cout << "Enter time when stores close" << endl; | |
while(cin >> time && time != -1) { | |
close.push_back(time); | |
} | |
cout << endl; | |
prettyPrint(open, close); | |
return 0; | |
} | |
void initDays(vector<int> d) | |
{ | |
for(int i = 0; i < d.size(); i++) { | |
days.push_back(1); | |
if(i == 0) namedDays.push_back("Mon"); | |
if(i == 1) namedDays.push_back("Tue"); | |
if(i == 2) namedDays.push_back("Wed"); | |
if(i == 3) namedDays.push_back("Thu"); | |
if(i == 4) namedDays.push_back("Fri"); | |
if(i == 5) namedDays.push_back("Sat"); | |
if(i == 6) namedDays.push_back("Sun"); | |
} | |
} | |
int consecutiveDays(vector<int> o, vector<int> c, int i) | |
{ | |
int count = 0; | |
while(i < o.size() - 1) { | |
if (o[i] == o[i+1] && c[i] == c[i+1]) { | |
days[i] = 0; | |
days[i+1] = 0; | |
i++; | |
++count; | |
} | |
i++; | |
} | |
return count; | |
} | |
bool storesClosed(vector<int> o, vector<int> c) | |
{ | |
bool closedDaysFound = false; | |
for(int i =0; i< o.size(); i++) { | |
if(o[i] <= 2400 || c[i] <= 2400 || o[i] > c[i]) { | |
closedDaysFound = true; | |
days[i] = -1; | |
} | |
} | |
return closedDaysFound; | |
} | |
void prettyPrint(vector<int> o, vector<int> c) | |
{ | |
// Initialize days | |
initDays(o); | |
int offset = consecutiveDays(o, c, 0); | |
// Do we have at least 1 set of consecutive days ? | |
if(offset) { | |
for(int i = 0; i < days.size(); i++) { | |
if(days[i] == 0 && days[i+1] == 0) { | |
cout << namedDays[i] << "-" << namedDays[i+ offset] << ": " << o[i] << "-" << c[i] << endl; | |
i += offset; | |
} else if(days[i] == -1) { | |
cout << namedDays[i] << "closed" << endl; | |
} else { | |
cout << namedDays[i] << ": " << o[i] << "-" << c[i] << endl; | |
} | |
} | |
} else { | |
// Are there any days when the store remains closed ? | |
if(storesClosed(o, c)) { | |
for(int i = 0; i < days.size(); i++) { | |
if(days[i] != -1) { | |
cout << namedDays[i] << ": " << o[i] << "-" << c[i] << endl; | |
} else { | |
cout << namedDays[i] << ": closed" << endl; | |
} | |
} | |
} else { | |
for(int i = 0; i < days.size(); i++) { | |
cout << namedDays[i] << ": " << o[i] << "-" << c[i] << endl; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment