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

→‎{{header|Haskell}}: Allowed for denomination lists which are ill-sorted or incomplete.
(→‎{{header|Haskell}}: Added a variant (hand-written recursion, rather than standard recursion scheme))
(→‎{{header|Haskell}}: Allowed for denomination lists which are ill-sorted or incomplete.)
Line 108:
(1,1)</pre>
 
Or as a hand-written recursion, defining a slightly more parsimonious listing:, and allowing for denomination lists which are ill-sorted or incomplete.
 
<lang haskell>----------import MINIMUMData.List NUMBER OF COINS TO MAKE A SUM ---------(sortOn)
import Data.Ord (Down (Down))
 
---------- MINIMUM NUMBER OF COINS TO MAKE A SUM ---------
change :: [Int] -> Int -> [(Int, Int)]
 
change [] n = []
change _:: 0[Int] =-> Int -> Either String [(Int, Int)]
change (x : xs)units n
| 01 ==`elem` qunits = changeRight $ go (sortOn Down units) xs(abs n)
| otherwise = (q,Left x)"Incomplete :denominations change- xslacks r1"
where
(q,go r)[] n = quotRem n x[]
go _ 0 = []
go (x : xs) n
| 0 == q = go xs n
| otherwise = (q, x) : go xs r
where
(q, r) = quotRem n x
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ putStrLn $ test <$> [1024, 988]
main = do
where
let f = change [200, 100, 50, 20, 10, 5, 2, 1]
test n =
mapM_ putStrLn $
fmap either
( \n ->id
( \qvs concat $->
"Summingconcat to " <> show n <> ":\n" :$
fmap "Summing to " <> show n <> ":\n" :
( \(q, v) ->fmap
( \(q, showv) q->
<>concat [show q, " * ", show v, "\n"]
<> show v)
<> "\n"qvs
)
(change [200, 100, 50, 20, 10, (f5, 2, 1] n)</lang>
)
[1024, 988]</lang>
{{Out}}
<pre>Summing to 1024:
Line 155 ⟶ 160:
1 * 5
1 * 2
1 * 1</pre>
</pre>
 
=={{header|Julia}}==
9,655

edits