This week's code snippet, Longest Common Subsequence in Elixir, is brought to you by Subete and the Sample Programs repo.
defmodule LongestCommonSubsequence do
def main() do
lcs = solve(System.argv())
IO.puts("#{lcs}")
end
def solve([as, bs]) when is_bitstring(as) and is_bitstring(bs) do
lcs(parse_string_to_list_integer(as), parse_string_to_list_integer(bs)) |> Enum.join(", ")
end
def solve(_) do
print_usage()
end
def print_usage() do
~s(Usage: please provide two lists in the format "1, 2, 3, 4, 5")
end
def lcs([], _) do
[]
end
def lcs(_, []) do
[]
end
def lcs([a | as], [b | bs]) when a == b do
[a] ++ lcs(as, bs)
end
def lcs([a | as], [b | bs]) do
longest(lcs(as, [b | bs]), lcs([a | as], bs))
end
def longest(l1, l2) do
if Enum.count(l1) > Enum.count(l2) do
l1
else
l2
end
end
def parse_string_to_list_integer(xs) do
xs
|> String.trim()
|> String.split(",")
|> Enum.map(&String.to_integer(String.trim(&1)))
end
end
LongestCommonSubsequence.main()
Below you'll find an up-to-date list of articles by me on The Renegade Coder. For ease of browsing, emojis let you know the article category (i.e., blog: βοΈ, code: π», meta: π, teach: π)
- βοΈ Is Anyone Else Bothered by How Quickly We Adopted Generative AI?
- βοΈ 31 Lessons Learned as a New Dad
- π So Youβre Not Sure If Computer Science Is for You
- π You Should Give Open-Ended Projects to Your Students
- π» How to Move Your Extensions Folder in VS Code
- π Sample Programs Repo Celebrates 1,000 Code Snippets
- π Canvas Is Not Built With Educators in Mind
- π» Workshopping a Tier List Generator
- βοΈ No, The GRE Should Not Be Reinstated
- βοΈ Summarizing My Dissertation for the Layman
Also, here are some fun links you can use to support my work.
This document was automatically rendered on 2025-02-14 using SnakeMD.