Skip to content

Instantly share code, notes, and snippets.

@pedrosnk
Created May 2, 2014 15:02
Show Gist options
  • Select an option

  • Save pedrosnk/ef8c2ea7c3a274f7ad22 to your computer and use it in GitHub Desktop.

Select an option

Save pedrosnk/ef8c2ea7c3a274f7ad22 to your computer and use it in GitHub Desktop.

Revisions

  1. pedrosnk created this gist May 2, 2014.
    24 changes: 24 additions & 0 deletions balance.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    defmodule Balance do
    def balance str do
    balance(str, 0)
    end
    defp balance str, parenthesis_count do
    if String.length(str) == 0 do
    parenthesis_count == 0
    else if parenthesis_count < 0 do
    false
    else
    {head, tail} = String.next_codepoint(str)
    parenthesis_count_acc = parenthesis_count
    cond do
    head == "(" ->
    parenthesis_count_acc = parenthesis_count_acc + 1
    head == ")" ->
    parenthesis_count_acc = parenthesis_count_acc - 1
    true ->
    end
    balance(tail, parenthesis_count_acc)
    end
    end
    end
    end
    20 changes: 20 additions & 0 deletions balance_suite_test.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    defmodule BalanceSuiteTest do
    use ExUnit.Case
    import Balance

    test 'balance: "(if (zero? x) max (/ 1 x))" is balanced' do
    assert balance("(if (zero? x) max (/ 1 x))")
    end

    test 'balance: "I told him ..." is balanced' do
    assert balance("I told him ...")
    end

    test 'balance: ":-)" is unbalanced' do
    assert !balance(":-)")
    end

    test 'balance: counting is not enough' do
    assert !balance("())(")
    end
    end
    17 changes: 17 additions & 0 deletions count_change.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    defmodule CountChange do

    def count_change money, change_list do
    cond do
    money == 0 ->
    1
    money < 0 ->
    0
    length(change_list) == 0 ->
    0
    true -> # else
    [head|tail] = change_list
    count_change(money - head, change_list) +
    count_change(money, tail)
    end
    end
    end
    21 changes: 21 additions & 0 deletions count_change_test.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    defmodule CountChangeTest do
    use ExUnit.Case
    import CountChange

    test 'countChange: example given in instructions' do
    assert count_change(4, [1,2]) == 3
    end

    test 'countChange: sorted CHF' do
    assert count_change(300, [5,10,20,50,100,200,500]) === 1022
    end

    test 'countChange: no pennies' do
    assert count_change(301, [5,10,20,50,100,200,500] ) === 0
    end

    test('countChange: unsorted CHF') do
    assert count_change(300, [500,5,50,100,20,200,10]) === 1022
    end

    end
    11 changes: 11 additions & 0 deletions pascal.ex
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    defmodule Pascal do

    def pascal column, row do
    if column == 0 || row == column do
    1
    else
    pascal(column, row - 1) + pascal( column - 1, row - 1 )
    end
    end

    end
    16 changes: 16 additions & 0 deletions pascal_suite_test.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    defmodule PascalSuiteTest do
    use ExUnit.Case
    import Pascal

    test 'pascal: col=0, row=2' do
    assert pascal(0,2) == 1
    end

    test 'pascal: col=1, row=2' do
    assert pascal(1,2) == 2
    end

    test 'pascal: col=1, row=3' do
    assert pascal(1,3) == 3
    end
    end