Skip to content

Commit c4d3fd1

Browse files
committed
updated on 20 July, 2019
1 parent 8ba96ff commit c4d3fd1

File tree

13 files changed

+713
-0
lines changed

13 files changed

+713
-0
lines changed

CoderByte/coderByte1.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
6+
#define M 100000000
7+
bool marked[M];
8+
void sieve2(int n)
9+
{
10+
for (int i = 2; i < n; i++)
11+
{
12+
if (marked[i] == false) // i is a prime
13+
{
14+
for (int j = i + i; j <= n; j += i)
15+
{
16+
marked[j] = true;
17+
}
18+
}
19+
}
20+
}
21+
22+
string PrimeTime(int num) {
23+
24+
if (marked[num]){
25+
return "false";
26+
}else{
27+
return "true";
28+
}
29+
}
30+
31+
int main() {
32+
33+
// keep this function call here
34+
sieve2(20000000);
35+
cout << PrimeTime(gets(stdin));
36+
return 0;
37+
38+
}
39+
40+

CoderByte/coderbyte.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include<bits/stdc++.h>
2+
#include <iostream>
3+
#include <string>
4+
#include <sstream>
5+
#include <cstdlib>
6+
using namespace std;
7+
8+
string nTs(int i){
9+
string s="";
10+
char c,p;
11+
int m;
12+
p = 0;
13+
//cout<<"i "<<i<<endl;
14+
while(i !=0 ){
15+
m = i%10;
16+
c = m-p+'0';
17+
//cout<<"what "<<c<<endl;
18+
s += c;
19+
//cout<<s<<endl;
20+
i/=10;
21+
}
22+
//cout<<s<<endl;
23+
string j ="";
24+
int k;
25+
k=s.size()-1;
26+
// cout<<"k "<<k<<endl;
27+
for(i=0;i<s.size();i++){
28+
j += s[k];
29+
k--;
30+
}
31+
// cout<<j<<endl;
32+
return j;
33+
34+
}
35+
36+
string FizzBuzz(int num) {
37+
38+
// code goes here
39+
string s="";
40+
for(int i=1; i<=num; i++){
41+
42+
if(i%5==0 && i%3==0){
43+
s+= "FizzBuzz";
44+
}else if(i%3==0){
45+
s+= "Fizz";
46+
}else if(i%5== 0){
47+
s+= "Buzz";
48+
}else{
49+
50+
s+= nTs(i);
51+
//cout<<"what"<<nTs(i);
52+
}
53+
s+= " ";
54+
}
55+
return s;
56+
57+
}
58+
59+
int main() {
60+
61+
// keep this function call here
62+
cout << FizzBuzz(gets(stdin));
63+
return 0;
64+
65+
}

CoderByte/coderbyte3.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include<bits/stdc++.h>
2+
#include <iostream>
3+
#include <string>
4+
#include <vector>
5+
#include <cstdio>
6+
#include <cstdlib>
7+
using namespace std;
8+
9+
10+
static int foo(const std::string *array)
11+
{
12+
size_t i = 0;
13+
while (!array[i].empty())
14+
++i;
15+
return i;
16+
}
17+
18+
19+
vector<string> v;
20+
21+
22+
string LRUCache(string strArr[])
23+
{
24+
//cout<<strArr[1][0];
25+
int j=0,i=0,p=0;
26+
int is = 9;
27+
is = foo(strArr);
28+
29+
//cout<<is<<endl;
30+
for(j=0; j<is; j++)
31+
{
32+
string s="";
33+
s += strArr[j][0];
34+
//cout<<s;
35+
if(v.size() != 0){
36+
bool flag = false;
37+
for(i=0; i<v.size(); i++)
38+
{
39+
if(s == v.at(i))
40+
{
41+
flag = true;
42+
break;
43+
}
44+
45+
}
46+
if(flag){
47+
// cout<<v.at(i);
48+
v.erase(v.begin()+i);
49+
v.push_back(s);
50+
//cout<<"worked " <<s<<endl;
51+
/** for(p=0;p<v.size();p++){
52+
cout<<v.at(p)<<" ";
53+
}
54+
cout<<endl;
55+
***/
56+
}
57+
else
58+
{
59+
if(v.size()>=5)
60+
{
61+
v.erase(v.begin());
62+
}
63+
v.push_back(s);
64+
//cout<<"worked " <<s<<endl;
65+
/***
66+
for(p=0;p<v.size();p++){
67+
cout<<v.at(p)<<" ";
68+
}
69+
cout<<endl;
70+
***/
71+
72+
}
73+
}
74+
else{
75+
v.push_back(s);
76+
//cout<<"worked " <<s<<endl;
77+
/***
78+
for(p=0;p<v.size();p++){
79+
cout<<v.at(p)<<" ";
80+
}
81+
cout<<endl;
82+
***/
83+
}
84+
}
85+
86+
/** for(i=0;i<v.size();i++){
87+
// cout<<v.at(i)<<" ";
88+
}**/
89+
90+
string ans;
91+
ans = "";
92+
int l = v.size();
93+
for(i=0;i<l;i++){
94+
ans += v.at(i);
95+
//v.erase(v.begin()+i);
96+
if(i+1!=l)
97+
ans += '-';
98+
}
99+
100+
return ans;
101+
102+
}
103+
104+
105+
int main()
106+
{
107+
108+
// keep this function call here
109+
/* Note: In C++ you first have to initialize an array and set
110+
it equal to the stdin to test your code with arrays. */
111+
v.clear();
112+
string A[] = gets(stdin);
113+
cout << LRUCache(A);
114+
return 0;
115+
116+
}
117+
118+

CoderByte/coderbyte4.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
const int noOfChar = 256;
5+
string mySubStr(string str, string pat)
6+
{
7+
int len1 = str.length();
8+
int len2 = pat.length();
9+
10+
int my_hash[noOfChar] = {0};
11+
int my_str[noOfChar] = {0};
12+
13+
for (int i = 0; i < len2; i++)
14+
my_hash[pat[i]]++;
15+
16+
int start = 0, start_index = -1, min_len = INT_MAX;
17+
18+
19+
int cnt = 0;
20+
for (int j = 0; j < len1 ; j++)
21+
{
22+
my_str[str[j]]++;
23+
24+
if (my_hash[str[j]] != 0 &&
25+
my_str[str[j]] <= my_hash[str[j]] )
26+
cnt++;
27+
28+
29+
if (cnt == len2)
30+
{
31+
while ( my_str[str[start]] > my_hash[str[start]]
32+
|| my_hash[str[start]] == 0)
33+
{
34+
35+
if (my_str[str[start]] > my_hash[str[start]])
36+
my_str[str[start]]--;
37+
start++;
38+
}
39+
40+
int len_window = j - start + 1;
41+
if (min_len > len_window)
42+
{
43+
min_len = len_window;
44+
start_index = start;
45+
}
46+
}
47+
}
48+
49+
return str.substr(start_index, min_len);
50+
}
51+
52+
53+
string MinWindowSubstring(string strArr[]) {
54+
55+
// code goes here
56+
strArr[0] = mySubStr(strArr[0], strArr[1]);
57+
return strArr[0];
58+
59+
}
60+
61+
int main() {
62+
63+
// keep this function call here
64+
/* Note: In C++ you first have to initialize an array and set
65+
it equal to the stdin to test your code with arrays. */
66+
67+
string A[] = gets(stdin);
68+
cout << MinWindowSubstring(A);
69+
return 0;
70+
71+
}
72+
73+

0 commit comments

Comments
 (0)