-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseFizzBuzz.cpp
More file actions
64 lines (63 loc) · 1.62 KB
/
Copy pathreverseFizzBuzz.cpp
File metadata and controls
64 lines (63 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Program that given the classic fizzbuzz sequence, prints what range of
numbers the input sequence belongs to.
Below is a brute force implementation using recurrsion. We can optimize
this since the pattern itself repeats and we need not try to match for every
number in the sequence.
*/
#include <iostream>
#include <vector>
using namespace std;
//variable num starts with 3 as default
//TODO: the input array of strings could be invalid!
void printPatternRange(const vector<string>& input, int num=3) {
bool done = false;
bool anymatch = false; //did anything match so far
int pos = 0;
string curr;
int start;
while (!done) {
if(num%3 == 0 && num%5 == 0)
curr = "fizzbuzz";
else if(num%3 == 0)
curr = "fizz";
else if(num%5 == 0)
curr = "buzz";
else {
num = num+1;
if(num > 1000) {
cout << "Invalid input!" << endl;
break;
}
continue;
}
if(anymatch) {//we expect it to match
//else we need to recurr
if(curr.compare(input[pos]) != 0) {
printPatternRange(input, start+1);
done = true;
}
else { //check end condition
if(pos == input.size() - 1) {
done = true;
cout << "[" << start << "," << num << "]" << endl;
}
pos++;
}
}
else {
if(curr.compare(input[pos]) == 0) {
start = num;
pos++;
anymatch = true;
}
}
num = num+1;
}
}
int main() {
//const vector<string> input = {"buzz", "fizz", "fizzbuzz"};
//const vector<string> input = {"fizz", "fizz", "buzz"};
const vector<string> input = {"fizz", "buzz", "fizz", "fizzbuzz"};
printPatternRange(input);
}