LoginSignup
1
1

More than 5 years have passed since last update.

Elixirで"1時間以内に解けなければ(ry"をやってみた

Last updated at Posted at 2015-08-21

Scala版の焼き直しです。
Elixirっぽさがでているかどうかは微妙。

問題2

Problem2.ex
defmodule Problem2 do
    def mutually_list(a, b) do
        Enum.zip(a,b) |> Enum.flat_map(fn {x,y} -> [x,y] end)
    end
end

問題3

Problem3.ex
defmodule Problem3 do
    def fib do
        Stream.unfold({0, 1}, fn {a,b} -> {a , {b, a + b}} end)
        |> Enum.take(100)
    end
end

問題4

Problem4.ex
defmodule Problem4 do
    def permutations([]), do: [[]]
    def permutations(list) do
        for h <- list, t <- permutations(list -- [h]), do: [h | t]
    end

    def max(list) do
        permutations(list) 
        |> Enum.map(fn p -> Enum.map(p, fn i -> Integer.to_string(i) end) |> List.to_string end) 
        |> Enum.max
    end
end

問題5

Problem5.ex
defmodule Problem5 do
    def calc(str, acc) do
        case {Regex.run(~r/(^[1-9]+)(.*)/, str), Regex.run(~r/([+-])([1-9]+)(.*)/, str)} do
            {[_, n, last], _} -> calc(last, acc + String.to_integer(n))
            {_, [_, "+", n, last]} -> calc(last, acc + String.to_integer(n))
            {_, [_, "-", n, last]} -> calc(last, acc - String.to_integer(n))
            _ -> acc
        end
    end

    def comb do
        Enum.to_list(2..9)
        |> List.foldl(["1"], fn (x, acc) -> Enum.flat_map(acc, fn n -> ["#{n}#{x}", "#{n}+#{x}", "#{n}-#{x}"] end) end)
    end

    def find do
        Enum.filter(comb, fn str -> calc(str, 0) == 100 end)
    end
end

1
1
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
1
1