2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Inverse FizzBuzz

Posted at

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

http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html

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
2
4
0

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?