forked from cse5391-ff/a01-functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex03.ex
176 lines (121 loc) · 4.87 KB
/
ex03.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Code.compiler_options(ignore_module_conflict: true)
defmodule Ex03 do
require Integer
##############################################################################
# 3: 4 questions, 20 points available #
##############################################################################
# Rubric (per question): #
# #
# Does it pass tests?. . . . . . . . . . . . . . . . 60% #
# #
# Was is written using the given constraints? Is . . 20% #
# it free of any errors not found by the tests #
# #
# #
# Is it written in a functional, Elixir style? . . . 20% #
##############################################################################
@moduledoc """
All the exercises in this module should be solved using _named
functions_ (`def fname...`).
The `iex` sessions in the documentation block for each shows samples
of how the function should be used.
Remember you can load this code in `iex` from the command line and
play with it using:
$ iex ex03.ex
You can run tests using:
$ elixir ex03.ex
V V V V V V V V V V V V V V V V V
> > > USE NO LIBRARY FUNCTIONS UNLESS EXPLICITLY NOTED. < < < <
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
"""
##############################################################################
# 3.1: 5 points #
##################
@doc """
Write a function (or functions) that take a list of integers and
generates a new list of atoms, where the output list contains `:odd`
is the corresponding integer is odd, `:even` otherwise.
iex> Ex03.odd_even [ 1, 2, 4, 7, 9 ]
[ :odd, :even, :even, :odd, :odd ]
(The library functions `Integer.is_even` and `Integer.is_odd` may
be used if needed.)
"""
def odd_even . . . "your code"
##############################################################################
# 3.2: 5 points #
##################
@doc """
Write a function that returns true if a list contains a
given value, false otherwise.
iex> Ex03.list_contains([ 1, 2, 3, 4], 3)
true
iex> Ex03.list_contains([ 1, 2, 3, 4], 3)
true
iex> Ex03.list_contains([ 1, 2, 3, 4], 3)
true
"""
def list_contains . .. "your code"
##############################################################################
# 3.3: 5 points #
##################
@doc """
Two lists are equal if they contain the same number of elements,
and each element in one equals the corresponding element in
the other. Normally you use the operator `==` to test for this.
However, let's assume that `==` doesn't work for lists. Write
a function that checks for the equality of two lists. You don't
need to consider nested lists.
iex> Ex03.list_equal [ 1, 2, 3 ], [1, 2, 3]
true
iex> Ex03.list_equal [ 1, 2, 3 ], [1, 2, 3, 4]
false
iex> Ex03.list_equal [ 1, 2, 3 ], [3, 2, 1]
false
"""
def list_equal . . . "your code"
##############################################################################
# 3.4: 5 points #
##################
@doc """
Inspired by recent computer wins in the game of Go, your team
decides to push the boundaries of technology by implementing
a noughts-and-crosses playing program.
The team has decided to represent the board as a 9 element
tuple:
0 | 1 | 2
--+---+--
3 | 4 | 5 => { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
--+---+--
6 | 7 | 8
Each element of the tuple is set to :x or :o if the corresponding
player has occupied the square, or its number otherwise.
X | X |
--+---+--
| O | => { :x, :x, 3, 4, :o, 6, 7, :o, :x }
--+---+--
| O | X
There are eight winning positions (three horizontal, three vertical,
and two diagonal). Write a function that returns :x if X has won, :o
if O has won, and false otherwise.
iex> Ex03.won { :x, :o, 3, :x, 5, :o, :x, 8, :o }
:x
iex> Ex03.won { :o, :x, 3, :x, :o, 6, :x, 5, :o }
:o
iex> Ex03.won { :o, :x, 3, :x, :o, 6, :x, :o, 9 }
false
Think a little about a nice way to lay this code out.
"""
def won . . . "your code"
###########################
# IGNORE FROM HERE TO END #
###########################
@after_compile __MODULE__
def __after_compile__(_env, bytecode) do
File.write("Elixir.Ex03.beam", bytecode)
end
end
ExUnit.start
defmodule TestEx03 do
use ExUnit.Case
doctest Ex03
end