Created
May 2, 2014 15:02
-
-
Save pedrosnk/ef8c2ea7c3a274f7ad22 to your computer and use it in GitHub Desktop.
Problems from scala course to elixir
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment