-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.cpp
More file actions
182 lines (160 loc) · 3.07 KB
/
Recursion.cpp
File metadata and controls
182 lines (160 loc) · 3.07 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
using namespace std;
void printArray(int n, int cnt = 0)
{
if (cnt == n) {
std::cout << cnt;
return;
}
std::cout << cnt << ", ";
return printArray(n, ++cnt);
}
void printPattern(int n, int lim = 2147)
{
if (lim == 2147) {
lim = n;
}
if (lim <= 0)
{
return;
}
cout << n << " ";
if (n > 0) {
printPattern(n - 5, lim);
}
else return;
if (n == lim) {
cout << n;
}
else {
cout << n << " ";
}
}
int findMax(int* arr, int length)
{
static int max = arr[length - 1];
if (length <= 0)
{
int temp = max;
max = 0;
return temp;
}
if (max < arr[length - 1]) {
max = arr[length - 1];
}
return findMax(arr, length - 1);
}
#include <string>
bool isPalindrome(const string& str, size_t i = 0, size_t spaceL = 0, size_t spaceR = 0)
{
if (str[i + spaceL] == ' ') {
spaceL++;
}
if (str[str.length() - i - spaceR - 1] == ' ') {
spaceR++;
}
if (i == str.length() / 2) {
return (str[i + spaceL] == str[str.length() - i - spaceR - 1]);
}
else if (str[i + spaceL] == str[str.length() - i - 1 - spaceR]) {
return isPalindrome(str, i + 1, spaceL, spaceR);
}
else return false;
}
int findGCD(int a, int b)
{
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (a == 0 || b == 0) {
return (a == 0) ? b : a;
}
return findGCD(b, a % b);
}
void printHailstone(int number)
{
if (number == 1) {
cout << number;
return;
}
cout << number << " ";
if (number % 2 == 0) {
printHailstone(number / 2);
}
else {
printHailstone(number * 3 + 1);
}
}
/*
int myArrayToInt(char* str, int n, int cnt = 0)
{
static int res = 0;
if (n == 0) return res;
res += str[cnt] * pow(10, --n);
myArrayToInt(str, n, cnt++);
return 0;
}
*/
int findLCM(int a, int b)
{
return (a * b) / findGCD(a, b);
}
int mininumBracketAdd(const string& s, int openCnt = 0, size_t idx = 0, int minAdd = 0)
{
if (idx == s.length()) return minAdd + openCnt;
if (s[idx] == '(') {
openCnt++;
}
else if (s[idx] == ')')
{
if (openCnt == 0) {
minAdd++;
}
else {
openCnt--;
}
}
return mininumBracketAdd(s, openCnt, ++idx, minAdd);
}
string reverseSentence(string s)
{
if (s.find(" ") == string::npos)
return s + " ";
return reverseSentence(s.substr(s.find(" ") + 1)) + s.substr(0, s.find(" ") + 1);
}
int strLen(char* str)
{
if (str[0] == 0) return 0;
return 1 + strLen(str + 1);
}
size_t findDigit(const string& s, size_t idx = 0)
{
if (isdigit(s[idx]) || idx == s.size() - 1) {
return idx;
}
findDigit(s, ++idx);
}
string repeater(const string& s, size_t repeatCnt)
{
if (repeatCnt == 0) return "";
if (repeatCnt == 1) return s;
return s + repeater(s, --repeatCnt);
}
string expandHelp(const string& s, int repeat)
{
size_t open = s.find("(");
string temp = s.substr(open);
size_t close = open + temp.rfind(")");
if (open == string::npos) {
return repeater(s, repeat);
}
return repeater(s.substr(0, (open - 1 < 0) ? 0 : open - 1) +
expandHelp(s.substr(open + 1, close - open - 1), s[open - 1] - '0') +
((close < s.size() - 1) ? expandHelp(s.substr(close + 1), 1) : 0), repeat);
}
string expand(string s)
{
return expandHelp(s, 1);
}