Skip to content

Instantly share code, notes, and snippets.

View mcelaney's full-sized avatar

Brian E. McElaney mcelaney

View GitHub Profile
@mcelaney
mcelaney / jawn.ex
Created March 9, 2019 21:03
Example for Alex
# In an Elixir app using https://github.com/beatrichartz/csv
defmodule CSVDecoder do
def perform(content) do
Enum.reduce(content, %{}, &set_key/2)
end
defp set_key([key, name, status, email, joined, email_status, moderation_status], acc) do
Map.put(acc, key, %{
name: name,

Erlang

-module( geometry1).
-export([ test/ 0, area/ 1]).
test() ->
  12 = area({ rectangle, 3, 4}),
  144 = area({ square, 12}),
  tests_worked.
@mcelaney
mcelaney / with_example.ex
Last active May 27, 2017 05:06
With Example
module WebStore.Catalog.Product
@moduledoc """
This is a module that might sit inside of a Phoenix application in our
umbrella. We'll need to make calls to several contexts and merge the results
together if successful.
"""
alias Revzilla.CoreModel.{Assets, Images, Merchandizing, Skus, Videos}
alias Revzilla.Recommendation.SimilarProduct
alias Revzilla.WebStore.{AssetMapper, ImageMapper, ProductMapper}
@mcelaney
mcelaney / summit.ex
Last active March 21, 2017 13:42
March 21, 2017 PhillyDevSlack #Daily_Programmer
defmodule SumJawn do
@moduledoc """
As any number greater than 9 is represented by several digits, we can
calculate the sum of these digits. For example, for numbers 1492 and 1776
we get:
```1 + 4 + 9 + 2 = 16
1 + 7 + 7 + 6 = 21
```
@mcelaney
mcelaney / thing.ex
Last active March 20, 2017 19:46
March 20, 2017 PhillyDevSlack #DailyProgrammer
defmodule Thing do
@moduledoc """
Sort a string by cardinality of repeating characters, then alphabetically within each partition created by cardinality sorting.
An example: "abcdddeeeffff" would be sorted "ffffdddeeeabc"
In the above, 'f' appears the most(4), so it comes first, followed by 'd's and 'e's (3), in alaphabetical order, since they have equal cardinality. Then follow 'a', 'b', and 'c' in alphabetical order since they all have the same cardinality(1).
Another example: "cardinality" would be sorted "aaiicdlnrty"
"""
@mcelaney
mcelaney / sorter.ex
Last active March 16, 2017 19:33
PhillyDev Slack #DailyProgrammer March 16, 2017
defmodule Sorter do
@doc """
Takes a list and prints the list with the non zero index position before it was sorted.
## Example
iex> Sorter.sortit([34, 7, 38, 1])
"1 was in position 3"
"7 was in position 1"
"34 was in position 0"
"38 was in position 2"
@mcelaney
mcelaney / hamming_distance.ex
Created March 7, 2017 16:02
PhillyDev Slack #daily_programmer for March 7, 2017
defmodule HammingDistance do
@moduledoc """
Write a program that can calculate the Hamming distance between two DNA
strands.
A mutation is simply a mistake that occurs during the creation or copying of a
nucleic acid, in particular DNA. Because nucleic acids are vital to cellular
functions, mutations tend to cause a ripple effect throughout the cell.
Although mutations are technically mistakes, a very rare mutation may equip
the cell with a beneficial attribute. In fact, the macro effects of
@mcelaney
mcelaney / hamming.rb
Created March 7, 2017 15:46
PhillyDev Slack #Daily_Programmer for March 7, 2016
class HammingDistance
def self.hammit(left, right)
fail 'lengths don\'t match' if different_lengths?(left, right)
(0..compare_length(left, right)).map { |index|
left[index] == right[index]
}.select { |result|
result == false
}.count
end
@mcelaney
mcelaney / alpha_bet.ex
Last active March 2, 2017 14:34
PhillyDev Slack #daily_programmer for March 1, 2017
defmodule AplhaBet do
@moduledoc """
The word `chimps` is an example of a word that's in alphabetical order. Write
a program that prints out if the word is in alphabetical order. For bonus
make it test backwards too!
"""
@doc """
Returns true if the given string is in alphabetical or reverse-alphabetical
order
@mcelaney
mcelaney / magic_square.ex
Created March 2, 2017 03:44
PhillyDev Slack #daily_programmer for March 1, 2017
defmodule MagicSquare do
@moduledoc """
Today's daily programmer challenge is to test a magic square. This challenge
is stolen verbatim from r/dailyprogrammer. A 3x3 magic square is a 3x3 grid
of the numbers 1-9 such that each row, column, and major diagonal adds up to
15. Here's an example:
```
8 1 6
3 5 7
4 9 2