Created
March 22, 2016 01:32
-
-
Save hugoestr/f299fedf7d6994b3f1fe to your computer and use it in GitHub Desktop.
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 Htest do | |
def equals(message, expect, value) do | |
IO.puts "#{message}: #{expect == value} " | |
end | |
end | |
defmodule Qsort do | |
def qsort([]), do: [] #base case for qsort | |
def qsort([pivot|[]]), do: [pivot] #base case for qsort | |
def qsort([pivot|tail]) do #how to get head and tail from | |
lower = Enum.filter(tail, fn(n) -> n < pivot end) #How to filter | |
higher = Enum.filter(tail, fn(n) -> n > pivot end) | |
qsort(lower) ++ [pivot] ++ qsort(higher) | |
end | |
end | |
# Test setup | |
list = [ 6, 1, 3, 5, 9, 8, 4, 7, 2] | |
result = Qsort.qsort list | |
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
# learned how to inspect in elixir | |
# IO.inspect result | |
IO.puts "testing" | |
Htest.equals "the list is sorted", expected, result | |
IO.inspect result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment