Created
January 18, 2018 02:32
-
-
Save dwhelan/5fbfeffae48c32fb9b1b2d98095e3cbc to your computer and use it in GitHub Desktop.
Exercism.io solutions from Elixir Magic meetup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule NucleotideCount do | |
@nucleotides [?A, ?C, ?G, ?T] | |
@doc """ | |
Counts individual nucleotides in a NucleotideCount strand. | |
## Examples | |
iex> NucleotideCount.count('AATAA', ?A) | |
4 | |
iex> NucleotideCount.count('AATAA', ?T) | |
1 | |
""" | |
@spec count([char], char) :: non_neg_integer | |
def count(strand, nucleotide) do | |
Enum.count(strand, &(&1 == nucleotide)) | |
end | |
@doc """ | |
Returns a summary of counts by nucleotide. | |
## Examples | |
iex> NucleotideCount.histogram('AATAA') | |
%{?A => 4, ?T => 1, ?C => 0, ?G => 0} | |
""" | |
@spec histogram([char]) :: map | |
def histogram(strand) do | |
Enum.reduce(@nucleotides, %{}, fn n,acc -> Map.put(acc, n, count(strand,n)) end) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule RotationalCipher do | |
@doc """ | |
Given a plaintext and amount to shift by, return a rotated string. | |
Example: | |
iex> RotationalCipher.rotate("Attack at dawn", 13) | |
"Nggnpx ng qnja" | |
""" | |
@spec rotate(text :: String.t(), shift :: integer) :: String.t() | |
def rotate(text, shift) do | |
text | |
|> String.to_charlist | |
|> Enum.map(&shift_char(&1, shift)) | |
|> List.to_string | |
end | |
@spec shift_char(char :: char, shift :: integer) :: char | |
def shift_char(char, shift) when char in ?a..?z, do: ?a + rem(char + shift - ?a, 26) | |
def shift_char(char, shift) when char in ?A..?Z, do: ?A + rem(char + shift - ?A, 26) | |
def shift_char(char = _, _), do: char | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment