Last active
April 12, 2018 11:58
-
-
Save lovasoa/21746b8c0e1dc65ad487bb20c1966c42 to your computer and use it in GitHub Desktop.
Soit la liste de nombres [3,8,3,8]. En utilisant tous les nombres de cette, liste, uniquement ces nombres, et les opérations mathématiques usuelles (addition, multiplication, division, soustraction), trouver une manière d'obtenir le nombre 24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import Control.Monad | |
data Operation = Plus | Minus | Mul | Div deriving (Eq,Ord) | |
data Tree = Compute Operation Tree Tree | Leaf Rational deriving (Eq,Ord) | |
instance Show Tree where | |
show (Compute op a b) = "(" ++ (show a) ++ (show op) ++ (show b) ++")" | |
show (Leaf x) = show (fromRational x) | |
instance Show Operation where | |
show Plus = "+" | |
show Minus = "-" | |
show Mul = "*" | |
show Div = "/" | |
operationFun :: Fractional a => Operation -> a -> a -> a | |
operationFun Plus = (+) | |
operationFun Minus = (-) | |
operationFun Mul = (*) | |
operationFun Div = (/) | |
treeEval :: Tree -> Rational | |
treeEval (Compute op left right) = (operationFun op) (treeEval left) (treeEval right) | |
treeEval (Leaf x) = x | |
validateOp :: Operation -> Tree -> Tree -> Bool | |
validateOp op left right | (op==Mul||op==Plus) && left > right = False | |
validateOp op left right | (op==Div) && (treeEval right) == 0 = False | |
validateOp op left right | otherwise = True | |
allElems :: Eq a => [a] -> [(a,[a])] | |
allElems xs = map (\x -> (x, delete x xs)) (nub xs) | |
solution :: [Tree] -> Rational -> Maybe Tree | |
solution [tree] objective = if (treeEval tree) == objective then Just tree else Nothing | |
solution trees objective = | |
msum$map (\(left, rems) -> | |
msum$map (\(right, rrems) -> | |
msum$map (\op -> | |
if validateOp op left right | |
then solution ((Compute op left right):rrems) objective | |
else Nothing | |
) [Plus,Minus,Mul,Div] | |
) (allElems rems) | |
) (allElems trees) | |
solutionNum :: Real a => [a] -> a -> Maybe Tree | |
solutionNum xs objective = solution (map (Leaf . toRational) xs) (toRational objective) | |
main = print $ solutionNum [3,3,8,8] 24 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solution(nums, objective) { | |
if (nums.length == 1) { | |
let res = eval(nums[0]); | |
if (Math.abs(res - objective) < 1e-5) return nums[0]; | |
return null; | |
} | |
for(let op of "+-*/") { | |
for(let i=0; i<nums.length; i++) { | |
for(let j=0; j<nums.length; j++) { | |
if(i!=j){ | |
let nnums = nums.filter((elt, k) => k!=i && k!=j).concat('(' + nums[i] + op + nums[j] + ')'); | |
let res = solution(nnums,objective); | |
if (res != null) return res; | |
} | |
} | |
} | |
} | |
return null; | |
} | |
solution(['3','3','8','8'], 24) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment