-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
David kim #21
base: master
Are you sure you want to change the base?
David kim #21
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
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) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 10 |
||
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 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
|
||
|
||
assert first2equal.([4, 4, 5, 6, 7]) | ||
assert !first2equal.([4, 5, 6, 7, 8]) | ||
|
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,14 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def odd_even . . . "your code" | ||
def odd_even(list) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0"Use no library functions unless otherwise directed" |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0 |
||
Enum.member?(list, number) | ||
end | ||
|
||
############################################################################## | ||
# 3.3: 5 points # | ||
|
@@ -100,8 +109,24 @@ defmodule Ex03 do | |
false | ||
|
||
""" | ||
|
||
def list_equal . . . "your code" | ||
# | ||
# 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0Even if you were allowed to use Enum.at, it is generally a smell to use it with lists. |
||
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 | ||
|
||
|
||
|
||
|
@@ -149,7 +174,27 @@ defmodule Ex03 do | |
Think a little about a nice way to lay this code out. | ||
""" | ||
|
||
def won . . . "your code" | ||
def won(moves) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3This is really procedural code. think how it might look with pattern matching: def won(position) do
case position do
{ a, a, a,
_, _, _,
_, _, _ } -> a
{ _, _, _,
a, a, a,
_, _, _ } -> a
{ _, _, _,
_, _, _,
a, a, a } -> a
{ a, _, _,
a, _, _,
a, _, _ } -> a
{ _, a, _,
_, a, _,
_, a, _ } -> a
{ _, _, a,
_, _, a,
_, _, a } -> a
{ a, _, _,
_, a, _,
_, _, a } -> a
{ _, _, a,
_, a, _,
a, _, _ } -> a
_ -> false
end
|
||
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 | ||
|
||
|
||
########################### | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 |
||
#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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5Several things hurt your mark here. First, the partitioning is crying out to be in a new function. Second, you're using elem and list concatenation rather that pattern matching. Here's an alternative; def even_odd(list) do
reduce(list, { [], [] }, &assign_one_even_odd/2)
|> reverse_two_lists
end
defp assign_one_even_odd(number, { evens, odds }) do
cond do
Integer.is_even(number)->
{ [ number | evens ], odds }
Integer.is_odd(number) ->
{ evens, [ number | odds ] }
end
end
defp reverse_two_lists({ evens, odds }) do
{ reverse(evens), reverse(odds) }
end |
||
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5