-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleAlgsFunctions.cpp
175 lines (150 loc) · 2.85 KB
/
simpleAlgsFunctions.cpp
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
#include "stdafx.h"
#include "simpleAlgsFunctions.h"
#include <vector>
#include <ctime>
#include <algorithm>
#include <regex>
using namespace std;
string capEachWrd(string str)
{
if (isalpha(str[0]))
{
str[0] = toupper(str[0]);
}
for (auto i = 1; i < str.length(); i++)
{
if (isalpha(str[i]) && str[i - 1] == ' ')
{
str[i] = toupper(str[i]);
}
}
return str;
}
int longestWord(string str)
{
vector<string> wrds = splitString(str, ' ');
int largeWrdLength = wrds[0].length();
for (auto i = 1; i < wrds.size(); i++) {
largeWrdLength = (largeWrdLength < wrds[i].length()) ? wrds[i].length() : largeWrdLength;
}
return largeWrdLength;
}
vector<string> splitString(string str, string spltr)
{
vector<string> vs;
string temp = "";
for (auto i = 0; i < str.length(); i++)
{
if (str[i] == spltr[0])
{
if (str.substr(i, spltr.length()) == spltr)
{
vs.push_back(temp);
temp = "";
i += spltr.length() - 1;
}
}
else
{
temp.push_back(str[i]);
}
}
vs.push_back(temp);
return vs;
}
vector<string> splitString(string str, char spltr)
{
vector<string> vs;
string temp = "";
for (auto i = 0; i < str.length(); i++)
{
if (str[i] == spltr)
{
vs.push_back(temp);
temp = "";
}
else
{
temp.push_back(str[i]);
}
}
vs.push_back(temp);
return vs;
}
bool palindrome(string str)
{
regex format("[[:digit:]]|[[:space:]]|[[:punct:]]");
transform(str.begin(), str.end(), str.begin(), ::tolower);
str = regex_replace(str, format, "");
return str == reverse(str);
}
std::vector<int> dataGen(int howMany, int range, bool shouldSort)
{
srand(time(nullptr));
std::vector<int> manyNums;
for (int i = 0; i<howMany; i++)
{
manyNums.push_back(rand() % range + 1);
}
if (shouldSort) sort(begin(manyNums), end(manyNums));
return manyNums;
}
std::vector<int> dataGen(int howMany, int range)
{
srand(time(nullptr));
std::vector<int> manyNums;
for (int i = 0; i<howMany; i++)
{
manyNums.push_back(rand() % range + 1);
}
return manyNums;
}
std::vector<int> dataGen(int howMany)
{
srand(time(nullptr));
std::vector<int> manyNums;
for (int i = 0; i<howMany; i++)
{
manyNums.push_back(rand() % 100 + 1);
}
return manyNums;
}
std::vector<int> dataGen()
{
srand(time(nullptr));
std::vector<int> manyNums;
for (int i = 0; i<35; i++)
{
manyNums.push_back(rand() % 100 + 1);
}
return manyNums;
}
std::string reverse(std::string str)
{
std::string reverse = "";
for (unsigned int i = 0; i < str.length(); i++)
{
reverse += str[(str.length() - 1) - i];
}
return reverse;
}
int reverse(int num)
{
int reverse = 0;
while (num != 0) {
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
return reverse;
}
int factorialize(int num) {
if (num == 1) {
return 1;
}
auto factorial = 1;
for (auto i = 2; i <= num; i++) {
factorial = factorial * i;
}
return factorial;
}