Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
10
Task/Population-count/Haskell/population-count-1.hs
Normal file
10
Task/Population-count/Haskell/population-count-1.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import Data.Bits (popCount)
|
||||
|
||||
printPops :: (Show a, Integral a) => String -> [a] -> IO ()
|
||||
printPops title counts = putStrLn $ title ++ show (take 30 counts)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
printPops "popcount " $ map popCount $ iterate (*3) (1 :: Integer)
|
||||
printPops "evil " $ filter (even . popCount) ([0..] :: [Integer])
|
||||
printPops "odious " $ filter ( odd . popCount) ([0..] :: [Integer])
|
||||
22
Task/Population-count/Haskell/population-count-2.hs
Normal file
22
Task/Population-count/Haskell/population-count-2.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import Data.Bifoldable (biList)
|
||||
import Data.List (partition, unfoldr)
|
||||
import Data.Tuple (swap)
|
||||
|
||||
--------------------- POPULATION COUNT -------------------
|
||||
popCount :: Int -> Int
|
||||
popCount = sum . unfoldr go
|
||||
where
|
||||
go x
|
||||
| 0 < x = (Just . swap) (quotRem x 2)
|
||||
| otherwise = Nothing
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ putStrLn $
|
||||
zipWith
|
||||
(\k xs -> concat [k, ":\n", show xs, "\n"])
|
||||
["Population count of powers of 3", "evil", "odious"]
|
||||
( (popCount . (3 ^) <$> [0 .. 29]) :
|
||||
biList (partition (even . popCount) [0 .. 59])
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue