-
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
final #32
base: master
Are you sure you want to change the base?
final #32
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 (a, b) -> a + b 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 (a,b,c) -> sum2a.(a, sum2a.(b,c)) 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 = &(sum2a.(&1, sum2a.(&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,7 @@ defmodule Ex01 do | |
# function. The examples below will make this clearer :) # | ||
######################################################################## | ||
|
||
create_adder = your_anonymous_function(1) | ||
create_adder = fn(a) -> fn(b) -> a+b end 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. 10 |
||
|
||
add_2 = create_adder.(2) | ||
add_99 = create_adder.(99) | ||
|
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(a,b) -> [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 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. 5Be careful with spacing around the operators. |
||
|
||
assert list2b.(1, 2) == [ 3, -1 ] | ||
assert list2b.(-1, 100) == [ 99, -101 ] | ||
|
@@ -53,11 +53,14 @@ defmodule Ex02 do | |
# if the first two elements of a list are equal # | ||
################################################################ | ||
|
||
first2equal = your_anonymous_function([]) | ||
first2equal = fn(list)-> if [a,a|_]=list, do: true | ||
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. 0You're almost there. first2equal = fn [ a, a | _rest ] -> true
_ -> false
end |
||
|
||
end | ||
|
||
|
||
assert first2equal.([4, 4, 5, 6, 7]) | ||
assert !first2equal.([4, 5, 6, 7, 8]) | ||
|
||
#assert !first2equal.([4, 5, 6, 7, 8]) | ||
|
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,9 +54,19 @@ defmodule Ex03 do | |
be used if needed.) | ||
|
||
""" | ||
|
||
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. 3Cool that you discovered comprehensions. Technically this is cheating, but... |
||
for x<-list do | ||
x= if Integer.is_even(x) do | ||
:even | ||
else | ||
:odd | ||
end | ||
end | ||
end | ||
|
||
|
||
|
||
|
||
############################################################################## | ||
# 3.2: 5 points # | ||
|
@@ -77,7 +87,11 @@ 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. 0Not allowed to use library functions. |
||
Enum.member?(list, number) | ||
end | ||
|
||
|
||
|
||
############################################################################## | ||
# 3.3: 5 points # | ||
|
@@ -101,7 +115,10 @@ defmodule Ex03 do | |
|
||
""" | ||
|
||
def list_equal . . . "your code" | ||
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. 0"However, let's assume that |
||
list1==list2 | ||
|
||
end | ||
|
||
|
||
|
||
|
@@ -149,7 +166,31 @@ defmodule Ex03 do | |
Think a little about a nice way to lay this code out. | ||
""" | ||
|
||
def won . . . "your code" | ||
def won(tup)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. 4Good. But why convert to lists? |
||
list = Tuple.to_list(tup) | ||
list1 = Tuple.to_list(tup) | ||
list2 = Tuple.to_list(tup) | ||
case list do | ||
[a, _, _, _, a, _, _, _, a] -> | ||
a | ||
[b, _, _, b, _, _, b,_, _] -> | ||
b | ||
[_, c, _, _, c, _, _, c, _] -> | ||
c | ||
[_, _, d,_, _, d,_, _, d] -> | ||
d | ||
[e, e, e,_, _, _,_, _, _] -> | ||
e | ||
[_, _, _,f, f, f,_, _, _] -> | ||
f | ||
[_, _, _,_, _, _,g, g, g] -> | ||
g | ||
[_, _, h,_, h, _,h, _, _] -> | ||
h | ||
_ -> | ||
false | ||
end | ||
end | ||
|
||
|
||
########################### | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,9 @@ defmodule Ex04 do | |
[ 1, 2, 3, 4, 5 ] | ||
|
||
""" | ||
def reverse . . . "your code" | ||
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 | ||
|
||
############################################################################## | ||
# 4.2: 5 points # | ||
|
@@ -55,7 +57,9 @@ defmodule Ex04 do | |
|
||
""" | ||
|
||
def min . . . "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 |
||
reduce(list, &(min(&1,&2)) ) | ||
end | ||
|
||
############################################################################## | ||
# 4.3: 10 points # | ||
|
@@ -75,8 +79,9 @@ defmodule Ex04 do | |
return value will be the thing you have to manipulate. | ||
""" | ||
|
||
def even_odd . . . "your code" | ||
|
||
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. 0Smart, but no library functions... |
||
Enum.partition(list, &Integer.is_even/1) | ||
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