using System.Collections.Generic;
private Dictionary<int, int> _amounts;
public AtmMachine(Dictionary<int, int> amounts)
public List<Tuple<int, int>> Withdraw(int amount)
throw new NotImplementedException();
import java.net.Inet4Address;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AtmMachine {
private Map<Integer,Integer> amounts;
public AtmMachine(Map<Integer,Integer> a){
public Integer totalSumOfAtm(){
for (Map.Entry<Integer,Integer> entry : amounts.entrySet()){
sum+= entry.getKey()*entry.getValue();
public Map<Integer,Integer> Withdraw(int amount){
if(amount>totalSumOfAtm() || amount<=0){
return Collections.emptyMap();
Map<Integer,Integer> result = new HashMap<>();
Stream<Map.Entry<Integer,Integer>> sortedMap = amounts.entrySet()
.sorted(Collections.reverseOrder(Map.Entry.comparingByKey()));
Map<Integer,Integer> amountsSorted = sortedMap.collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (e1,e2)->e1, LinkedHashMap::new));
for (Map.Entry<Integer,Integer> entry : amountsSorted.entrySet()){
Integer nrBills=entry.getValue();
while(entry.getKey()<=amount && nrBills>0 && amount>0){
result.put(entry.getKey(),added);
return Collections.emptyMap();
import java.util.HashMap;
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<>();
map.put(1,10); map.put(5,10); map.put(10,10);
AtmMachine a = new AtmMachine(map);
System.out.println(a.totalSumOfAtm());
Map<Integer,Integer> result = a.Withdraw(27);
for (Map.Entry<Integer,Integer> entry : result.entrySet()){
System.out.println(entry.getKey()+" "+ entry.getValue());
public class VersionComparer
public int Compare(string versionOne, string versionTwo)
throw new NotImplementedException();
public class VersionComparer {
public VersionComparer(String s1,String s2){
this.s1=processedString(s1);
this.s2=processedString(s2);
public String processedString(String s){
StringBuilder result = new StringBuilder();
for(Integer i = 0; i<s.length();i++){
result.append(s.charAt(i));
for(Integer i = 0; i<s.length();i++){
result.append(s.charAt(i));
return result.toString();
public boolean containsDigits(String s){
return(s.contains("1") || s.contains("2")|| s.contains("3") || s.contains("4") || s.contains("5") ||
s.contains("6") || s.contains("7") || s.contains("8") || s.contains("9") );
public int compare(String versionOne,String versionTwo){
if(versionOne.equals(versionTwo)) return 0;
if(!containsDigits(versionOne) && !containsDigits(versionTwo) ) return 0;
Integer minLength = versionOne.length()<versionTwo.length() ? versionOne.length() : versionTwo.length();
for(Integer index=0;index<minLength;index++){
if(versionOne.charAt(index)!=versionTwo.charAt(index)){
return Character.compare(versionOne.charAt(index),versionTwo.charAt(index));
public static void Main()
private static void Run()
new AtmMachineTests().Start();
new VersionComparerTests().Start();
public class AtmMachineTests
Tests.Run($"{nameof(AtmMachine)} should", (it) =>
it.Should(() => NotProcessNegativeValues(), "Not process negative values");
it.Should(() => ReturnMultipleDenominationAmounts(), "Return multiple denomination amounts");
it.Should(() => NotProcessWithdrawalsWhenInventoryIsSmallerThanRequestedAmount(), "Not process witdrawals greater than inventory");
it.Should(() => ReturnAllAvailableMoney(), "Return all available money");
it.Should(() => KeepStateBetweenWithdrawals(), "Keep state between withdrawals");
it.Should(() => NotChangeStateIfWithdrawalIsntSuccessful(), "Not change state if withdrawal isn't successful");
private int CountMoney(List<Tuple<int, int>> amounts) => amounts.Aggregate(0, (a, i) => a + i.Item1 * i.Item2);
public void NotChangeStateIfWithdrawalIsntSuccessful()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 5 }, { 5, 5 }, { 10, 5 } });
var r1 = atm.Withdraw(50);
CountMoney(r1).ShouldBe(50);
var r2 = atm.Withdraw(50);
CountMoney(r2).ShouldBe(0);
var r3 = atm.Withdraw(20);
CountMoney(r3).ShouldBe(20);
public void NotProcessNegativeValues()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 5 } });
var result = atm.Withdraw(-5);
CountMoney(result).ShouldBe(0);
public void KeepStateBetweenWithdrawals()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 5 }, { 5, 5 } });
var r1 = atm.Withdraw(5);
CountMoney(r1).ShouldBe(5);
var r2 = atm.Withdraw(26);
CountMoney(r2).ShouldBe(0);
public void ReturnAllAvailableMoney()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 10 }, { 5, 10 }, { 10, 10 }, { 50, 10 }, { 100, 10 }, { 200, 10 }, { 500, 10 } });
var result = atm.Withdraw(8660);
var sum = CountMoney(result);
public void ReturnMultipleDenominationAmounts()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 10 }, { 5, 10 }, { 10, 10 } });
var result = atm.Withdraw(87);
CountMoney(result).ShouldBe(87);
public void NotProcessWithdrawalsWhenInventoryIsSmallerThanRequestedAmount()
var atm = new AtmMachine(new Dictionary<int, int> { { 1, 10 }, { 5, 10 }, { 10, 10 }, { 50, 10 }, { 100, 10 }, { 200, 10 }, { 500, 10 } });
var result = atm.Withdraw(8661);
CountMoney(result).ShouldBe(0);
public class VersionComparerTests
Tests.Run("VersionComparer should", (it) =>
it.Should(() => CompareEqualVersions(), "Compare equal versions");
it.Should(() => CompareDifferentVersions(), "Compare different versions");
public void CompareEqualVersions()
var comparer = new VersionComparer();
comparer.Compare("0", "0.0.0").ShouldBe(0);
comparer.Compare("0.01", "0.1").ShouldBe(0);
comparer.Compare("1.001", "1.1").ShouldBe(0);
comparer.Compare("7.015.84", "7.15.84").ShouldBe(0);
comparer.Compare("7.1", "7.1.0.0").ShouldBe(0);
public void CompareDifferentVersions()
var comparer = new VersionComparer();
comparer.Compare("01.0.1", "1.1.0").ShouldBe(-1);
comparer.Compare("33.1", "33.0.5").ShouldBe(1);
comparer.Compare("7.0105.84", "7.15.84").ShouldBe(-1);
public static void Run(string testGroup, Action<Tests> tests)
public static void StartTests(string testGroup)
Console.WriteLine("====================================================");
Console.WriteLine(testGroup);
Console.WriteLine("====================================================");
public void Should(Action test, string testName)
Console.WriteLine(testName);
Console.WriteLine(" Passed");
Console.WriteLine(" Failed");
Console.WriteLine(" " + e.Message);
public static void EndTests()