-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
76 lines (55 loc) · 1.98 KB
/
helpers.cpp
File metadata and controls
76 lines (55 loc) · 1.98 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
#include "helpers.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <ctype.h>
#include <vector>
#include <fstream>
#include <math.h>
#include <iostream>
void helpers::exitIf(const bool cond, const std::string msg) {
if (cond) {
printf("%s\n", msg.c_str());
exit(-1);
}
}
void helpers::exitIfInvalidArgs(const int argc, const char *argv[]) {
helpers::exitIf(argc != 4, "[USAGE]: ./tema1 <NR_MAPPERS> <NR_REDUCERS> <TEST_FILE_PATH>");
helpers::exitIf(!helpers::isPosInt(argv[1]), "[ERROR]: NR_MAPPERS must be a positive int.");
helpers::exitIf(!helpers::isPosInt(argv[2]), "[ERROR]: NR_REDUCERS must be a positive int.");
}
bool helpers::isPosInt(const std::string nrStr) {
return (std::all_of(nrStr.begin(), nrStr.end(), [](char c){return isdigit(c);}) && !nrStr.empty());
}
std::vector<std::string> helpers::getFilePaths(const std::string filePath) {
std::ifstream inputFile(filePath);
exitIf(inputFile.fail(), "[ERROR]: " + filePath + " file path doesn't exist.");
std::string lineRead;
getline(inputFile, lineRead);
exitIf(!isPosInt(lineRead), "[ERROR]: First line of input file must be a positive int.");
const unsigned long nrLines = std::stol(lineRead);
std::vector<std::string> filePaths;
for (unsigned long i = 0; i < nrLines; i++) {
getline(inputFile, lineRead);
filePaths.push_back(lineRead);
}
return filePaths;
}
bool helpers::isPerfectNumber(const unsigned long long int n, const unsigned int exp) {
// base cases
if (n == 1) return true;
if (n == 0) return false;
if (std::pow(2, exp) == n) return true;
unsigned long long int low = 2;
unsigned long long int high = n / exp;
unsigned long long int mid;
while (high - low > 1) {
mid = (low + high) / 2;
auto value = std::pow(mid, exp);
if (value == n) return true;
if (value > n) high = mid;
else low = mid;
}
return false;
}