DojoDeveloppement/Mercredi13Juin2012Rétrospective de la session du /Mercredi6Juin2012 :
Sujets de ce soir :
PeserAvecDesPierres.hs
module PeserAvecDesPierres where
import Data.List
import Data.Ord
combienDePoidsPossibles :: [Int] -> Int
combienDePoidsPossibles [_] = 1
combienDePoidsPossibles [x, y] = length $ filter (>0) $ nub [x, y, x + y, y - x]
candidatsParDeux :: Int -> [[Int]]
candidatsParDeux x = [[a,x-a] | a <- [1..x `div` 2]]
candidatsParTrois x =
nub [ sort [a, b1, b2] | [a, b] <- candidatsParDeux x,
[b1, b2] <- candidatsParDeux b ]
meilleurDécoupage :: Int -> [Int]
meilleurDécoupage = maximumBy (comparing combienDePoidsPossibles) . candidatsParDeux
Tests.hs
module Tests where import Test.HUnit import PeserAvecDesPierres main = runTestTT $ TestList [ combienDePoidsPossibles [1] ~?= 1 ,combienDePoidsPossibles [1, 1] ~?= 2 ,combienDePoidsPossibles [1, 2] ~?= 3 ,combienDePoidsPossibles [2, 3] ~?= 4 ,candidatsParDeux 2 ~?= [[1, 1]] ,candidatsParDeux 3 ~?= [[1, 2]] ,candidatsParDeux 4 ~?= [[1, 3],[2, 2]] ,meilleurDécoupage 4 ~?= [1, 3] ,candidatsParTrois 3 ~?= [[1, 1, 1]] ,candidatsParTrois 4 ~?= [[1, 1, 2]] ,candidatsParTrois 5 ~?= [[1, 1, 3],[1, 2, 2]] ,candidatsParTrois 7 ~?= [[1, 1, 5],[1, 2, 4],[1,3,3],[2,2,3]] ]