LoginSignup
4

More than 5 years have passed since last update.

Inverse FizzBuzz

Posted at

何やら流行っているようなので書いてみた。

import Control.Monad
import Data.List
import Data.Maybe

iFizzBuzz :: [String] -> Maybe (Int, Int)
iFizzBuzz inp = listToMaybe . map snd . sort $ do
  start <- [1..15]
  let (pos, fbs) = unzip . take (length inp) $ do
        x <- [start..]
        let fizz = x `mod` 3 == 0
            buzz = x `mod` 5 == 0
        guard $ fizz || buzz
        return (x, (if fizz then "fizz" else "") ++ (if buzz then "buzz" else ""))
  guard $ inp == fbs
  return (last pos - head pos, (head pos, last pos))

main = print . iFizzBuzz . words =<< getContents

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4