Реализации алгоритмов/Алгоритм Робинсона — Шенстеда: различия между версиями
Содержимое удалено Содержимое добавлено
РоманСузи (обсуждение | вклад) программа на Elixir |
(нет различий)
|
Версия от 12:36, 19 декабря 2015
Elixir
defmodule Schensted do
# выставка в таблицу
def insert(table, nil) do
table
end
def insert([], x) do
[[x]]
end
def insert([current | rest], x) do
{updated, x1} = insert_into_row(current, x)
[updated | insert(rest, x1)]
end
# вставка в строку
def insert_into_row([], x) do
{[x], nil}
end
def insert_into_row([current | rest], x) when current <= x do
{updated, x1} = insert_into_row(rest, x)
{[current | updated], x1}
end
def insert_into_row([current | rest], x) when current > x do
{[x | rest], current}
end
# формирование таблицы на основе слова Шенстеда (списка)
def from_word(w) do
Enum.reduce(w, [], fn(x, acc) -> insert(acc, x) end)
end
# вывод таблицы на печать
def print_table(table) do
IO.inspect(table |> Enum.map (&List.to_tuple/1))
end
# примеры
def example do
table = [[2, 3, 3, 4],
[3, 4, 5],
[6, 7, 8],
[7, 9]]
x = 3
res = insert(table, x)
print_table res
end
def example2 do
[3,1, 2] |> from_word |> print_table
end
end