LL Golf Hole 7 - バイト数を読みやすくする
Posted feedbacks - Haskell
無難に。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import List
import Maybe
units = " KMGTPEZY"
k = 1024
humanReadable x
| x < k = show x
| otherwise = show a ++ "." ++ show b ++ [c] where
(y,xs) = mapAccumL (\x c -> (div x k, (x,c))) (x*10) $ init units
(z,c) = fromMaybe (y,last units) $ find ((<k*10).fst) xs
(a,b) = divMod z 10
{-
> humanReadable 123456789012345678901234567890
"102121.0Y"
-}
|
全部作ってから判断すると言う素直(富豪?)な実装。
1 2 3 4 5 6 | toMan n = snd $ head $ reverse $ filter (\(x,y) -> n>x)
$ map (\(x,y) -> (10**x, (show (n / (10**x))) ++ y))
$ zip [0,3.0..] ["","k","M","G","T","P","E","Z","Y"]
main = do
putStrLn $ toMan 1234567890123456789012345678901
|
もうちょっと短くなりますね。
1 2 3 | toMan n = snd $ last $ filter (\x -> n>fst x)
$ map (\(x,y) -> (10^x,(show $ n/10^x) ++ [y]))
$ zip [0,3..] " kMGTPEZY"
|
バイト数との事なので。。。 スペースと改行を詰めて101Byte
1 2 3 | toMan n = snd $ last $ filter (\x -> n>fst x)
$ map (\(x,y) -> (1024^x,(show $ n/1024^x) ++ [y]))
$ zip [0,1..] " kMGTPEZY"
|
内包表現を使用して空白を詰めると73Byte
1 | toMan n = last [(show $ n/1024^x) ++ [y] | (x,y) <- zip [0,1..] " kMGTPEZY", n>=1024^x]
|
1 2 | r=reverse
main=readLn>>=putStrLn.f;f n|n<4^5=show n|1>0=(\(x,u)->let(a:b)=r$show x in r b++'.':a:[u])$last$filter((>9).fst)$scanl((,).(`div`4^5).fst)(n*10,' ')"KMT"
|




takano32
#7310()
[
Ruby
]
Rating1/1=1.00
与えられたバイト数を読みやすくしてください。読みやすくとは、いわゆる human readable な表記とします(詳しくはサンプルのコードを参考にしてください)。
与えるバイト数についてはリテラルで与える、標準入力で与える、引数で与えるなどは自由とします。
余力のあるものはこのプログラムを短くしてください。
※ LL Future実行委員の高野光弘です。この出題は LL Future公式の出題であり、優れたものについてはLL Golfのセッションでご紹介させていただくかもしれません。ご理解の上、ご投稿ください。また、LL Futureのチケットは現在も発売中です。よろしければ、メインイベントの方にもぜひご参加ください。
Rating1/1=1.00-0+
[ reply ]