Highest Scoring Word

Elixir

defmodule Kata do
  def high(str) do
    words = String.split(str, " ")
    word_values = Enum.map(words, fn word -> get_word_value(word) end)
    max_value = Enum.max(word_values)
    index = Enum.find_index(word_values, fn x -> x == max_value end)
    Enum.at(words, index)
  end

  def get_word_value(word) do
    chars = String.split(word, "", trim: true)
    Enum.reduce chars, 0, fn (char, acc) ->
      # https://elixirforum.com/t/get-ascii-value-of-a-character/16619/3
      <<val::utf8>> = char
      val - 96 + acc # a's codepoint is 97 and the intended value is 1
    end
  end
end

Tags

  1. elixir (Private)
  2. 6-kyu (Private)
  3. codewars (Private)
  4. answer (Private)