207 lines
5.6 KiB
C++
207 lines
5.6 KiB
C++
//
|
|
// Created by caleb on 3/6/24.
|
|
//
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <random>
|
|
#include <exception>
|
|
#include <climits>
|
|
#include <cstdlib>
|
|
#include <map>
|
|
#include <algorithm>
|
|
|
|
using namespace std::literals::string_literals; // import std::string literals
|
|
#include "binarySearch.h"
|
|
#include "colormap.h"
|
|
|
|
class OutOfBoundsException: public std::exception {
|
|
virtual const char* what() const throw()
|
|
{
|
|
return "Method attempted to call a vector location that was larger than the vector itself.";
|
|
}
|
|
} OutOfBoundsException;
|
|
|
|
int getTotal(std::vector<int> arr) {
|
|
int sum = 0;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
sum += arr[i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
int getTotal(std::vector<std::vector<int>> arr) {
|
|
int sum = 0;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
sum += getTotal(arr[i]);
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
int getAverage(std::vector<int> arr) {
|
|
return getTotal(arr) / arr.size(); // NOLINT(*-narrowing-conversions)
|
|
}
|
|
|
|
int getAverage(std::vector<std::vector<int>> arr) {
|
|
return getTotal(arr) / arr.size();
|
|
}
|
|
|
|
int getRowTotal(std::vector<std::vector<int>> arr, int desiredRow) {
|
|
// validate
|
|
if (desiredRow > arr[desiredRow].size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int sum = 0;
|
|
for (int i = 0; i < arr[desiredRow].size(); ++i) {
|
|
sum += arr[desiredRow][i];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
int getColumnTotal(std::vector<std::vector<int>> arr, int desiredColumn) {
|
|
// validate
|
|
if (desiredColumn > arr.size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int sum = 0;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
sum += arr[i][desiredColumn];
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
int getHighestInRow(std::vector<std::vector<int>> arr, int desiredRow) {
|
|
// validate
|
|
if (desiredRow > arr[desiredRow].size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int highest = INT_MIN;
|
|
for (int i = 0; i < arr[desiredRow].size(); ++i) {
|
|
if (arr[desiredRow][i] > highest) {
|
|
highest = arr[desiredRow][i];
|
|
}
|
|
}
|
|
return highest;
|
|
}
|
|
|
|
int getLowestInRow(std::vector<std::vector<int>> arr, int desiredRow) {
|
|
// validate
|
|
if (desiredRow > arr[desiredRow].size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int lowest = INT_MAX;
|
|
for (int i = 0; i < arr[desiredRow].size(); ++i) {
|
|
if (arr[desiredRow][i] < lowest) {
|
|
lowest = arr[desiredRow][i];
|
|
}
|
|
}
|
|
return lowest;
|
|
}
|
|
|
|
int getHighestInColumn(std::vector<std::vector<int>> arr, int desiredColumn) {
|
|
// validate
|
|
if (desiredColumn > arr.size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int highest = INT_MIN;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
if (arr[i][desiredColumn] > highest) {
|
|
highest = arr[i][desiredColumn];
|
|
}
|
|
}
|
|
return highest;
|
|
}
|
|
|
|
int getLowestInColumn(std::vector<std::vector<int>> arr, int desiredColumn) {
|
|
// validate
|
|
if (desiredColumn > arr.size()) {
|
|
throw OutOfBoundsException;
|
|
}
|
|
int lowest = INT_MAX;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
if (arr[i][desiredColumn] < lowest) {
|
|
lowest = arr[i][desiredColumn];
|
|
}
|
|
}
|
|
return lowest;
|
|
}
|
|
|
|
std::vector<int> getRowMap(std::vector<std::tuple<int, int>> locationVector, int row) {
|
|
std::vector<int> rowMap;
|
|
|
|
for (const auto& tuple : locationVector) {
|
|
int tupleRow = std::get<0>(tuple); // Get the first element of the tuple
|
|
int tupleValue = std::get<1>(tuple); // Get the second element of the tuple
|
|
|
|
if (tupleRow == row) {
|
|
rowMap.push_back(tupleValue); // Add the tuple value to the rowMap
|
|
}
|
|
}
|
|
|
|
return rowMap;
|
|
}
|
|
|
|
std::string printVector(std::vector<int> arr, std::vector<int> locationVector = {-1}) {
|
|
std::string output = "{";
|
|
std::map<std::string, int> columnMap;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
// check if iteration value matches one of the locationVector values. If it does, append an ASCII color sequence.
|
|
int binSearchResult = binarySearch(locationVector, i);
|
|
//printf("bin search result:%i", binSearchResult);
|
|
if (binSearchResult > -1) {
|
|
output.append(colorIterator());
|
|
}
|
|
output.append(std::to_string(arr[i]));
|
|
if ((arr.size() - 1) > i) {
|
|
output.append(", ");
|
|
}
|
|
if (binSearchResult > -1) {
|
|
|
|
output.append(COLOR_WHITE.c_str());
|
|
}
|
|
}
|
|
output.append("}");
|
|
return output;
|
|
}
|
|
|
|
std::string printVector(std::vector<std::vector<int>> arr, std::vector<std::tuple<int, int>> locationVector = {{-1, -1}}) {
|
|
std::string output = "{\n";
|
|
std::tuple highlightTuple;
|
|
for (int i = 0; i < arr.size(); ++i) {
|
|
std::vector<int> rowVector = getRowMap(locationVector, i);
|
|
output.append(printVector(arr[i], rowVector));
|
|
// print ",\n" if at the end of the loop
|
|
if ((arr.size() - 1) > i) {
|
|
output.append(",\n");
|
|
}
|
|
}
|
|
output.append("\n}");
|
|
return output;
|
|
}
|
|
|
|
std::vector<std::vector<int>> random2DArray(int x, int y) {
|
|
std::vector<std::vector<int>> array(x, std::vector<int>(y, 0));
|
|
// setup random number generation
|
|
srand((unsigned) time(NULL));
|
|
|
|
for (int i = 0; i < x; ++i) {
|
|
for (int j = 0; j < y; ++j) {
|
|
array[i][j] = rand() / 1000;
|
|
}
|
|
}
|
|
return array;
|
|
}
|
|
|
|
std::vector<std::tuple<int, int>> highlightEveryOther(int x, int y) {
|
|
std::vector<std::tuple<int, int>> returnVector;
|
|
for (int i = 0; i < x; ++i) {
|
|
for (int j = 0; j < y; ++j) {
|
|
if ((j + i) % 2 == 0) {
|
|
//printf("(%i, %i), ", i, j);
|
|
returnVector.push_back({i, j});
|
|
}
|
|
}
|
|
}
|
|
printf("\n");
|
|
return returnVector;
|
|
}
|