From 9a65938232d1fa9e1afe9a6eb2de48d25ff738a6 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 17 Aug 2019 20:39:03 +0200 Subject: haskell problem 22, 24, 25, 29, 39, 40, 45, 53, 59 --- haskell/024-lexicographic_premutations.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 haskell/024-lexicographic_premutations.hs (limited to 'haskell/024-lexicographic_premutations.hs') diff --git a/haskell/024-lexicographic_premutations.hs b/haskell/024-lexicographic_premutations.hs new file mode 100644 index 0000000..5db0aaa --- /dev/null +++ b/haskell/024-lexicographic_premutations.hs @@ -0,0 +1,21 @@ +-- Lexicographic permutations +-- +-- Problem 24 +-- A permutation is an ordered arrangement of objects. For example, 3124 is one possible +-- permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically +-- or alphabetically, we call it lexicographic order. The lexicographic permutations of +-- 0, 1 and 2 are: +-- +-- 012 021 102 120 201 210 +-- +-- What is the millionth lexicographic permutation of the digits +-- 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? + + +import Data.Char(chr,ord) + +main = print (foldr (\x acc -> chr (ord '0' + x) : acc) "" ((permutations' [0..9]) !! 999999)) + +permutations' :: [Int] -> [[Int]] +permutations' [x] = [[x]] +permutations' xs = concat [[x : p | p <- permutations' (filter (x/=) xs)] | x <- xs] -- cgit