LoginSignup
4
2

More than 3 years have passed since last update.

「Google Recruit」をElixirでやってみる

Last updated at Posted at 2020-12-28

はじめに

What is Elixir ?

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development, embedded software, data ingestion, and multimedia processing domains across a wide range of industries.

  • 不老不死の霊薬っちいうことですよ

準備

  • Elixirをインストールしましょう
  • 手前味噌ですが、インストールなどを参考にしてください
$ mix new google_recruit
$ cd google_recruit
  • いろいろファイルができますが、Elixirが初めての方はそんなものだとおもってください
    • 慣れると景色に見えます (by @kikuyuta 先生)
  • 今回は1ファイルしか触りません
  • プロジェクトのルートにexp.txtを置きました
exp.txt
2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

問題

  • 問題文は参考にした記事中をご参照ください :pray::pray_tone1::pray_tone2::pray_tone3::pray_tone4::pray_tone5:
  • あっ、Ruby縛りなの...
  • まあ、いいやElixirでやろう :rocket::rocket::rocket:
  • 私には問題の意味があんまりわかりませんでしたが、参考記事のRubyプログラムをElixirで書いてみたらどうなるかをやってみて、答えが同じになればそれでよしとします

で、四の五の言わずに書いてみる

lib/google_recruit.ex
defmodule GoogleRecruit do
  def problem_one do
    list_of_10_digits()
    |> Enum.map(&Enum.join/1)
    |> Enum.map(&String.to_integer/1)
    |> Enum.find(&is_prime?/1)
  end

  def problem_two do
    list_of_10_digits()
    |> Enum.filter(&is_digits_sum_49?/1)
    |> Enum.at(4)
    |> Enum.join()
    |> String.to_integer()
  end

  defp list_of_10_digits do
    File.read!("exp.txt")
    |> String.replace("\n", "")
    |> String.replace(".", "")
    |> String.codepoints()
    |> Enum.chunk_every(10, 1, :discard)
  end

  defp is_prime?(n) when n in [2, 3], do: true

  defp is_prime?(n) do
    floored_sqrt =
      :math.sqrt(n)
      |> Float.floor()
      |> round

    !Enum.any?(2..floored_sqrt, &(rem(n, &1) == 0))
  end

  defp is_digits_sum_49?(digits) do
    Enum.map(digits, &String.to_integer/1)
    |> Enum.sum()
    |> Kernel.==(49)
  end
end

Run

$ iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Interactive Elixir (1.11.2) - press Ctrl+C to exit (type h() ENTER for help)

iex> GoogleRecruit.problem_one
7427466391

iex> GoogleRecruit.problem_two
5966290435

iex> System.halt
$

:tada::tada::tada:

Wrapping Up :christmas_tree::santa::santa_tone1::santa_tone2::santa_tone3::santa_tone4::santa_tone5::christmas_tree:

  • Enum.chunk_every/4くん、大活躍 :rocket::rocket::rocket:
  • Enumモジュールは、Elixirにおいて必修科目
  • Stringモジュールもよく使うでありますよ
  • 今回に関していうと大部分この2つのモジュールで書き上げています
  • 他は
  • Enjoy Elixir :bangbang::bangbang::bangbang:

ありがとナイス :flag_cn:


  1. 素数の判定について、私が狼おっさんになってしまっていたり、Elixirではこうやるのが鉄則です、みたいなものをご存知の方がいらっしゃいましたら、ぜひご指導・ご鞭撻のほどよろしくお願いします。すぐ書き直します :pray::pray_tone1::pray_tone2::pray_tone3::pray_tone4::pray_tone5:  

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