Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,26 @@
import Data.List (sort)
import System.Random (randomRIO)
import System.IO.Unsafe (unsafePerformIO)
data Color = Red | White | Blue deriving (Show, Eq, Ord, Enum)
dutch :: [Color] -> [Color]
dutch = sort
isDutch :: [Color] -> Bool
isDutch x = x == dutch x
randomBalls :: Int -> [Color]
randomBalls 0 = []
randomBalls n = toEnum (unsafePerformIO (randomRIO (fromEnum Red,
fromEnum Blue))) : randomBalls (n - 1)
main :: IO ()
main = do
let a = randomBalls 20
case isDutch a of
True -> putStrLn $ "The random sequence " ++ show a ++
" is already in the order of the Dutch national flag!"
False -> do
putStrLn $ "The starting random sequence is " ++ show a ++ "\n"
putStrLn $ "The ordered sequence is " ++ show (dutch a)

View file

@ -0,0 +1,21 @@
inorder n = and $ zipWith (<=) n (tail n) -- or use Data.List.Ordered
mk012 :: Int -> Int -> [Int] -- definitely unordered
mk012 n = (++[0]).(2:).map (`mod` 3).take n.frr where
-- frr = Fast Rubbish Randoms
frr = tail . iterate (\n -> n * 7 + 13)
dutch1 n = (filter (==0) n)++(filter (==1) n)++(filter (==2) n)
dutch2 n = a++b++c where
(a,b,c) = foldl f ([],[],[]) n -- scan list once; it *may* help
f (a,b,c) x = case x of
0 -> (0:a, b, c)
1 -> (a, x:b, c)
2 -> (a, b, x:c)
main = do -- 3 methods, comment/uncomment each for speed comparisons
-- print $ inorder $ sort s -- O(n log n)
-- print $ inorder $ dutch1 s -- O(n)
print $ inorder $ dutch2 s -- O(n)
where s = mk012 10000000 42