Compare commits

...

6 Commits

Author SHA1 Message Date
y-syo
e43a18b091 🔨」 fix: fixed some things. 2025-09-03 17:07:03 +02:00
y-syo
a56d1ee540 🗑️」 clean(ex00): cleaned up data files 2025-09-01 16:37:27 +02:00
y-syo
40ea42fe7b 🗑️」 clean(ex00): cleaned debug 2025-09-01 16:35:02 +02:00
y-syo
f9c38cfe86 🗑️」 clean: cleanup things and slightly improved ex02/main.cpp 2025-09-01 16:00:41 +02:00
y-syo
7b47994ddd 🔨」 fix(ex00): fixed date parsing to be more precise :D 2025-09-01 14:49:08 +02:00
f41fe7846d 🏗️」 wip(ex02): algo is basically done now i think ? just need some optimisations iirc 2025-08-30 16:23:35 +02:00
8 changed files with 68 additions and 1682 deletions

View File

@@ -13,7 +13,10 @@ Date::Date(void)
Date::Date(std::string str)
{
// check format (4 char & isdigit into dash into 2 char isdigit & < 1 && isdigit & < 2, and same for day except ahah funny cuz 30 and 31 and 28 and 29 agfjasfgfjahkfga)
for (size_t i = 0; str[i]; ++i)
if (!isdigit(str[i]) && !((i == (str.length() - 6) || i == (str.length() - 3)) && str[i] == '-'))
throw std::invalid_argument("invalid date.");
_value = 0;
for (std::string::iterator it = str.begin(); it < str.end(); ++it)
{
@@ -22,7 +25,6 @@ Date::Date(std::string str)
_value *= 10;
_value += *it - '0';
}
int year = _value / 10000;
int month = (_value / 100) % 100;
int day = _value % 100;
@@ -156,7 +158,7 @@ void BitcoinExchange::str(std::string filename)
std::cout << "Error: bad input => " << line << std::endl;
continue ;
}
if (std::atol(value_str.c_str()) > 2147483647)
if (std::atol(value_str.c_str()) > 1000)
{
std::cout << "Error: too large a number." << std::endl;
continue ;
@@ -167,7 +169,7 @@ void BitcoinExchange::str(std::string filename)
continue ;
}
try {
Date date(date_str);
Date date(trim(date_str));
if (date < _data.begin()->first)
{
std::cout << "Error: input date too low." << std::endl;

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +0,0 @@
date | value
1971-01-02 | 5
1971-31-02 | 5
2011-01-03 | 3
2011-01-03 | 2
2011-01-03 | 1
2011-01-03 | 1.2
2011-01-09 | 1
2012-01-11 | -1
2001-42-42
2012-01-11 | 1
2012-01-11 | 2147483648

View File

@@ -15,10 +15,10 @@ typedef int (*OperatorFunctions)(int, int);
void rpn(char *av)
{
OperatorFunctions calc[128] = { NULL };
calc['+'] = &add;
calc['-'] = &subst;
calc['*'] = &mult;
calc['/'] = &div;
calc[(int) '+'] = &add;
calc[(int) '-'] = &subst;
calc[(int) '*'] = &mult;
calc[(int) '/'] = &div;
std::istringstream ss_input((std::string(av)));
std::stack<int> stack;

View File

@@ -6,7 +6,7 @@
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/30 13:38:12 by mmoussou ### ########.fr #
# Updated: 2025/09/01 15:45:57 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
@@ -27,7 +27,7 @@ INCLUDES = .
NAME = PmergeMe
SRCS = main.cpp #PmergeMe.cpp
SRCS = main.cpp PmergeMe.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))

23
ex02/PmergeMe.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "PmergeMe.hpp"
std::vector<size_t> generateJacobsthal(size_t n)
{
std::vector<size_t> seq;
if (n == 0)
return seq;
seq.push_back(0);
if (n == 1)
return seq;
seq.push_back(1);
size_t j0 = 0, j1 = 1;
while (true)
{
size_t jn = j1 + 2 * j0;
if (jn >= n)
break;
seq.push_back(jn);
j0 = j1;
j1 = jn;
}
return seq;
}

View File

@@ -11,42 +11,25 @@
#include <ctime>
#include <deque>
std::vector<size_t> generateJacobsthal(size_t n)
std::vector<size_t> generateJacobsthal(size_t n);
template <typename Container, typename T>
typename Container::iterator binaryInsertPosition(Container &c, const T &value)
{
std::vector<size_t> seq;
if (n == 0)
return seq;
seq.push_back(0);
if (n == 1)
return seq;
seq.push_back(1);
size_t j0 = 0, j1 = 1;
while (true)
typename Container::iterator first = c.begin();
typename Container::iterator last = c.end();
while (first < last)
{
size_t jn = j1 + 2 * j0;
if (jn >= n)
break;
seq.push_back(jn);
j0 = j1;
j1 = jn;
typename Container::iterator mid = first + (last - first) / 2;
if (*mid < value)
first = mid + 1;
else
last = mid;
}
return seq;
return first;
}
template <typename T>
struct Pair {
T first;
T second;
};
template <typename T>
struct PairComp {
bool operator()(const Pair<T> &x, const Pair<T> &y) const
{
return x.second < y.second;
}
};
template <typename Container>
Container fordJohnsonSort(const Container &input)
{
@@ -56,7 +39,7 @@ Container fordJohnsonSort(const Container &input)
if (input.size() <= 1)
return input;
std::vector<Pair<T> > pairs;
std::vector<std::pair<T, T> > pairs;
size_t i = 0;
for (; i + 1 < input.size(); i += 2)
{
@@ -64,16 +47,16 @@ Container fordJohnsonSort(const Container &input)
T b = input[i+1];
if (a > b)
std::swap(a, b);
Pair<T> p;
p.first = a;
p.second = b;
pairs.push_back(p);
pairs.push_back(std::make_pair(a, b));
}
std::sort(pairs.begin(), pairs.end(), PairComp<T>());
Container larger;
for (size_t j = 0; j < pairs.size(); ++j)
larger.push_back(pairs[j].second);
for (size_t k = 0; k < pairs.size(); ++k)
result.push_back(pairs[k].second);
larger = fordJohnsonSort(larger);
result = larger;
std::vector<size_t> order = generateJacobsthal(pairs.size());
std::vector<bool> inserted(pairs.size(), false);
@@ -83,8 +66,7 @@ Container fordJohnsonSort(const Container &input)
size_t j = order[idx];
if (j < pairs.size() && !inserted[j])
{
typename Container::iterator pos = std::lower_bound(
result.begin(), result.end(), pairs[j].first);
typename Container::iterator pos = binaryInsertPosition(result, pairs[j].first);
result.insert(pos, pairs[j].first);
inserted[j] = true;
}
@@ -94,8 +76,7 @@ Container fordJohnsonSort(const Container &input)
{
if (!inserted[j])
{
typename Container::iterator pos = std::lower_bound(
result.begin(), result.end(), pairs[j].first);
typename Container::iterator pos = binaryInsertPosition(result, pairs[j].first);
result.insert(pos, pairs[j].first);
}
}
@@ -103,7 +84,7 @@ Container fordJohnsonSort(const Container &input)
if (i < input.size())
{
T leftover = input[i];
typename Container::iterator pos = std::lower_bound(result.begin(), result.end(), leftover);
typename Container::iterator pos = binaryInsertPosition(result, leftover);
result.insert(pos, leftover);
}

View File

@@ -43,7 +43,7 @@ int main(int argc, char **argv)
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
std::cout << "[...]";
std::cout << "[...] (" << vec.size() << ")";
std::cout << std::endl;
clock_t startVec = clock();
@@ -58,9 +58,14 @@ int main(int argc, char **argv)
for (size_t i = 0; i < vec.size() && i < 6; i++)
std::cout << vec[i] << " ";
if (vec.size() > 6)
std::cout << "[...]";
std::cout << "[...] (" << vec.size() << ")";
std::cout << std::endl;
std::vector<int> sortedVec = vec;
std::sort(sortedVec.begin(), sortedVec.end());
if (vec != sortedVec)
throw std::runtime_error("it's not sorted, what-");
double vecTime = (double)(endVec - startVec) / CLOCKS_PER_SEC * 1000000.0;
double deqTime = (double)(endDeq - startDeq) / CLOCKS_PER_SEC * 1000000.0;