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

Line 186:
1 * 1</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<lang jq>
# If $details then provide {details, coins}, otherwise just the number of coins.
def minimum_number($details):
. as $amount
| [200, 100, 50, 20, 10, 5, 2, 1] as $denoms
| {coins: 0, remaining: 988, details: []}
| label $out
| foreach $denoms[] as $denom (.;
((.remaining / $denom)|floor) as $n
| if $n > 0
then .coins += $n
| if $details then .details += [{$denom, $n}] else . end
| .remaining %= $denom
else . end;
if .remaining == 0 then ., break $out else empty end)
| if $details then {details, coins} else .coins end ;
 
# Verbose mode:
def task:
"\nThe minimum number of coins needed to make a value of \(.) is as follows:",
(minimum_number(true)
| .details[],
"\nA total of \(.coins) coins in all." );
 
 
988
| minimum_number(false), # illustrate minimal output
task # illustrate detailed output
</lang>
{{out}}
<pre>
11
 
The minimum number of coins needed to make a value of 988 is as follows:
{"denom":200,"n":4}
{"denom":100,"n":1}
{"denom":50,"n":1}
{"denom":20,"n":1}
{"denom":10,"n":1}
{"denom":5,"n":1}
{"denom":2,"n":1}
{"denom":1,"n":1}
 
A total of 11 coins in all.</pre>
</pre>
=={{header|Julia}}==
=== Long version ===
2,471

edits