Back
Close

merge_adjacent

joboc
2,189 views
#include <iostream>
#include <vector>
/////////// ALGORITHM TO IMPLEMENT ////////////////////
template <typename ForwardIterator, typename OutputIterator, typename Equal, typename Merge>
void merge_adjacent(ForwardIterator first, ForwardIterator last, OutputIterator out, Equal equal, Merge merge)
{
// Write your code here, and make the tests pass!
}
////////////////////////////////////////////////////////
/////////// SALE CLASS /////////////////////////////////
class Sale
{
public:
Sale(int day, double amount) : day_(day), amount_(amount) {}
int getDate() const { return day_; }
double getAmount() const { return amount_; }
private:
int day_;
double amount_;
};
bool operator==(Sale const& sale1, Sale const& sale2)
{
return sale1.getDate() == sale2.getDate() && sale1.getAmount() == sale2.getAmount();
}
bool sameDate(Sale const& sale1, Sale const& sale2)
{
return sale1.getDate() == sale2.getDate();
}
Sale mergeSales(Sale const& sale1, Sale const& sale2)
{
if (sale1.getDate() != sale2.getDate()) throw "Error: cannot add sales with different dates";
return Sale(sale1.getDate(), sale1.getAmount() + sale2.getAmount());
}
////////////////////////////////////////////////////////
/////////// TEST CODE: MAKE IT PASS! ///////////////////
std::vector<Sale> aggregateDailySales(std::vector<Sale> const& sales)
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants