Next highest int from digits: Difference between revisions

m
→‎Haskell Minimal digit-swaps: (slight pruning of splitBy)
m (→‎Haskell :: Minimal digit-swaps: (Restored cropped final line of output))
m (→‎Haskell Minimal digit-swaps: (slight pruning of splitBy))
Line 293:
 
<lang haskell>import Data.List (unfoldr)
 
digitShuffleSuccessors
:: Integral b
Line 306:
else Just (ds, (go . reverse) ds))
(go (reversedDigits n))
 
minimalSwap
:: Ord a
Line 316:
let (less, h:more) = break (> pivot) reversedSuffix
in reverse (h : prefix) ++ less ++ pivot : more
 
---------------------------TEST----------------------------
main :: IO ()
Line 333:
digitShuffleSuccessors
[0, 9, 12, 21, 12453, 738440, 45072010, 95322020]
putStrLn $
fTable
Line 340 ⟶ 341:
(take 10 . digitShuffleSuccessors)
[9589776899767587796600]
 
--------------------------GENERIC--------------------------
reversedDigits
Line 350 ⟶ 351:
go 0 = []
go x = rem x 10 : go (quot x 10)
 
splitBy :: (a -> a -> Bool) -> [a] -> ([a], [a])
splitBy f xs = go $ break (uncurry f) $ zip xs (tail xs)
Line 356 ⟶ 357:
go (ys, zs)
| null ys = ([], xs)
| null zs = (xs, [])
| otherwise = (fst (head ys) : map snd ys, map snd zs)
 
unDigits
:: (Foldable t, Num a)
=> t a -> a
unDigits = foldl (\a b -> 10 * a + b) 0
 
--------------------------DISPLAY--------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
Line 371:
where
w = maximum (length . xShow <$> xs)
 
rjust :: Int -> Char -> String -> String
rjust n c = drop . length <*> (replicate n c ++)</lang>
9,655

edits