LoginSignup
0
0

More than 5 years have passed since last update.

逆FizzBuzz

Last updated at Posted at 2016-12-27

逆FizzBuzzをElixirで。

defmodule ReverseFizzBuzz do
  def fb do
    Stream.iterate(1,&(&1+1))
    |> Stream.map(fn(n) -> {n,"#{if rem(n,3)==0, do: 'Fizz'}#{if rem(n,5)==0, do: 'Buzz'}"} end)
    |> Stream.filter(fn {_,b} -> b != "" end)
  end

  def find(input) do
    count = Enum.count(input)
    list = fb |> Enum.take(15*(1+trunc(Float.ceil(count/7,0))))
    case (0..Enum.count(list)-1
      |> Enum.map(fn(i) -> Enum.slice(list, i, count) end)
      |> Enum.filter(fn(i) -> Enum.map(i,&(elem(&1,1))) == input end)) do
        [] -> 'NotFound'
        res ->
          Enum.min_by(res,fn(i) -> elem(List.last(i),0)-elem(List.first(i),0) end)
          |> Enum.map(&(elem(&1,0)))
          |> Enum.reduce(fn(x,acc) -> "#{acc},#{x}" end)
    end
  end
end

IO.puts ReverseFizzBuzz.find(String.split(List.first(System.argv),","))
0
0
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
0
0