From 7dc2e3391720e5f875d730edbd212c2a22ae6d01 Mon Sep 17 00:00:00 2001 From: David Kim Date: Sun, 18 Sep 2016 15:14:01 -0500 Subject: [PATCH 1/3] finished 0,1,2 and half of 3 --- ex00.ex | 5 ++--- ex01.ex | 14 +++++++------- ex02.ex | 8 +++----- ex03.ex | 38 ++++++++++++++++++++++++++++++++++---- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/ex00.ex b/ex00.ex index 92fe8c8..fd16e3e 100644 --- a/ex00.ex +++ b/ex00.ex @@ -36,10 +36,9 @@ defmodule Ex00 do # Write a function that increments its numeric parameter # ########################################################## - inc = your_anonymous_function(1) + inc = fn(number) -> number + 1 end assert inc.(2) == 3 assert inc.(-1) == 0 + assert 3 == 3 end - - diff --git a/ex01.ex b/ex01.ex index ef38293..aec1e5c 100644 --- a/ex01.ex +++ b/ex01.ex @@ -36,7 +36,7 @@ defmodule Ex01 do # Write a function that adds two numbers using fn syntax # ########################################################## - sum2a = your_anonymous_function(1, 2) + sum2a = fn(num1, num2) -> num1 + num2 end assert sum2a.(1, 2) == 3 assert sum2a.(-1, 100) == 99 @@ -47,7 +47,7 @@ defmodule Ex01 do # Write a function that adds two numbers using & syntax # ########################################################## - sum2b = your_anonymous_function(1, 2) + sum2b = &(&1 + &2) assert sum2b.(1, 2) == 3 assert sum2b.(-1, 100) == 99 @@ -60,7 +60,7 @@ defmodule Ex01 do # no explicit + operators in your function # ##################################################################### - sum3a = your_anonymous_function(1, 2, 3) + sum3a = fn(num1, num2, num3) -> sum2b.(num1, sum2b.(num2, num3)) end assert sum3a.(1, 3, 5) == 9 assert sum3a.(1, -3, 5) == 3 @@ -71,7 +71,7 @@ defmodule Ex01 do # Do the same using the & notation # #################################### - sum3b = your_anonymous_function + sum3b = &(sum2b.(&1, sum2b.(&2, &3))) assert sum3b.(1, 3, 5) == 9 assert sum3b.(1, -3, 5) == 3 @@ -86,7 +86,9 @@ defmodule Ex01 do # function. The examples below will make this clearer :) # ######################################################################## - create_adder = your_anonymous_function(1) + create_adder = fn(add_num) -> + fn(num) -> num + add_num end + end add_2 = create_adder.(2) add_99 = create_adder.(99) @@ -95,5 +97,3 @@ defmodule Ex01 do assert add_99.(3) == 102 end - - diff --git a/ex02.ex b/ex02.ex index 4b7b6e1..e96e242 100644 --- a/ex02.ex +++ b/ex02.ex @@ -30,7 +30,7 @@ defmodule Ex02 do # numbers, and second should be the difference # ######################################################################## - list2a = your_anonymous_function(1, 2) + list2a = fn(num1, num2) -> [num1 + num2, num1 - num2] end assert list2a.(1, 2) == [ 3, -1 ] assert list2a.(-1, 100) == [ 99, -101 ] @@ -41,7 +41,7 @@ defmodule Ex02 do # Do the same using the & syntax # ################################## - list2b = your_anonymous_function(1, 2) + list2b = &([&1 + &2, &1 - &2]) assert list2b.(1, 2) == [ 3, -1 ] assert list2b.(-1, 100) == [ 99, -101 ] @@ -53,12 +53,10 @@ defmodule Ex02 do # if the first two elements of a list are equal # ################################################################ - first2equal = your_anonymous_function([]) + first2equal = fn([a, b | _]) -> a == b end assert first2equal.([4, 4, 5, 6, 7]) assert !first2equal.([4, 5, 6, 7, 8]) end - - diff --git a/ex03.ex b/ex03.ex index c6c13cb..17a1a59 100644 --- a/ex03.ex +++ b/ex03.ex @@ -55,7 +55,14 @@ defmodule Ex03 do """ - def odd_even . . . "your code" + def odd_even(list) do + Enum.map(list, fn(x) -> + cond do + rem(x, 2) == 0 -> :even + rem(x, 2) == 1 -> :odd + end + end) + end ############################################################################## @@ -77,7 +84,9 @@ defmodule Ex03 do """ - def list_contains . .. "your code" + def list_contains(list, number) do + Enum.member?(list, number) + end ############################################################################## # 3.3: 5 points # @@ -101,7 +110,9 @@ defmodule Ex03 do """ - def list_equal . . . "your code" + def list_equal(list1, list2) do + false + end @@ -149,8 +160,27 @@ defmodule Ex03 do Think a little about a nice way to lay this code out. """ - def won . . . "your code" + def won(moves) do + #vertical cases + cond do + + end + + end + end + + def check(type, index) do + cond do + type == :vertical -> + if index == index + 3 && index == index + 6 do + index + else + false + end + type == :horizontal + end + end ########################### # IGNORE FROM HERE TO END # From d0640bec0ab97c82cf945f1be825601ebd8a55b7 Mon Sep 17 00:00:00 2001 From: David Kim Date: Sun, 18 Sep 2016 21:44:03 -0500 Subject: [PATCH 2/3] ex03 complete and tested --- ex03.ex | 75 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/ex03.ex b/ex03.ex index 17a1a59..2f40dd5 100644 --- a/ex03.ex +++ b/ex03.ex @@ -55,14 +55,14 @@ defmodule Ex03 do """ - def odd_even(list) do - Enum.map(list, fn(x) -> - cond do - rem(x, 2) == 0 -> :even - rem(x, 2) == 1 -> :odd - end - end) - end +def odd_even(list) do + Enum.map(list, fn(x) -> + cond do + rem(x, 2) == 0 -> :even + rem(x, 2) == 1 -> :odd + end + end) +end ############################################################################## @@ -109,10 +109,24 @@ defmodule Ex03 do false """ - - def list_equal(list1, list2) do - false +# +# def list_equal(list1, list2) do +# cond do +# Enum.count(list1) != Enum.count(list2) -> false +# Enum.count(list1) == 0 -> true +# h1 == h2 -> list_equal(t1, t2) +# h1 != h2 -> false +# +# end +# end +def list_equal(list1, list2) do + cond do + length(list1) != length(list2) -> false + length(list1) == 0 -> true + Enum.at(list1, 0) == Enum.at(list2, 0) -> list_equal(Enum.drop(list1, 1), Enum.drop(list2, 1)) + true -> false end +end @@ -160,27 +174,28 @@ defmodule Ex03 do Think a little about a nice way to lay this code out. """ - def won(moves) do - #vertical cases - cond do - - - end - - end +def won(moves) do + false + cond do + #vertical cases + elem(moves, 0) == elem(moves, 3) && elem(moves, 0) == elem(moves, 6) -> elem(moves, 0) + elem(moves, 1) == elem(moves, 4) && elem(moves, 0) == elem(moves, 7) -> elem(moves, 1) + elem(moves, 2) == elem(moves, 5) && elem(moves, 0) == elem(moves, 8) -> elem(moves, 2) + + #horizontal cases + elem(moves, 0) == elem(moves, 1) && elem(moves, 0) == elem(moves, 2) -> elem(moves, 0) + elem(moves, 3) == elem(moves, 4) && elem(moves, 0) == elem(moves, 5) -> elem(moves, 3) + elem(moves, 6) == elem(moves, 7) && elem(moves, 0) == elem(moves, 8) -> elem(moves, 6) + + #diagonal cases + elem(moves, 0) == elem(moves, 4) && elem(moves, 0) == elem(moves, 8) -> elem(moves, 0) + elem(moves, 2) == elem(moves, 4) && elem(moves, 0) == elem(moves, 6) -> elem(moves, 2) + + #no winner case + true -> false end +end - def check(type, index) do - cond do - type == :vertical -> - if index == index + 3 && index == index + 6 do - index - else - false - end - type == :horizontal - end - end ########################### # IGNORE FROM HERE TO END # From ed830800f9f41509b829ac43890e36c89ca23608 Mon Sep 17 00:00:00 2001 From: David Kim Date: Sun, 18 Sep 2016 22:26:18 -0500 Subject: [PATCH 3/3] assignment complete --- ex04.ex | 142 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 75 insertions(+), 67 deletions(-) diff --git a/ex04.ex b/ex04.ex index 6730e64..30689f6 100644 --- a/ex04.ex +++ b/ex04.ex @@ -1,101 +1,109 @@ Code.compiler_options(ignore_module_conflict: true) defmodule Ex04 do - require Integer +require Integer - ############################################################################## - # 3: 3 questions, 20 points available # - ######################################## +############################################################################## +# 3: 3 questions, 20 points available # +######################################## - @doc """ - Here's a `reduce` function you can use in the following exercises. - You may not use any Elixir library functions unless specifically allowed - in the notes for an exercise. +@doc """ +Here's a `reduce` function you can use in the following exercises. +You may not use any Elixir library functions unless specifically allowed +in the notes for an exercise. - iex> Ex04.reduce [1, 2, 3, 4], 0, &(&1 + &2) - 10 + iex> Ex04.reduce [1, 2, 3, 4], 0, &(&1 + &2) + 10 - iex> Ex04.reduce [1, 2, 3, 4], &(&1 + &2) - 10 + iex> Ex04.reduce [1, 2, 3, 4], &(&1 + &2) + 10 - iex> Ex04.reduce [1, 2, 3, 4], [], &[ &1 | &2 ] - [ 4, 3, 2, 1 ] + iex> Ex04.reduce [1, 2, 3, 4], [], &[ &1 | &2 ] + [ 4, 3, 2, 1 ] - """ +""" - def reduce([ h | t ], func), do: reduce(t, h, func) - def reduce([ ], state, _func), do: state - def reduce([ h | t ], state, func), do: reduce(t, func.(h, state), func) +def reduce([ h | t ], func), do: reduce(t, h, func) +def reduce([ ], state, _func), do: state +def reduce([ h | t ], state, func), do: reduce(t, func.(h, state), func) - ############################################################################## - # 4.1: 5 points # - ################## +############################################################################## +# 4.1: 5 points # +################## - @doc """ - Use `reduce` to reverse a list. (there's a hint above) +@doc """ +Use `reduce` to reverse a list. (there's a hint above) - iex> Ex04.reverse [ 5, 4, 3, 2, 1 ] - [ 1, 2, 3, 4, 5 ] + iex> Ex04.reverse [ 5, 4, 3, 2, 1 ] + [ 1, 2, 3, 4, 5 ] - """ - def reverse . . . "your code" +""" - ############################################################################## - # 4.2: 5 points # - ################## - @doc """ - Use `reduce` to find the minimum of a list of numbers. - - iex> Ex04.min [ 5, 2, 7, 9 ] - 2 - - iex> Ex04.min [ 5, 2, -7, 9 ] - -7 - - """ +def reverse(list) do + reduce(list, [], &[ &1|&2 ]) +end - def min . . . "your code" +############################################################################## +# 4.2: 5 points # +################## +@doc """ +Use `reduce` to find the minimum of a list of numbers. - ############################################################################## - # 4.3: 10 points # - ################## - @doc """ - Given a list of integers, return a two element tuple. The first element - is a list of the even numbers from the list. The second is a list of - the odd numbers. You may use the library functions `Integer.is_even` and - `Integer.is_odd`, and you can use the `reverse` function you defined - above. Feel free to write helper functions if you want. + iex> Ex04.min [ 5, 2, 7, 9 ] + 2 - iex> Ex04.even_odd [ 1, 2, 3, 4, 5 ] - { [ 2, 4], [ 1, 3, 5 ] } + iex> Ex04.min [ 5, 2, -7, 9 ] + -7 - Hint: you're taking a list and converting it into something else. What function - helps you do that. And, if you use that function, what does it return? That - return value will be the thing you have to manipulate. - """ +""" - def even_odd . . . "your code" +def min(list) do + #return whichever value is lower + reduce(list, &(if &1 < &2, do: &1, else: &2)) +end +############################################################################## +# 4.3: 10 points # +################## +@doc """ +Given a list of integers, return a two element tuple. The first element +is a list of the even numbers from the list. The second is a list of +the odd numbers. You may use the library functions `Integer.is_even` and +`Integer.is_odd`, and you can use the `reverse` function you defined +above. Feel free to write helper functions if you want. + + iex> Ex04.even_odd [ 1, 2, 3, 4, 5 ] + { [ 2, 4], [ 1, 3, 5 ] } + +Hint: you're taking a list and converting it into something else. What function +helps you do that. And, if you use that function, what does it return? That +return value will be the thing you have to manipulate. +""" + +def even_odd(list) do + reduce(list, {[], []}, &(if Integer.is_even(&1), + do: {elem(&2, 0) ++ [&1], elem(&2, 1)}, #return {new list, old list} + else: {elem(&2, 0), elem(&2, 1) ++ [&1]})) #return {old list, new list} +end - ########################### - # IGNORE FROM HERE TO END # - ########################### +########################### +# IGNORE FROM HERE TO END # +########################### - @after_compile __MODULE__ +@after_compile __MODULE__ - def __after_compile__(_env, bytecode) do - File.write("Elixir.Ex04.beam", bytecode) - end +def __after_compile__(_env, bytecode) do + File.write("Elixir.Ex04.beam", bytecode) +end end ExUnit.start defmodule TestEx04 do - use ExUnit.Case - doctest Ex04 +use ExUnit.Case +doctest Ex04 end -