Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Minimal Coin Output filter_list
Author
Message
Minimal Coin Output #1
I wrote this for a challenge on another forum. I thought it may be worth while sharing here too.

Here's my solution in C++:
Code:
#include <iostream>
#include <map>

bool keyComp(unsigned lch, unsigned rch) { return lch > rch; }

typedef std::pair < std::string, size_t > coin_def;
typedef std::map<size_t, coin_def, bool(*)(size_t, size_t)> coin_map;

const size_t TOONIE = 200;
const size_t LOONIE = 100;
const size_t QUARTER = 25;
const size_t DIME = 10;
const size_t NICKEL = 5;
const size_t PENNY = 1;

void CoinsRequired(double amt, coin_map& coinMap)
{
   size_t pennySum = amt * 100;
   for (const auto &it : coinMap)
   {
      size_t count = 0;
      while (pennySum >= count + it.first)
      {
         count += it.first;
         coinMap[it.first].second++;
      }
      pennySum -= count;
   }
}


int main(void)
{
   double amt = 2.37;
   coin_map coinMap(keyComp);
   coinMap.insert(std::pair < size_t, coin_def >(QUARTER, coin_def("Quarter(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(DIME, coin_def("Dime(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(NICKEL, coin_def("Nickel(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(PENNY, coin_def("Penny/Pennies", 0)));
   CoinsRequired(amt, coinMap);

   // Output resules
   std::cout << "Minimum Coin Sums for: $" << amt << std::endl;
   for (const auto &it : coinMap)
   {
      std::cout.width(15);
      std::cout << std::left << it.second.first << ": ";
      std::cout << std::right << it.second.second << std::endl;
   }
}

Modulo or the remainder operator for this challenge isn't really necessary anyways. The thing about my solution is it allows you to choose what coins are valid for choice; adaptable/manipulable.

Example output:
Code:
Minimum Coin Sums for: $2.37
Quarter(s)     : 9
Dime(s)        : 1
Nickel(s)      : 0
Penny/Pennies  : 2

I guess I should explain the point of the code, or just the challenge requirements. The idea is to, from a set of choose-able coins in my case, although the challenge was only for a constant collection of quarters, dimes, nickels, and pennies, show the value of the input amount, with the minimum number of coins possible.

This obviously means having as many of the larger coin values as possible to create the sum. My solution is adaptable though. You could choose what coin values you want to include from the constant values defined in my code above, or perhaps even define your own custom coin values if you wanted.

Could change it to this as well to allow both double and an integer type of input for the penny amount:
Code:
template <typename T>
void CoinsRequired(T amt, coin_map& coinMap)
{
   T pennySum = amt;
   for (const auto &it : coinMap)
   {
      T count = 0;
      while (pennySum >= count + it.first)
      {
         count += it.first;
         coinMap[it.first].second++;
      }
      pennySum -= count;
   }
}

void CoinsRequired(double amt, coin_map& coinMap)
{
   CoinsRequired((size_t) amt * 100, coinMap);
}

If I didn't go with the incremental method, I would've used fmod() if dealing with floating point, or just the regular modulo operator for integers. Floating point is okay, but not precise. I'd liked to have used a library like GMP if I took this code more seriously.

Be wary of the lack of preciseness with floating point datatypes though.

Enjoy
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

Minimal Coin Output #2
I wrote this for a challenge on another forum. I thought it may be worth while sharing here too.

Here's my solution in C++:
Code:
#include <iostream>
#include <map>

bool keyComp(unsigned lch, unsigned rch) { return lch > rch; }

typedef std::pair < std::string, size_t > coin_def;
typedef std::map<size_t, coin_def, bool(*)(size_t, size_t)> coin_map;

const size_t TOONIE = 200;
const size_t LOONIE = 100;
const size_t QUARTER = 25;
const size_t DIME = 10;
const size_t NICKEL = 5;
const size_t PENNY = 1;

void CoinsRequired(double amt, coin_map& coinMap)
{
   size_t pennySum = amt * 100;
   for (const auto &it : coinMap)
   {
      size_t count = 0;
      while (pennySum >= count + it.first)
      {
         count += it.first;
         coinMap[it.first].second++;
      }
      pennySum -= count;
   }
}


int main(void)
{
   double amt = 2.37;
   coin_map coinMap(keyComp);
   coinMap.insert(std::pair < size_t, coin_def >(QUARTER, coin_def("Quarter(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(DIME, coin_def("Dime(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(NICKEL, coin_def("Nickel(s)", 0)));
   coinMap.insert(std::pair < size_t, coin_def >(PENNY, coin_def("Penny/Pennies", 0)));
   CoinsRequired(amt, coinMap);

   // Output resules
   std::cout << "Minimum Coin Sums for: $" << amt << std::endl;
   for (const auto &it : coinMap)
   {
      std::cout.width(15);
      std::cout << std::left << it.second.first << ": ";
      std::cout << std::right << it.second.second << std::endl;
   }
}

Modulo or the remainder operator for this challenge isn't really necessary anyways. The thing about my solution is it allows you to choose what coins are valid for choice; adaptable/manipulable.

Example output:
Code:
Minimum Coin Sums for: $2.37
Quarter(s)     : 9
Dime(s)        : 1
Nickel(s)      : 0
Penny/Pennies  : 2

I guess I should explain the point of the code, or just the challenge requirements. The idea is to, from a set of choose-able coins in my case, although the challenge was only for a constant collection of quarters, dimes, nickels, and pennies, show the value of the input amount, with the minimum number of coins possible.

This obviously means having as many of the larger coin values as possible to create the sum. My solution is adaptable though. You could choose what coin values you want to include from the constant values defined in my code above, or perhaps even define your own custom coin values if you wanted.

Could change it to this as well to allow both double and an integer type of input for the penny amount:
Code:
template <typename T>
void CoinsRequired(T amt, coin_map& coinMap)
{
   T pennySum = amt;
   for (const auto &it : coinMap)
   {
      T count = 0;
      while (pennySum >= count + it.first)
      {
         count += it.first;
         coinMap[it.first].second++;
      }
      pennySum -= count;
   }
}

void CoinsRequired(double amt, coin_map& coinMap)
{
   CoinsRequired((size_t) amt * 100, coinMap);
}

If I didn't go with the incremental method, I would've used fmod() if dealing with floating point, or just the regular modulo operator for integers. Floating point is okay, but not precise. I'd liked to have used a library like GMP if I took this code more seriously.

Be wary of the lack of preciseness with floating point datatypes though.

Enjoy
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Minimal Coin Output #3
(11-02-2013, 03:51 AM)ArkPhaze Wrote: const size_t TOONIE = 200;
const size_t LOONIE = 100;

You must be Canadian... LOL

Reply

RE: Minimal Coin Output #4
(11-02-2013, 03:51 AM)ArkPhaze Wrote: const size_t TOONIE = 200;
const size_t LOONIE = 100;

You must be Canadian... LOL

Reply

RE: Minimal Coin Output #5
(11-02-2013, 04:02 AM)Geoff Wrote:
(11-02-2013, 03:51 AM)ArkPhaze Wrote: const size_t TOONIE = 200;
const size_t LOONIE = 100;

You must be Canadian... LOL

Ahaha, yeah.. Smile They don't have to be there for you Americans (presuming you are American lol).

Modified and extended OP a bit.

Edit: Just realized those type params should be updated behind the typedefs as well. Easy fix though... This is C++11 btw too. Smile
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Minimal Coin Output #6
(11-02-2013, 04:02 AM)Geoff Wrote:
(11-02-2013, 03:51 AM)ArkPhaze Wrote: const size_t TOONIE = 200;
const size_t LOONIE = 100;

You must be Canadian... LOL

Ahaha, yeah.. Smile They don't have to be there for you Americans (presuming you are American lol).

Modified and extended OP a bit.

Edit: Just realized those type params should be updated behind the typedefs as well. Easy fix though... This is C++11 btw too. Smile
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Minimal Coin Output #7
(11-02-2013, 04:41 AM)ArkPhaze Wrote: Ahaha, yeah.. Smile They don't have to be there for you Americans (presuming you are American lol).

fuck that im not American. Canadian all the way lol. Though ive lived in Australia the last 3 years lol.

Reply

RE: Minimal Coin Output #8
(11-02-2013, 04:41 AM)ArkPhaze Wrote: Ahaha, yeah.. Smile They don't have to be there for you Americans (presuming you are American lol).

fuck that im not American. Canadian all the way lol. Though ive lived in Australia the last 3 years lol.

Reply







Users browsing this thread: 1 Guest(s)