Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
56
Task/Rep-string/Haskell/rep-string-1.hs
Normal file
56
Task/Rep-string/Haskell/rep-string-1.hs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import Data.List (inits, maximumBy)
|
||||
import Data.Maybe (fromMaybe)
|
||||
|
||||
repstring :: String -> Maybe String
|
||||
-- empty strings are not rep strings
|
||||
repstring [] = Nothing
|
||||
-- strings with only one character are not rep strings
|
||||
repstring [_] = Nothing
|
||||
repstring xs
|
||||
| any (`notElem` "01") xs = Nothing
|
||||
| otherwise = longest xs
|
||||
where
|
||||
-- length of the original string
|
||||
lxs = length xs
|
||||
-- half that length
|
||||
lq2 = lxs `quot` 2
|
||||
-- make a string of same length using repetitions of a part
|
||||
-- of the original string, and also return the substring used
|
||||
subrepeat x = (x, take lxs $ concat $ repeat x)
|
||||
-- check if a repeated string matches the original string
|
||||
sndValid (_, ys) = ys == xs
|
||||
-- make all possible strings out of repetitions of parts of
|
||||
-- the original string, which have max. length lq2
|
||||
possible = map subrepeat . take lq2 . tail . inits
|
||||
-- filter only valid possibilities, and return the substrings
|
||||
-- used for building them
|
||||
valid = map fst . filter sndValid . possible
|
||||
-- see which string is longer
|
||||
compLength a b = compare (length a) (length b)
|
||||
-- get the longest substring that, repeated, builds a string
|
||||
-- that matches the original string
|
||||
longest ys = case valid ys of
|
||||
[] -> Nothing
|
||||
zs -> Just $ maximumBy compLength zs
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ processIO examples
|
||||
where
|
||||
examples =
|
||||
[ "1001110011",
|
||||
"1110111011",
|
||||
"0010010010",
|
||||
"1010101010",
|
||||
"1111111111",
|
||||
"0100101101",
|
||||
"0100100",
|
||||
"101",
|
||||
"11",
|
||||
"00",
|
||||
"1"
|
||||
]
|
||||
process = fromMaybe "Not a rep string" . repstring
|
||||
processIO xs = do
|
||||
putStr (xs <> ": ")
|
||||
putStrLn $ process xs
|
||||
53
Task/Rep-string/Haskell/rep-string-2.hs
Normal file
53
Task/Rep-string/Haskell/rep-string-2.hs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import Data.Bool (bool)
|
||||
import Data.List (inits, intercalate, transpose)
|
||||
|
||||
------------------------ REP-CYCLES ----------------------
|
||||
|
||||
repCycles :: String -> [String]
|
||||
repCycles cs =
|
||||
filter
|
||||
((cs ==) . take n . cycle)
|
||||
((tail . inits) $ take (quot n 2) cs)
|
||||
where
|
||||
n = length cs
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn $
|
||||
fTable
|
||||
"Longest cycles:\n"
|
||||
id
|
||||
((flip bool "n/a" . last) <*> null)
|
||||
repCycles
|
||||
[ "1001110011",
|
||||
"1110111011",
|
||||
"0010010010",
|
||||
"1010101010",
|
||||
"1111111111",
|
||||
"0100101101",
|
||||
"0100100",
|
||||
"101",
|
||||
"11",
|
||||
"00",
|
||||
"1"
|
||||
]
|
||||
|
||||
------------------------- GENERIC ------------------------
|
||||
fTable ::
|
||||
String ->
|
||||
(a -> String) ->
|
||||
(b -> String) ->
|
||||
(a -> b) ->
|
||||
[a] ->
|
||||
String
|
||||
fTable s xShow fxShow f xs =
|
||||
let rjust n c = drop . length <*> (replicate n c <>)
|
||||
w = maximum (length . xShow <$> xs)
|
||||
in unlines $
|
||||
s :
|
||||
fmap
|
||||
( ((<>) . rjust w ' ' . xShow)
|
||||
<*> ((" -> " <>) . fxShow . f)
|
||||
)
|
||||
xs
|
||||
Loading…
Add table
Add a link
Reference in a new issue