Skip to content

Commit a2175fb

Browse files
committed
TCO 2014 Round 1C
1 parent 864de4d commit a2175fb

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

FizzBuzzTurbo.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
using namespace std;
8+
9+
class FizzBuzzTurbo
10+
{
11+
public:
12+
vector<long long> counts(long long A, long long B)
13+
{
14+
long long a = B/3 - A/3;
15+
long long b = B/5 - A/5;
16+
long long c = B/15 - A/15;
17+
if(A%3 == 0) a++;
18+
if(A%5 == 0) b++;
19+
if(A%15 == 0) c++;
20+
long long fb = c, fz = a - c, bz = b - c;
21+
vector<long long> v;
22+
v.push_back(fz);
23+
v.push_back(bz);
24+
v.push_back(fb);
25+
return v;
26+
}
27+
28+
// BEGIN CUT HERE
29+
public:
30+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
31+
private:
32+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
33+
void verify_case(int Case, const vector<long long> &Expected, const vector<long long> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
34+
void test_case_0() { long long Arg0 = 1LL; long long Arg1 = 4LL; long Arr2[] = {1, 0, 0 }; vector<long long> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(0, Arg2, counts(Arg0, Arg1)); }
35+
void test_case_1() { long long Arg0 = 2LL; long long Arg1 = 6LL; long Arr2[] = {2, 1, 0 }; vector<long long> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(1, Arg2, counts(Arg0, Arg1)); }
36+
void test_case_2() { long long Arg0 = 150LL; long long Arg1 = 165LL; long Arr2[] = {4, 2, 2 }; vector<long long> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(2, Arg2, counts(Arg0, Arg1)); }
37+
void test_case_3() { long long Arg0 = 474747LL; long long Arg1 = 747474LL; long Arr2[] = {72728, 36363, 18182 }; vector<long long> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); verify_case(3, Arg2, counts(Arg0, Arg1)); }
38+
39+
// END CUT HERE
40+
41+
};
42+
43+
// BEGIN CUT HERE
44+
int main()
45+
{
46+
FizzBuzzTurbo ___test;
47+
___test.run_test(-1);
48+
}
49+
// END CUT HERE

FizzBuzzTurbo.txt

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
PROBLEM STATEMENT
2+
3+
Fizz Buzz is a simple game used to teach kids about divisibility.
4+
The goal of the game is to say positive integers in increasing order, with a twist:
5+
You don't say the numbers divisible by 3 and 5.
6+
Instead, whenever a number was divisible by 3 you say "fizz" and for a number divisible by 5 you say "buzz".
7+
(Thus, if a number was divisible by 15, you say "fizzbuzz".)
8+
9+
10+
11+
Here is how the game starts: 1, 2, fizz, 4, buzz, fizz, 7, 8, fizz, buzz, 11, fizz, 13, 14, fizzbuzz, 16, 17, fizz, 19, buzz, fizz, 22, 23, fizz, buzz, 26, fizz, 28, 29, fizzbuzz, 31, 32, fizz, 34, buzz, fizz, ...
12+
13+
14+
15+
Fizz Buzz has also become a traditional programming interview question.
16+
However, in this problem we have a more tricky assignment for you.
17+
18+
19+
20+
You are given long longs A and B.
21+
Consider the part of the game that corresponds to integers from A to B, inclusive.
22+
During this part of the game, you will say "fizz" X times, "buzz" Y times, and "fizzbuzz" Z times.
23+
Return a vector<long long> with three elements: {X,Y,Z}.
24+
25+
26+
DEFINITION
27+
Class:FizzBuzzTurbo
28+
Method:counts
29+
Parameters:long long, long long
30+
Returns:vector<long long>
31+
Method signature:vector<long long> counts(long long A, long long B)
32+
33+
34+
NOTES
35+
-The return value can be quite large. Make sure to use the appropriate data type.
36+
37+
38+
CONSTRAINTS
39+
-A will be between 1 and 10^18, inclusive.
40+
-B will be between A and 10^18, inclusive.
41+
42+
43+
EXAMPLES
44+
45+
0)
46+
1
47+
4
48+
49+
Returns: {1, 0, 0 }
50+
51+
This is the sequence "1, 2, fizz, 4".
52+
53+
1)
54+
2
55+
6
56+
57+
Returns: {2, 1, 0 }
58+
59+
This is the sequence "2, fizz, 4, buzz, fizz".
60+
61+
2)
62+
150
63+
165
64+
65+
Returns: {4, 2, 2 }
66+
67+
This sequence begins and ends with a "fizzbuzz". There are some "fizz"es and some "buzz"es inbetween.
68+
69+
3)
70+
474747
71+
747474
72+
73+
Returns: {72728, 36363, 18182 }
74+
75+

Unique.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
using namespace std;
8+
9+
class Unique
10+
{
11+
public:
12+
string removeDuplicates(string S)
13+
{
14+
int c[1000];
15+
string ret;
16+
for(int i = 'a'; i <= 'z'; i++) c[i] = 0;
17+
for(int i = 0; i < S.size(); i++){
18+
if(c[S[i]] == 0){
19+
c[S[i]] = 1;
20+
ret += S[i];
21+
}
22+
}
23+
return ret;
24+
}
25+
26+
// BEGIN CUT HERE
27+
public:
28+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
29+
private:
30+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
31+
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
32+
void test_case_0() { string Arg0 = "banana"; string Arg1 = "ban"; verify_case(0, Arg1, removeDuplicates(Arg0)); }
33+
void test_case_1() { string Arg0 = "aardvark"; string Arg1 = "ardvk"; verify_case(1, Arg1, removeDuplicates(Arg0)); }
34+
void test_case_2() { string Arg0 = "xxxxx"; string Arg1 = "x"; verify_case(2, Arg1, removeDuplicates(Arg0)); }
35+
void test_case_3() { string Arg0 = "topcoder"; string Arg1 = "topcder"; verify_case(3, Arg1, removeDuplicates(Arg0)); }
36+
37+
// END CUT HERE
38+
39+
};
40+
41+
// BEGIN CUT HERE
42+
int main()
43+
{
44+
Unique ___test;
45+
___test.run_test(-1);
46+
}
47+
// END CUT HERE

Unique.txt

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
PROBLEM STATEMENT
2+
3+
You are given a string S of lowercase English letters.
4+
5+
6+
7+
Some of the letters may occur multiple times in S.
8+
For example, there are three 'a's and two 'n's in "banana".
9+
10+
11+
12+
You dislike duplicates.
13+
For each letter, you want to keep only its first occurrence and delete all the others.
14+
Return the string obtained from S by deleting the duplicates of each letter, as described above.
15+
16+
17+
DEFINITION
18+
Class:Unique
19+
Method:removeDuplicates
20+
Parameters:string
21+
Returns:string
22+
Method signature:string removeDuplicates(string S)
23+
24+
25+
CONSTRAINTS
26+
-S will contain between 1 and 1000 characters, inclusive.
27+
-Each character of S will be a lowercase English letter ('a'-'z').
28+
29+
30+
EXAMPLES
31+
32+
0)
33+
"banana"
34+
35+
Returns: "ban"
36+
37+
38+
39+
1)
40+
"aardvark"
41+
42+
Returns: "ardvk"
43+
44+
45+
46+
2)
47+
"xxxxx"
48+
49+
Returns: "x"
50+
51+
52+
53+
3)
54+
"topcoder"
55+
56+
Returns: "topcder"
57+
58+

0 commit comments

Comments
 (0)