-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
144 lines (120 loc) · 2.87 KB
/
utils.cpp
File metadata and controls
144 lines (120 loc) · 2.87 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
#include <string>
#include <cmath>
#include <iostream>
#include <iterator>
#include <vector>
#include <array>
#include <iostream>
using namespace std;
bool isPerfectSquare(double x)
{
float sr = sqrt(x);
return ((sr - floor(sr)) == 0);
}
// put an array as a c++ argument , use foreach
void forEachLoop(int (&charCount)[26], int k)
{
for (int &c : charCount)
{
float repeated = c / k;
if (c % k)
{
return;
}
}
}
// loop string to get each character
void loopString()
{
string inputString;
int charCount[26];
// get each character decimal to be count of repeated
for (size_t i = 0; i < inputString.length(); ++i)
{
charCount[inputString[i] - 'a'] += 1;
}
}
// loop vector
void loopVector()
{
vector<int> days;
for (auto i = days.begin(); i != days.end(); ++i)
cout << *i << ' ';
}
// loop array
void loopArray()
{
array<int, 5> v;
for (std::size_t i = 0; i != v.size(); ++i)
{
// access element as v[i]
// any code including continue, break, return
}
}
void defineAndUseArrayOfChars()
{
char roadString[5];
scanf("%s", roadString);
}
// read empty lines empty string read array of chars => used in parenthesee balance problem
void readEmptyLinesInCharsArray()
{
char rawChars[129];
int numOfLines = 0;
scanf("%d", &numOfLines);
// https://stackoverflow.com/questions/25597732/read-empty-lines-c
// you have to read it twice
cin.getline(rawChars, 129); // reads the rest of the line that the number was on
cin.getline(rawChars, 129); // reads the blank line
}
// multiple way to seperate any iterators
//example : 1, 2, 3, 4, 5
void sepAnyIterator()
{
// way 1
vector<int> discarded;
auto it = discarded.begin();
if (it != discarded.end())
{
std::cout << *it;
++it;
}
for (; it != discarded.end(); ++it)
{
cout << ", " << *it;
}
// way 2
array<int, 3> data = {1, 2, 3};
ostream_iterator<int> out(cout, ", ");
copy(data.begin(), data.end(), out);
cout << '\n';
}
// return actual string length from array of char or a string
void StrLen(value)
{
strlen(value);
}
// this finds all possible sorting of string in algoritm lib
void allPossibleSotring()
{
next_permutation(sentence.begin(), sentence.end());
}
// find number of occurence to in a string
void findNumOfOccurencesInString()
{
string rawString;
string periodStr = "findme";
int cou = 0;
signed int foundIndex = 0;
// check if the period string repeated till the end of the rawstring
// find number of occurences in a string
while (foundIndex != string::npos)
{
foundIndex = rawString.find(periodStr, foundIndex);
if (foundIndex != string::npos)
{
foundIndex += periodStr.length();
cou++;
}
}
}