PHP Classes

PHP Coins Change: Solve the coin change problem for a given amount

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 270 This week: 1All time: 7,716 This week: 560Up
Version License PHP version Categories
coins-change 0.1GNU General Publi...5.0Algorithms, PHP 5, Finances
Description 

Author

This class can solve the coin change problem for a given amount.

It can take a given change amount and computes the smallest number of a given set of available coins to make up the change amount.

The class may consider an infinite number of coins of each value or just a limited number of coins of each one.

Innovation Award
PHP Programming Innovation award nominee
September 2014
Number 9


Prize: One book of choice by Packt
Vending machines usually need to give change for the amount the customer paid after subtracting the due amount for the goods the customer purchased.

This class implements an algorithm to compute the number of coins to make up a given change amount, considering the number of coins available for different types of coin.

Manuel Lemos
Picture of Michele Andreoli
  Performance   Level  
Name: Michele Andreoli is available for providing paid consulting. Contact Michele Andreoli .
Classes: 7 packages by
Country: Italy Italy
Age: 38
All time rank: 54118 in Italy Italy
Week rank: 411 Up18 in Italy Italy Equal
Innovation award
Innovation award
Nominee: 4x

Example

<?php
   
/**
     * @author Michele Andreoli <michi.andreoli[at]gmail.com>
     * @name index.php
     * @version 0.1 updated 30-08-2014
     * @license http://opensource.org/licenses/gpl-license-php GNU Public License
     * @package CoinsChanger
     */

   
require_once 'CoinsManager.class.php';
      
   
$amount = 30;
   
$coins = [50, 10, 5, 2, 1, 0.5];
   
$limits = [1, 2, 1, 5, 3, 10];
   
   
//Coin change problem with FINITE coins supply
   
$changerMngr = new CoinsManager($coins, $limits);
   
   
//Get an array with coins
   
$exchange = $changerMngr->getChange($amount);
   
//Get an array with the amount for each denominations
   
$groups = $changerMngr->groupBy($exchange);
   
   
//Coin change problem with INFINITE coins supply
   
$changerMngrInfinite = new CoinsManager($coins);
   
   
//Get an array with coins
   
$exchangeInfinite = $changerMngrInfinite->getChange($amount);
   
//Get an array with the amount for each denominations
   
$groupsInfinite = $changerMngrInfinite->groupBy($exchangeInfinite);
?>
<html>
    <head>
        <title>Coins Changer Machine</title>
    </head>
    <body>
        <h2>Coins changer with <b>finite</b> coins supply</h2>
        <p>Change <b><?= $amount ?>&euro;</b> with:</p>
        <?php
           
if ($exchange < 0 || $groups == FALSE) {
                echo
"<p><b>No solutions</b></p>";
            }
            else {
       
?>
<ul>
            <?php
               
foreach ($groups as $key => $group) {
                    echo
"<li><b>" . $key . "&euro;</b> => " . $group . "</li>";
                }
           
?>
</ul>
            <?php } ?>
<h2>Coins changer with <b>infinite</b> coins supply</h2>
        <p>Change <b><?= $amount ?>&euro;</b> with:</p>
        <?php
           
if ($exchangeInfinite < 0 || $groupsInfinite == FALSE) {
                echo
"<p><b>No solutions</b></p>";
            }
            else {
       
?>
<ul>
            <?php
               
foreach ($groupsInfinite as $key => $group) {
                    echo
"<li><b>" . $key . "&euro;</b> => " . $group . "</li>";
                }
           
?>
</ul>
            <?php } ?>
</body>
</html>


  Files folder image Files  
File Role Description
Plain text file CoinsManager.class.php Class Main class for coin changer
Accessible without login Plain text file index.php Example Example of use
Accessible without login HTML file output.html Output Output sample

 Version Control Unique User Downloads Download Rankings  
 0%
Total:270
This week:1
All time:7,716
This week:560Up