Skip to content

Instantly share code, notes, and snippets.

@berdario
Created November 15, 2016 15:39
Show Gist options
  • Save berdario/46070970cab92b20d16056ea973dbc00 to your computer and use it in GitHub Desktop.
Save berdario/46070970cab92b20d16056ea973dbc00 to your computer and use it in GitHub Desktop.
import Data.List (permutations, sortBy)
import Test.QuickCheck
readInt :: String -> Int
readInt = read
comp GT _ _ _ = GT
comp LT _ _ _ = LT
comp EQ _ [] [] = EQ
comp EQ previous [x] [] = compare x previous
comp EQ previous [] [y] = compare previous y
comp EQ _ (x:xs) (y:ys) = comp (compare x y) (max x y) xs ys
biggest :: [Int] -> String
biggest = concat . sortBy (flip $ comp EQ '0') . map show
bruteForce :: [Int] -> Int
bruteForce ns = maximum $ map (readInt . concat) $ permutations numbers
where
numbers = map show ns
prop_biggest :: NonEmptyList (NonNegative Int) -> Bool
prop_biggest (NonEmpty ns) = read (biggest numbers) == bruteForce numbers
where
numbers = map (\(NonNegative x) -> x) ns
main :: IO ()
main = quickCheck $ property prop_biggest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment