Skip to content

Instantly share code, notes, and snippets.

@jerronimo
Last active February 8, 2018 09:03
Show Gist options
  • Save jerronimo/cd25009f00d3c67f424db85dfe6c076e to your computer and use it in GitHub Desktop.
Save jerronimo/cd25009f00d3c67f424db85dfe6c076e to your computer and use it in GitHub Desktop.
<?php
//
//1. Write an application which calculates the least amount of Euro-Coins needed for any input amount
//7,21 Euro = 6 Coins (2€+2€+2€+1€+0,2€+0,01€)
//3,72 Euro = 6 Münzen (2€+1€+0,5€+0,2€+0,02€) - >>>> it looks like mistake. Right answer should be 5 coins
class App {
/**
* Different coins nominals
* @var array
*/
const EURO_COINS = [
0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2
];
/**
* Calculate min amount of coins
* @param float $amount
* @return array
*/
public function calculate($amount)
{
if (in_array($amount, self::EURO_COINS)) {
return [
'coins' => [$amount],
'amount' => 1,
];
} else {
$coins = self::EURO_COINS;
arsort($coins);
$result = $this->getCoinsCount($coins, $amount, [
'coins' => [],
'amount' => 0,
]);
return $result;
}
}
/**
* @param array $coins
* @param integer $amount
* @param array $result
* @return array
*/
private function getCoinsCount($coins, $amount, $result = [])
{
foreach ($coins as $coin) {
if (round($amount, 2) >= round($coin, 2)) {
$current = (int)($amount / $coin);
if ($current > 1) {
for ( $i = 0; $i < $current; $i++) {
$result['coins'][] = $coin;
$result['amount'] += 1;
}
} else {
$result['coins'][] = $coin;
$result['amount'] += 1;
}
$amount -= ($current * $coin);
}
}
return $result;
}
}
//2. Write a function that accepts two parameters, a parent and a child string. Determine how
//many times the child string - or an anagram of the child string - appears in the parent string.
//f('AdnBndAndBdaBn', 'dAn') // 4 ("Adn", "ndA", "dAn", "And")
//f('AbrAcadAbRa', 'cAda') // 2
class Counter {
/**
* Count amount of substring (and substring's anagrams)
* @param string $mainString
* @param string $substring
* @return integer
*/
public function countSubstring($mainString, $substring)
{
$amount = 0;
$anagrams = $this->getAnagrams($substring);
if (!empty($anagrams)) {
foreach ($anagrams as $anagram) {
$amount += substr_count($mainString, $anagram);
}
}
return $amount;
}
/**
* @param string $substring
* @return array
*/
private function getAnagrams($substring)
{
if (strlen($substring) < 2) {
return [$substring];
}
$combination = substr($substring, 1);
$result = [];
foreach ($this->getAnagrams($combination) as $letter) {
$length = strlen($letter);
for ($i = 0; $i <= $length; $i++) {
$result[] = substr($letter, 0, $i) . $substring[0] . substr($letter, $i);
}
}
return array_unique($result);
}
}
$app = new App();
$answer1 = $app->calculate(7.21);
$answer2 = $app->calculate(3.72);
$answer3 = $app->calculate(2);
echo "<pre>";
print_r($answer1);
echo "<pre>";
echo "<pre>";
print_r($answer2);
echo "<pre>";
echo "<pre>";
print_r($answer3);
echo "<pre>";
$counter = new Counter();
$answer4 = $counter->countSubstring('AdnBndAndBdaBn', 'dAn');
$answer5 = $counter->countSubstring('AbrAcadAbRa', 'cAda');
echo "<pre>";
print_r($answer4);
echo "<pre>";
echo "<pre>";
print_r($answer5);
echo "<pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment