Avatar of davearonson

davearonson's solution

to Palindrome Products in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are reversed. For example, 121 is a palindromic number but 112 is not.

Given a range of numbers, find the largest and smallest palindromes which are products of numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range. If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

Example 1

Given the range [1, 9] (both inclusive)...

And given the list of all possible products within this range: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]

The palindrome products are all single digit numbers (in this case): [1, 2, 3, 4, 5, 6, 7, 8, 9]

The smallest palindrome product is 1. Its factors are (1, 1). The largest palindrome product is 9. Its factors are (1, 9) and (3, 3).

Example 2

Given the range [10, 99] (both inclusive)...

The smallest palindrome product is 121. Its factors are (11, 11). The largest palindrome product is 9009. Its factors are (91, 99).

Running tests

Execute the tests with:

$ elixir palindrome_products_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.

Source

Problem 4 at Project Euler http://projecteuler.net/problem=4

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

palindrome_products_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("palindrome_products.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule PalindromeProductsTest do
  use ExUnit.Case

  # @tag :pending
  test "largest palindrome from single digit factors" do
    palindromes = Palindromes.generate(9)
    assert palindromes |> Map.keys() |> Enum.sort() |> List.last() == 9
    assert Enum.sort(palindromes[9]) == [[1, 9], [3, 3]]
  end

  @tag :pending
  test "largest palindrome from double digit factors" do
    palindromes = Palindromes.generate(99, 10)
    assert palindromes |> Map.keys() |> Enum.sort() |> List.last() == 9009
    assert palindromes[9009] == [[91, 99]]
  end

  @tag :pending
  test "smallest palindrome from double digit factors" do
    palindromes = Palindromes.generate(99, 10)
    assert palindromes |> Map.keys() |> Enum.sort() |> hd == 121
    assert palindromes[121] == [[11, 11]]
  end

  @tag :pending
  test "largest palindrome from triple digit factors" do
    palindromes = Palindromes.generate(999, 100)
    assert palindromes |> Map.keys() |> Enum.sort() |> List.last() == 906_609
    assert palindromes[906_609] == [[913, 993]]
  end

  @tag :pending
  test "smallest palindromes from triple digit factors" do
    palindromes = Palindromes.generate(999, 100)
    assert palindromes |> Map.keys() |> Enum.sort() |> hd == 10201
    assert palindromes[10201] == [[101, 101]]
  end
end
defmodule Palindromes do

  @doc """
  Generates all palindrome products from an optionally given min factor (or 1) to a given max factor.
  """
  @spec generate(non_neg_integer, non_neg_integer) :: map
  def generate(max_factor, min_factor \\ 1) do
    do_generate(min_factor, min_factor, max_factor + 1, %{})
  end

  defp do_generate(too_high, _, too_high, acc), do: acc

  defp do_generate(cur1, too_high, too_high, acc), do:
    do_generate(cur1 + 1, cur1 + 1, too_high, acc)

  defp do_generate(cur1, cur2, too_high, acc) do
    do_generate(cur1, cur2 + 1, too_high,
                revise_accumulator(cur1, cur2, acc))
  end

  defp revise_accumulator(cur1, cur2, acc) do
    product = cur1 * cur2
    if palindrome?(Integer.to_string(product)) do
      Map.put(acc, product, [[cur1, cur2] | Map.get(acc, product, [])])
    else
      acc
    end
  end

  defp palindrome?(str), do: String.reverse(str) == str

end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?