How to resolve the algorithm Cantor set step by step in the Elixir programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cantor set step by step in the Elixir programming language
Table of Contents
Problem Statement
Draw a Cantor set.
See details at this Wikipedia webpage: Cantor set
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cantor set step by step in the Elixir programming language
Source code in the elixir programming language
defmodule Cantor do
@pos "█"
@neg " "
def run(lines) do
Enum.map(0..lines, fn line ->
segment_size = 3 ** (lines - line - 1)
chars = (3 ** line)
Enum.map(0..chars, fn char ->
char
|> Integer.digits(3)
|> Enum.any?(fn x -> x === 1 end)
|> case do
true -> @neg
false -> @pos
end
end)
|> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end)
|> Enum.join()
|> String.trim_trailing()
end)
|> Enum.filter(fn line -> line !== "" end)
end
def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)]
def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc
end
Cantor.run(5) |> IO.inspect()
You may also check:How to resolve the algorithm ISBN13 check digit step by step in the Julia programming language
You may also check:How to resolve the algorithm Inconsummate numbers in base 10 step by step in the Raku programming language
You may also check:How to resolve the algorithm Bitmap step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm First-class functions/Use numbers analogously step by step in the Java programming language
You may also check:How to resolve the algorithm Exceptions step by step in the Objective-C programming language