Find minimum number of coins that make a given value: Difference between revisions

→‎{{header|Python}}: Added a functionally composed variant.
(→‎{{header|Haskell}}: Added a draft in Haskell)
(→‎{{header|Python}}: Added a functionally composed variant.)
Line 234:
 
=={{header|Python}}==
===Python :: Procedural===
<lang python>def makechange(denominations = [1,2,5,10,20,50,100,200], total = 988):
print(f"Available denominations: {denominations}. Total is to be: {total}.")
Line 255 ⟶ 256:
1 * 1
</pre>
 
===Python :: Functional===
<lang python>'''Minimum number of coins to make a given value'''
 
 
# change :: [Int] -> Int -> [(Int, Int)]
def change(xs):
'''A list of (quantity, denomination) pairs.
Unused denominations are excluded from the list.
'''
def go(n):
if xs and n:
h, *t = xs
q, r = divmod(n, h)
 
return ([(q, h)] if q else []) + (
change(t)(r)
)
else:
return []
 
return go
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Testing a set of denominations with two sums'''
 
f = change([200, 100, 50, 20, 10, 5, 2, 1])
print(
"\n".join([
f'Summing to {n}:\n' + "\n".join([
f'{qu[0]} * {qu[1]}' for qu in f(n)]
) + "\n"
for n in [1024, 988]
])
)
 
 
# MAIN ---
if __name__ == '__main__':
main()</lang>
{{Out}}
<pre>Summing to 1024:
5 * 200
1 * 20
2 * 2
 
Summing to 988:
4 * 200
1 * 100
1 * 50
1 * 20
1 * 10
1 * 5
1 * 2
1 * 1</pre>
 
=={{header|Raku}}==
9,655

edits