Friday 15 January 2010

haskell - Determining ways of splitting change -


I am very new to Haskell and am interested in ways to improve the solution to this problem. "Looking at the amount of money (

in St.), determining all the methods of making a change in the list of denominations". Changes :: Int -> [Int] - & gt; [[Internet]] change AMT [] = [[]] AMT [D] = change [APTD] D] AMT (D: DINOOM) = = DT, LT; = AMT, reverse [0 .. (Quot MT D)]> gt; & Gt; = \ X - & gt; [(Transcript xd) ++ c | C & lt; - (change (amt - (x * d) denom) Other changes amt denoms change amset = change amt [25, 10, 5, 1] ​​- * main & gt; ChangeUS 29 - [[25,1,1,1,1], [10,10,5,1,1,1,1], [10,10,1,1,1,1,1,1, 1 , 1,1], [10,5,5,5,1,1,1], [10,5,5,1,1,1,1,1,1,1,1], [10,5,1,1,1,1,1,1,1,1,1,1,1,1,1], [10,1,1,1,1,1,1,1,1 , 1,1,1,1,1,1,1,1,1,1,1,1], [5,5,5,5,1,1,1,1], [5, 5 , 5,5,1,1,1,1,1,1,1,1,1], [5,5,5,1,1,1,1,1,1,1,1,1, 11 , 1,1,1], [5,5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ], [5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1] , 1], [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1 , 1,1,1,1,1,1]]

A problem with this solution assumes that the lowest number will be 1. Otherwise, the change AMT [d] case will be wrong. I can add a if / then to ensure AMT in that case is equally divisible from d , but it obtains slightly verbose Is starting to do, and I think this matter is not necessarily a better solution.

This is just a tremendous force.

  - Believers that sects are different if they are not, then - return value is ambiguous change: [integer] -> Integer - & gt; [[Integer]] change _ 0 = [[]] change [] _ ​​= [] change xxs @ (x: xs) n | N & gt; = X = map (x) (change xxs (n - x)) ++ change xs n | Otherwise = change xs n   

This is completely immune to the cases where a greedy approach does not work, it does not care whether the input list has been sorted, and its The only reason is failure when the denomination is not different, then the output format does not distinguish between different inputs in that case. If you change the output type to differentiate between different inputs with the same sect, the same algorithm will work.

It is probably slow in cases with many branches, but it is not possible in change-making problems. It is also productive, it is thus able to increase production if the consumer is partial output Can do anything worthwhile with.

No comments:

Post a Comment