Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Artemis: 'title': 'Fix: Simplify vector sorting using std::sort' #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "primes.h"

/** @brief Checks if a number is prime
*
* @param n Number to check
* @return True if n is prime, false otherwise
*/
bool
Primes::IsPrime(int n) {
bool Primes::IsPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < n; i += 1) {
if (n % i == 0) {
if (n <= 3) {
return true;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
Expand Down Expand Up @@ -51,4 +51,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}
31 changes: 14 additions & 17 deletions src/control/double.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unordered_map>
#include "double.h"

/**
Expand All @@ -24,14 +25,12 @@ DoubleForLoop::SumSquare(int n) {
*
* @param n
* @return the sum of all triangle numbers from T(1) to T(n)
*/
*/
long
DoubleForLoop::SumTriangle(int n) {
long sum = 0;
for (int i = 0; i < n + 1; i += 1) {
for (int j = 0; j < i; j += 1) {
sum = sum + (long) j;
}
for (int i = 1; i <= n; ++i) {
sum += (long)i * (i - 1) / 2;
}
return sum;
}
Expand All @@ -44,22 +43,20 @@ DoubleForLoop::SumTriangle(int n) {
*
* @param v
* @return the number of pairs in an vay
*/
*/
int
DoubleForLoop::CountPairs(std::vector<int> v) {
int count = 0;
std::unordered_map<int, int> counts;
for (int i = 0; i < (int) v.size(); i += 1) {
int nDuplicates = 0;
for (int j = 0; j < (int) v.size(); j += 1) {
if (v[i] == v[j]) {
nDuplicates += 1;
}
}
if (nDuplicates == 2) {
count += 1;
counts[v[i]]++;
}
int nPairs = 0;
for (auto it = counts.begin(); it != counts.end(); ++it) {
if (it->second == 2) {
nPairs++;
}
}
return count / 2;
return nPairs;
}

/**
Expand Down Expand Up @@ -98,4 +95,4 @@ DoubleForLoop::SumMatrix(std::vector<std::vector<int>> matrix) {
}
}
return sum;
}
}
20 changes: 4 additions & 16 deletions src/control/single.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,12 @@ SingleForLoop::MaxVector(std::vector<int> &arr) {
return max;
}

/**
* @brief Sums all values from 0 to n that are divisible by m
*
* @param n the upper bound (non-inclusive)
* @param m the modulus
* @return the sum of all values from 0 to n that are divisible by m
*/
int
SingleForLoop::SumModulus(int n, int m) {
int array[n];
int SingleForLoop::SumModulus(int n, int m) {
int sum = 0;
for (int i = 0; i < n; i += 1) {
array[i] = i;
}
for (int i = 0; i < n; i += 1) {
if (array[i] % m == 0) {
sum += array[i];
if (i % m == 0) {
sum += i;
}
}
return sum;
}
}
13 changes: 6 additions & 7 deletions src/datastructures/list.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "list.h"

/** @brief shuffles a list into a new list
Expand Down Expand Up @@ -28,14 +29,12 @@ OpsList::Shuffle(std::list<int> &l) {
* @param start the start index of the slice
* @param end the end index of the slice (exclusive)
* @return a new list with the elements of l sliced
*/
*/
std::list<int>
OpsList::Slice(std::list<int> &l, int start, int end) {
std::list<int> ret;
for (int i = start; i < end; i++) {
std::list<int>::iterator it = l.begin();
std::advance(it, i);
ret.push_back(*it);
}
auto it = std::next(l.begin(), start);
auto end_it = std::next(l.begin(), end);
ret.insert(ret.end(), it, end_it);
return ret;
}
}
20 changes: 6 additions & 14 deletions src/datastructures/vector.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "vector.h"

#include <iostream>
Expand Down Expand Up @@ -77,21 +78,12 @@ OpsVector::SearchVector(std::vector<int> &v, int n) {
*
* @param v Vector to sort.
* @return The sorted vector.
*/
*/
std::vector<int>
OpsVector::SortVector(std::vector<int> &v) {
std::vector<int> ret(v);

for (int i = 0; i < (int) ret.size(); i += 1) {
for (int j = 0; j < (int) ret.size() - 1; j += 1) {
if (ret[j] > ret[j + 1]) {
int temp = ret[j];
ret[j] = ret[j + 1];
ret[j + 1] = temp;
}
}
}
return ret;
std::vector<int> ret(v);
std::sort(ret.begin(), ret.end());
return ret;
}

/**
Expand Down Expand Up @@ -149,4 +141,4 @@ OpsVector::MergeVectors(std::vector<int> &v1, std::vector<int> &v2) {
}

return ret;
}
}