Data Structures and Algorithms
03/.DS_Store
__MACOSX/03/._.DS_Store
03/A2/.DS_Store
__MACOSX/03/A2/._.DS_Store
03/A2/build.xml
Some useful functions for working with pipes.
__MACOSX/03/A2/._build.xml
03/A2/src/.DS_Store
__MACOSX/03/A2/src/._.DS_Store
03/A2/src/nz/.DS_Store
__MACOSX/03/A2/src/nz/._.DS_Store
03/A2/src/nz/retro_freedom/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/._.DS_Store
03/A2/src/nz/retro_freedom/dsa2016/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/._.DS_Store
03/A2/src/nz/retro_freedom/dsa2016/pair/Pair.java
03/A2/src/nz/retro_freedom/dsa2016/pair/Pair.java
package
nz
.
retro_freedom
.
dsa2016
.
pair
;
import
java
.
util
.
*
;
/**
*
@author
*
@version
1.0
*
* A data type representing generic, non-{
@code
null} pairs.
* Pairs cannot be modified once created - if you need to change a pair,
* make a new one instead.
*/
public
class
Pair
<
T1
,
T2
>
{
/**
* Makes a new pair.
*
*
@param
left the left side of the pair (cannot be {
@code
null}).
*
@param
right the right side of the pair (cannot be {
@code
null}).
*
@return
a new pair with the given left and right sides.
*
@throws
NullPointerException if left or right are {
@code
null}.
*/
public
static
<
T1
,
T2
>
Pair
<
T1
,
T2
>
make
(
T1 left
,
T2 right
)
{
Objects
.
requireNonNull
(
left
,
"left cannot be null."
);
Objects
.
requireNonNull
(
right
,
"right cannot be null."
);
return
new
Pair
<
T1
,
T2
>
(
left
,
right
);
}
/**
* Convenience factory for replacing left side of an existing pair.
*
*
@param
p a pair (cannot be {
@code
null}).
*
@param
left a new left element (cannot be {
@code
null}).
*
@return
a new pair which is the same as p, but with a new left element.
*
@throws
NullPointerException if an argument is {
@code
null}.
*/
public
static
<
T1
,
T2
>
Pair
<
T1
,
T2
>
newLeft
(
Pair
<
T1
,
T2
>
p
,
T1 left
)
{
Objects
.
requireNonNull
(
p
,
"p cannot be null."
);
return
make
(
left
,
p
.
right
());
}
/**
* Convenience factory for replacing right side of an existing pair.
*
*
@param
p a pair (cannot be {
@code
null}).
*
@param
right a new right element (cannot be {
@code
null}).
*
@return
a new pair which is the same as p, but with a new right element.
*
@throws
NullPointerException if an argument is {
@code
null}.
*/
public
static
<
T1
,
T2
>
Pair
<
T1
,
T2
>
newRight
(
Pair
<
T1
,
T2
>
p
,
T2 right
)
{
Objects
.
requireNonNull
(
p
,
"p cannot be null."
);
return
make
(
p
.
left
(),
right
);
}
/**
* Compares on both elements.
*
*
@param
o a thing to compare to.
*
@return
true if both sides of both pairs match.
*/
@
Override
public
boolean
equals
(
Object
o
)
{
if
(
o
instanceof
Pair
<?
,
?>
)
{
Pair
<?
,
?>
p
=
(
Pair
<?
,
?>
)
o
;
if
(
left
.
equals
(
p
.
left
)
&&
right
.
equals
(
p
.
right
))
{
return
true
;
}
}
return
false
;
}
/**
* Hashes on both sides.
*
*
@return
a unique code representing a pair.
*/
@
Override
public
int
hashCode
()
{
int
result
=
1
;
result
=
37
*
result
+
left
.
hashCode
();
result
=
37
*
result
+
right
.
hashCode
();
return
result
;
}
/**
* Gives a debugging representation.
*
@return
an implementation-defined debug string.
*/
@
Override
public
String
toString
()
{
return
"("
+
left
.
toString
()
+
", "
+
right
.
toString
()
+
")"
;
}
/**
* Fetches the left side of the pair.
*
@return
the left side of the pair.
*/
public
T1 left
()
{
return
left
;}
/**
* Fetches the right side of the pair.
*
@return
the right side of the pair.
*/
public
T2 right
()
{
return
right
;}
// Private stuff
private
final
T1 left
;
private
final
T2 right
;
private
Pair
(
T1 left
,
T2 right
)
{
this
.
left
=
left
;
this
.
right
=
right
;
}
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pair/._Pair.java
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/._pair
03/A2/src/nz/retro_freedom/dsa2016/pipes/.DS_Store
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._.DS_Store
03/A2/src/nz/retro_freedom/dsa2016/pipes/Pipes.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Pipes.java
package
nz
.
retro_freedom
.
dsa2016
.
pipes
;
import
java
.
util
.
*
;
import
nz
.
retro_freedom
.
dsa2016
.
pair
.
*
;
/**
*
@author
*
@version
1.0
*
* A library for working with pipes.
*/
public
class
Pipes
{
/**
* Retrieves the first element of a list, in O(1) time.
*
*
@param
list the list (cannot be {
@code
null} or empty).
*
@return
the first element of the argument.
*
@throws
NullPointerException if the list is {
@code
null}.
*
@throws
IllegalArgumentException if the list is empty.
*/
public
static
<
T
>
T head
(
LinkedList
<
T
>
list
)
{
Objects
.
requireNonNull
(
list
,
"list cannot be null."
);
try
{
return
list
.
getFirst
();
}
catch
(
NoSuchElementException
e
)
{
throw
new
IllegalArgumentException
(
"list cannot be empty."
);
}
}
/**
* Retrieves all but the first element of the argument, in O(1) time.
* Will yield an empty list if given a list with just one element.
*
*
@param
list the list.
*
@return
a new list containing everything but the first element of the
* argument.
*
@throws
NullPointerException if the list is {
@code
null}.
*
@throws
IllegalArgumentException if the list is empty.
*/
public
static
<
T
>
LinkedList
<
T
>
tail
(
LinkedList
<
T
>
list
)
{
Objects
.
requireNonNull
(
list
,
"list cannot be null."
);
try
{
return
new
LinkedList
<>
(
list
.
subList
(
1
,
list
.
size
()));
}
catch
(
IllegalArgumentException
e
)
{
throw
new
IllegalArgumentException
(
"list cannot be empty."
);
}
}
/**
* Returns a new list with e as its head, and list as its tail.
*
*
@param
e an element (cannot be {
@code
null}).
*
@param
list a list to serve as the tail (cannot be {
@code
null}).
*
@return
a new list with e as its head, and list as its tail.
*
@throws
NullPointerException if the list is {
@code
null}.
*/
public
static
<
T
>
LinkedList
<
T
>
cons
(
T e
,
LinkedList
<
T
>
list
)
{
Objects
.
requireNonNull
(
e
,
"e cannot be null."
);
Objects
.
requireNonNull
(
list
,
"list cannot be null."
);
LinkedList
<
T
>
ll
=
new
LinkedList
<>
(
list
);
ll
.
addFirst
(
e
);
return
ll
;
}
/**
* Decomposes a list into its head and tail.
*
*
@param
list a list (cannot be {
@code
null} or empty).
*
@return
a pair of the head and tail of the list.
*
@throws
NullPointerException if the list is {
@code
null}.
*
@throws
IllegalArgumentException if the list is empty.
*/
public
static
<
T
>
Pair
<
T
,
LinkedList
<
T
>>
uncons
(
LinkedList
<
T
>
list
)
{
return
Pair
.
make
(
Pipes
.
head
(
list
),
Pipes
.
tail
(
list
));
}
/**
* Converts a list into another list by repeated application of a conversion
* rule.
*
*
@param
trans a transformer for elements of the list (cannot be
* {
@code
null}).
*
@param
list a list (cannot be {
@code
null}).
*
@return
a new list, defined as every element of the argument list, with
* the given transformation applied to the elements in the same order.
*
@throws
NullPointerException if any argument is {
@code
null}.
*/
public
static
<
From
,
To
>
LinkedList
<
To
>
map
(
Transformer
<
From
,
To
>
trans
,
LinkedList
<
From
>
list
)
{
Objects
.
requireNonNull
(
trans
,
"trans cannot be null"
);
Objects
.
requireNonNull
(
list
,
"list cannot be null"
);
if
(
list
.
isEmpty
())
{
return
new
LinkedList
<
To
>
();
}
To
t
=
trans
.
transform
(
Pipes
.
head
(
list
));
LinkedList
<
To
>
rest
=
Pipes
.
map
(
trans
,
Pipes
.
tail
(
list
));
return
Pipes
.
cons
(
t
,
rest
);
}
/**
* Removes elements from a list if they don't fit a rule.
*
*
@param
pred a predicate defining the condition for keeping elements
* (cannot be {
@code
null}).
*
@param
list a list (cannot be {
@code
null}).
*
@return
a new list containing only those elements from the argument
* list for which the given predicate is true.
*
@throws
NullPointerException if any argument is {
@code
null}.
*/
public
static
<
T
>
LinkedList
<
T
>
filter
(
Predicate
<
T
>
pred
,
LinkedList
<
T
>
list
)
{
Objects
.
requireNonNull
(
pred
,
"pred cannot be null"
);
Objects
.
requireNonNull
(
list
,
"list cannot be null"
);
if
(
list
.
isEmpty
())
{
return
new
LinkedList
<
T
>
();
}
T e
=
Pipes
.
head
(
list
);
LinkedList
<
T
>
rest
=
Pipes
.
filter
(
pred
,
Pipes
.
tail
(
list
));
if
(
pred
.
is
(
e
))
{
return
Pipes
.
cons
(
e
,
rest
);
}
return
rest
;
}
/**
* 'Boils down' a list into a single value using the given combination rule.
*
*
@param
start an initial value (cannot be {
@code
null}).
*
@param
red a combination rule for combining elements in the list
* (cannot be {
@code
null}).
*
@param
list a list (cannot be {
@code
null}).
*
@return
a 'boiled-down' value based on combining all list elements and
* the starting value in the same order as the argument list.
*
@throws
NullPointerException if any argument is {
@code
null}.
*/
public
static
<
From
,
To
>
To
reduce
(
To
start
,
Reducer
<
From
,
To
>
red
,
LinkedList
<
From
>
list
)
{
Objects
.
requireNonNull
(
start
,
"start cannot be null."
);
Objects
.
requireNonNull
(
red
,
"red cannot be null."
);
Objects
.
requireNonNull
(
list
,
"list cannot be null."
);
if
(
list
.
isEmpty
())
{
return
start
;
}
To
combined
=
red
.
combine
(
start
,
Pipes
.
head
(
list
));
return
Pipes
.
reduce
(
combined
,
red
,
Pipes
.
tail
(
list
));
}
// Private stuff
// disable constructor
private
Pipes
()
{}
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Pipes.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Predicate.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Predicate.java
package
nz
.
retro_freedom
.
dsa2016
.
pipes
;
/**
*
*
@version
1.0
*
* An interface for defining predicates (functions which return true or
* false).
*/
public
interface
Predicate
<
T
>
{
/**
* The core driver of this interface.
*
*
@param
in the input element.
*
@return
true if the element meets the conditions of the predicate.
*/
public
boolean
is
(
T in
);
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Predicate.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Reducer.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Reducer.java
package
nz
.
retro_freedom
.
dsa2016
.
pipes
;
/**
*
*
@version
1.0
*
* An interface for defining reducers (functions which take two items and
* combine them into one).
*/
public
interface
Reducer
<
From
,
To
>
{
/**
* The core driver of this interface.
*
*
@param
x the first input element.
*
@param
y the second input element.
*
@return
the combination of x and y.
*/
public
To
combine
(
To
x
,
From
y
);
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Reducer.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Transformer.java
03/A2/src/nz/retro_freedom/dsa2016/pipes/Transformer.java
package
nz
.
retro_freedom
.
dsa2016
.
pipes
;
/**
*
*
@version
1.0
*
* An interface for defining transformers (functions which take one thing and
* emit another).
*/
public
interface
Transformer
<
From
,
To
>
{
/**
* Transforms its input.
*
@param
in the input.
*
@return
the transformed input.
*/
public
To
transform
(
From
in
);
}
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/pipes/._Transformer.java
__MACOSX/03/A2/src/nz/retro_freedom/dsa2016/._pipes
__MACOSX/03/A2/src/nz/retro_freedom/._dsa2016
__MACOSX/03/A2/src/nz/._retro_freedom
__MACOSX/03/A2/src/._nz
__MACOSX/03/A2/._src
__MACOSX/03/._A2
03/A3/.DS_Store
__MACOSX/03/A3/._.DS_Store
03/A3/build.xml
Crunching some delicious data.
__MACOSX/03/A3/._build.xml
03/A3/data/iris.csv
| 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | Iris-setosa |
| 4.6 | 3.4 | 1.4 | 0.3 | Iris-setosa |
| 5.0 | 3.4 | 1.5 | 0.2 | Iris-setosa |
| 4.4 | 2.9 | 1.4 | 0.2 | Iris-setosa |
| 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa |
| 5.4 | 3.7 | 1.5 | 0.2 | Iris-setosa |
| 4.8 | 3.4 | 1.6 | 0.2 | Iris-setosa |
| 4.8 | 3.0 | 1.4 | 0.1 | Iris-setosa |
| 4.3 | 3.0 | 1.1 | 0.1 | Iris-setosa |
| 5.8 | 4.0 | 1.2 | 0.2 | Iris-setosa |
| 5.7 | 4.4 | 1.5 | 0.4 | Iris-setosa |
| 5.4 | 3.9 | 1.3 | 0.4 | Iris-setosa |
| 5.1 | 3.5 | 1.4 | 0.3 | Iris-setosa |
| 5.7 | 3.8 | 1.7 | 0.3 | Iris-setosa |
| 5.1 | 3.8 | 1.5 | 0.3 | Iris-setosa |
| 5.4 | 3.4 | 1.7 | 0.2 | Iris-setosa |
| 5.1 | 3.7 | 1.5 | 0.4 | Iris-setosa |
| 4.6 | 3.6 | 1.0 | 0.2 | Iris-setosa |
| 5.1 | 3.3 | 1.7 | 0.5 | Iris-setosa |
| 4.8 | 3.4 | 1.9 | 0.2 | Iris-setosa |
| 5.0 | 3.0 | 1.6 | 0.2 | Iris-setosa |
| 5.0 | 3.4 | 1.6 | 0.4 | Iris-setosa |
| 5.2 | 3.5 | 1.5 | 0.2 | Iris-setosa |
| 5.2 | 3.4 | 1.4 | 0.2 | Iris-setosa |
| 4.7 | 3.2 | 1.6 | 0.2 | Iris-setosa |
| 4.8 | 3.1 | 1.6 | 0.2 | Iris-setosa |
| 5.4 | 3.4 | 1.5 | 0.4 | Iris-setosa |
| 5.2 | 4.1 | 1.5 | 0.1 | Iris-setosa |
| 5.5 | 4.2 | 1.4 | 0.2 | Iris-setosa |
| 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa |
| 5.0 | 3.2 | 1.2 | 0.2 | Iris-setosa |
| 5.5 | 3.5 | 1.3 | 0.2 | Iris-setosa |
| 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa |
| 4.4 | 3.0 | 1.3 | 0.2 | Iris-setosa |
| 5.1 | 3.4 | 1.5 | 0.2 | Iris-setosa |
| 5.0 | 3.5 | 1.3 | 0.3 | Iris-setosa |
| 4.5 | 2.3 | 1.3 | 0.3 | Iris-setosa |
| 4.4 | 3.2 | 1.3 | 0.2 | Iris-setosa |
| 5.0 | 3.5 | 1.6 | 0.6 | Iris-setosa |
| 5.1 | 3.8 | 1.9 | 0.4 | Iris-setosa |
| 4.8 | 3.0 | 1.4 | 0.3 | Iris-setosa |
| 5.1 | 3.8 | 1.6 | 0.2 | Iris-setosa |
| 4.6 | 3.2 | 1.4 | 0.2 | Iris-setosa |
| 5.3 | 3.7 | 1.5 | 0.2 | Iris-setosa |
| 5.0 | 3.3 | 1.4 | 0.2 | Iris-setosa |
| 7.0 | 3.2 | 4.7 | 1.4 | Iris-versicolor |
| 6.4 | 3.2 | 4.5 | 1.5 | Iris-versicolor |
| 6.9 | 3.1 | 4.9 | 1.5 | Iris-versicolor |
| 5.5 | 2.3 | 4.0 | 1.3 | Iris-versicolor |
| 6.5 | 2.8 | 4.6 | 1.5 | Iris-versicolor |
| 5.7 | 2.8 | 4.5 | 1.3 | Iris-versicolor |
| 6.3 | 3.3 | 4.7 | 1.6 | Iris-versicolor |
| 4.9 | 2.4 | 3.3 | 1.0 | Iris-versicolor |
| 6.6 | 2.9 | 4.6 | 1.3 | Iris-versicolor |
| 5.2 | 2.7 | 3.9 | 1.4 | Iris-versicolor |
| 5.0 | 2.0 | 3.5 | 1.0 | Iris-versicolor |
| 5.9 | 3.0 | 4.2 | 1.5 | Iris-versicolor |
| 6.0 | 2.2 | 4.0 | 1.0 | Iris-versicolor |
| 6.1 | 2.9 | 4.7 | 1.4 | Iris-versicolor |
| 5.6 | 2.9 | 3.6 | 1.3 | Iris-versicolor |
| 6.7 | 3.1 | 4.4 | 1.4 | Iris-versicolor |
| 5.6 | 3.0 | 4.5 | 1.5 | Iris-versicolor |
| 5.8 | 2.7 | 4.1 | 1.0 | Iris-versicolor |
| 6.2 | 2.2 | 4.5 | 1.5 | Iris-versicolor |
| 5.6 | 2.5 | 3.9 | 1.1 | Iris-versicolor |
| 5.9 | 3.2 | 4.8 | 1.8 | Iris-versicolor |
| 6.1 | 2.8 | 4.0 | 1.3 | Iris-versicolor |
| 6.3 | 2.5 | 4.9 | 1.5 | Iris-versicolor |
| 6.1 | 2.8 | 4.7 | 1.2 | Iris-versicolor |
| 6.4 | 2.9 | 4.3 | 1.3 | Iris-versicolor |
| 6.6 | 3.0 | 4.4 | 1.4 | Iris-versicolor |
| 6.8 | 2.8 | 4.8 | 1.4 | Iris-versicolor |
| 6.7 | 3.0 | 5.0 | 1.7 | Iris-versicolor |
| 6.0 | 2.9 | 4.5 | 1.5 | Iris-versicolor |
| 5.7 | 2.6 | 3.5 | 1.0 | Iris-versicolor |
| 5.5 | 2.4 | 3.8 | 1.1 | Iris-versicolor |
| 5.5 | 2.4 | 3.7 | 1.0 | Iris-versicolor |
| 5.8 | 2.7 | 3.9 | 1.2 | Iris-versicolor |
| 6.0 | 2.7 | 5.1 | 1.6 | Iris-versicolor |
| 5.4 | 3.0 | 4.5 | 1.5 | Iris-versicolor |
| 6.0 | 3.4 | 4.5 | 1.6 | Iris-versicolor |
| 6.7 | 3.1 | 4.7 | 1.5 | Iris-versicolor |
| 6.3 | 2.3 | 4.4 | 1.3 | Iris-versicolor |
| 5.6 | 3.0 | 4.1 | 1.3 | Iris-versicolor |
| 5.5 | 2.5 | 4.0 | 1.3 | Iris-versicolor |
| 5.5 | 2.6 | 4.4 | 1.2 | Iris-versicolor |
| 6.1 | 3.0 | 4.6 | 1.4 | Iris-versicolor |
| 5.8 | 2.6 | 4.0 | 1.2 | Iris-versicolor |
| 5.0 | 2.3 | 3.3 | 1.0 | Iris-versicolor |
| 5.6 | 2.7 | 4.2 | 1.3 | Iris-versicolor |
| 5.7 | 3.0 | 4.2 | 1.2 | Iris-versicolor |
| 5.7 | 2.9 | 4.2 | 1.3 | Iris-versicolor |
| 6.2 | 2.9 | 4.3 | 1.3 | Iris-versicolor |
| 5.1 | 2.5 | 3.0 | 1.1 | Iris-versicolor |
| 5.7 | 2.8 | 4.1 | 1.3 | Iris-versicolor |
| 6.3 | 3.3 | 6.0 | 2.5 | Iris-virginica |
| 5.8 | 2.7 | 5.1 | 1.9 | Iris-virginica |
| 7.1 | 3.0 | 5.9 | 2.1 | Iris-virginica |
| 6.3 | 2.9 | 5.6 | 1.8 | Iris-virginica |
| 6.5 | 3.0 | 5.8 | 2.2 | Iris-virginica |
| 7.6 | 3.0 | 6.6 | 2.1 | Iris-virginica |
| 4.9 | 2.5 | 4.5 | 1.7 | Iris-virginica |
| 7.3 | 2.9 | 6.3 | 1.8 | Iris-virginica |
| 6.7 | 2.5 | 5.8 | 1.8 | Iris-virginica |
| 7.2 | 3.6 | 6.1 | 2.5 | Iris-virginica |
| 6.5 | 3.2 | 5.1 | 2.0 | Iris-virginica |
| 6.4 | 2.7 | 5.3 | 1.9 | Iris-virginica |
| 6.8 | 3.0 | 5.5 | 2.1 | Iris-virginica |
| 5.7 | 2.5 | 5.0 | 2.0 | Iris-virginica |
| 5.8 | 2.8 | 5.1 | 2.4 | Iris-virginica |
| 6.4 | 3.2 | 5.3 | 2.3 | Iris-virginica |
| 6.5 | 3.0 | 5.5 | 1.8 | Iris-virginica |
| 7.7 | 3.8 | 6.7 | 2.2 | Iris-virginica |
| 7.7 | 2.6 | 6.9 | 2.3 | Iris-virginica |
| 6.0 | 2.2 | 5.0 | 1.5 | Iris-virginica |
| 6.9 | 3.2 | 5.7 | 2.3 | Iris-virginica |
| 5.6 | 2.8 | 4.9 | 2.0 | Iris-virginica |
| 7.7 | 2.8 | 6.7 | 2.0 | Iris-virginica |
| 6.3 | 2.7 | 4.9 | 1.8 | Iris-virginica |
| 6.7 | 3.3 | 5.7 | 2.1 | Iris-virginica |
| 7.2 | 3.2 | 6.0 | 1.8 | Iris-virginica |
| 6.2 | 2.8 | 4.8 | 1.8 | Iris-virginica |
| 6.1 | 3.0 | 4.9 | 1.8 | Iris-virginica |
| 6.4 | 2.8 | 5.6 | 2.1 | Iris-virginica |
| 7.2 | 3.0 | 5.8 | 1.6 | Iris-virginica |
| 7.4 | 2.8 | 6.1 | 1.9 | Iris-virginica |
| 7.9 | 3.8 | 6.4 | 2.0 | Iris-virginica |
| 6.4 | 2.8 | 5.6 | 2.2 | Iris-virginica |
| 6.3 | 2.8 | 5.1 | 1.5 | Iris-virginica |
| 6.1 | 2.6 | 5.6 | 1.4 | Iris-virginica |
| 7.7 | 3.0 | 6.1 | 2.3 | Iris-virginica |
| 6.3 | 3.4 | 5.6 | 2.4 | Iris-virginica |
| 6.4 | 3.1 | 5.5 | 1.8 | Iris-virginica |
| 6.0 | 3.0 | 4.8 | 1.8 | Iris-virginica |
| 6.9 | 3.1 | 5.4 | 2.1 | Iris-virginica |
| 6.7 | 3.1 | 5.6 | 2.4 | Iris-virginica |
| 6.9 | 3.1 | 5.1 | 2.3 | Iris-virginica |
| 5.8 | 2.7 | 5.1 | 1.9 | Iris-virginica |
| 6.8 | 3.2 | 5.9 | 2.3 | Iris-virginica |
| 6.7 | 3.3 | 5.7 | 2.5 | Iris-virginica |
| 6.7 | 3.0 | 5.2 | 2.3 | Iris-virginica |
| 6.3 | 2.5 | 5.0 | 1.9 | Iris-virginica |
| 6.5 | 3.0 | 5.2 | 2.0 | Iris-virginica |
| 6.2 | 3.4 | 5.4 | 2.3 | Iris-virginica |
| 5.9 | 3.0 | 5.1 | 1.8 | Iris-virginica |
__MACOSX/03/A3/data/._iris.csv
__MACOSX/03/A3/._data
03/A3/src/.DS_Store
__MACOSX/03/A3/src/._.DS_Store
03/A3/src/nz/.DS_Store
__MACOSX/03/A3/src/nz/._.DS_Store
03/A3/src/nz/retro_freedom/.DS_Store
__MACOSX/03/A3/src/nz/retro_freedom/._.DS_Store
03/A3/src/nz/retro_freedom/dsa2016/.DS_Store
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/._.DS_Store
03/A3/src/nz/retro_freedom/dsa2016/analysis/Flower.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/Flower.java
package
nz
.
retro_freedom
.
dsa2016
.
analysis
;
import
java
.
util
.
*
;
/**
*
@author
*
@version
1.0
*
* A representation of a flower in the Iris data set.
*/
public
class
Flower
{
/**
* The length of the sepals on the represented flower.
*/
public
final
double
sepalLength
;
/**
* The width of the sepals on the represented flower.
*/
public
final
double
sepalWidth
;
/**
* The length of the petals on the represented flower.
*/
public
final
double
petalLength
;
/**
* The width of the petals on the represented flower.
*/
public
final
double
petalWidth
;
/**
* The type of flower being represented.
*/
public
final
FlowerType
type
;
/**
* Makes a Flower from its representation as a row in a CSV file.
*
*
@param
row a CSV file row for a flower (cannot be {
@code
null} or
* empty).
*
@return
a new Flower from the given data.
*
@throws
NullPointerException if row is {
@code
null}.
*
@throws
IllegalArgumentException if row is empty.
*/
public
static
Flower
fromCSVRow
(
String
row
)
{
Objects
.
requireNonNull
(
row
,
"row cannot be null."
);
requireNonEmpty
(
row
,
"row cannot be empty."
);
try
{
String
[]
divided
=
row
.
split
(
","
);
double
sepalLength
=
Double
.
parseDouble
(
divided
[
0
]);
double
sepalWidth
=
Double
.
parseDouble
(
divided
[
1
]);
double
petalLength
=
Double
.
parseDouble
(
divided
[
2
]);
double
petalWidth
=
Double
.
parseDouble
(
divided
[
3
]);
FlowerType
type
=
FlowerType
.
fromString
(
divided
[
4
]);
return
new
Flower
(
sepalLength
,
sepalWidth
,
petalLength
,
petalWidth
,
type
);
}
catch
(
NumberFormatException
e
)
{
throw
new
IllegalArgumentException
(
"Could not parse a number "
+
"from this row: "
+
row
);
}
}
/**
* Compares by all sepal and petal parameters and type.
*
*
@param
o a thing to compare to.
*
@return
true if the sepal and petal parameters, and type, match exactly.
*/
@
Override
public
boolean
equals
(
Object
o
)
{
if
(
o
instanceof
Flower
)
{
Flower
f
=
(
Flower
)
o
;
if
(
sepalLength
==
f
.
sepalLength
&&
sepalWidth
==
f
.
sepalWidth
&&
petalLength
==
f
.
petalLength
&&
petalWidth
==
f
.
petalWidth
)
{
return
true
;
}
}
return
false
;
}
/**
* Hashes on sepal and petal parameters, and type.
*
*
@return
a unique code for the given aspects of the Flower.
*/
@
Override
public
int
hashCode
()
{
int
result
=
1
;
result
=
37
*
result
+
doubleHash
(
sepalLength
);
result
=
37
*
result
+
doubleHash
(
sepalWidth
);
result
=
37
*
result
+
doubleHash
(
petalLength
);
result
=
37
*
result
+
doubleHash
(
petalWidth
);
result
=
37
*
result
+
type
.
hashCode
();
return
result
;
}
/**
* Emits a CSV representation, which can be read back in with
* fromCSVRow.
*
*
@return
a CSV row representing this Flower.
*/
@
Override
public
String
toString
()
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
sepalLength
);
sb
.
append
(
","
);
sb
.
append
(
sepalWidth
);
sb
.
append
(
","
);
sb
.
append
(
petalLength
);
sb
.
append
(
","
);
sb
.
append
(
petalWidth
);
sb
.
append
(
","
);
sb
.
append
(
type
.
toString
());
return
sb
.
toString
();
}
// Private stuff
// private constructor for safety
private
Flower
(
double
sepalLength
,
double
sepalWidth
,
double
petalLength
,
double
petalWidth
,
FlowerType
type
)
{
this
.
sepalLength
=
sepalLength
;
this
.
sepalWidth
=
sepalWidth
;
this
.
petalLength
=
petalLength
;
this
.
petalWidth
=
petalWidth
;
this
.
type
=
type
;
}
// Helper for emptiness checking
private
static
void
requireNonEmpty
(
String
s
,
String
message
)
{
if
(
s
.
length
()
==
0
)
{
throw
new
IllegalArgumentException
(
message
);
}
}
// Helper for hashCode
private
static
int
doubleHash
(
double
d
)
{
long
f
=
Double
.
doubleToLongBits
(
d
);
return
(
int
)(
f
^
(
f
>>>
32
));
}
}
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/analysis/._Flower.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/FlowerType.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/FlowerType.java
package
nz
.
retro_freedom
.
dsa2016
.
analysis
;
import
java
.
util
.
*
;
/**
*
@author
*
@version
1.0
*
* The types of flowers in the Iris data set.
*/
public
enum
FlowerType
{
SETOSA
(
"Iris-setosa"
),
VERSICOLOUR
(
"Iris-versicolor"
),
VIRGINICA
(
"Iris-virginica"
);
/**
* Emits the name of the flower type, as in the sample data set.
*
@return
a representation of this flower type, matching the way it is
* shown in the sample data set.
*/
@
Override
public
String
toString
()
{
return
rep
;
}
/**
* Makes a FlowerType from its representation in the sample data file.
*
@param
s the textual representation of the type (as per sample data).
* Cannot be {
@code
null} or empty.
*
@return
the FlowerType represented by this data.
*
@throws
NullPointerException if the argument is {
@code
null}.
*
@throws
IllegalArgumentException if the argument is empty.
*/
public
static
FlowerType
fromString
(
String
s
)
{
Objects
.
requireNonNull
(
s
,
"Cannot parse a FlowerType from null"
);
if
(
s
.
length
()
==
0
)
{
throw
new
IllegalArgumentException
(
"Cannot parse a FlowerType "
+
"from an empty string"
);
}
switch
(
s
)
{
case
"Iris-setosa"
:
return
SETOSA
;
case
"Iris-versicolor"
:
return
VERSICOLOUR
;
case
"Iris-virginica"
:
return
VIRGINICA
;
default
:
throw
new
IllegalArgumentException
(
"Not a valid FlowerType "
+
s
);
}
}
// Private stuff
private
final
String
rep
;
// private constructor to take rep into account
private
FlowerType
(
String
s
)
{
rep
=
s
;
}
}
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/analysis/._FlowerType.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/Main.java
03/A3/src/nz/retro_freedom/dsa2016/analysis/Main.java
package
nz
.
retro_freedom
.
dsa2016
.
analysis
;
import
java
.
util
.
*
;
import
java
.
io
.
*
;
import
nz
.
retro_freedom
.
dsa2016
.
pipes
.
*
;
/**
*
@author
*
@version
1.0
*
* Entry point to this program.
*/
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
if
(
args
.
length
==
0
)
{
System
.
out
.
printf
(
"ERROR: Must give a path to a data file.\n"
);
}
try
{
LinkedList
<
String
>
data
=
readAll
(
args
[
0
]);
LinkedList
<
Flower
>
flowers
=
Pipes
.
map
(
new
Transformer
<
String
,
Flower
>
()
{
@
Override
public
Flower
transform
(
String
in
)
{
return
Flower
.
fromCSVRow
(
in
);
}
},
data
);
// YOUR CODE BEGINS HERE
// Use this as an example to start from
int
len
=
Pipes
.
reduce
(
0
,
new
Reducer
<
Flower
,
Integer
>
()
{
@
Override
public
Integer
combine
(
Integer
x
,
Flower
y
)
{
return
x
+
1
;
}
},
flowers
);
System
.
out
.
printf
(
"Length of result is: %d\n"
,
len
);
// YOUR CODE ENDS HERE
}
catch
(
FileNotFoundException
e
)
{
System
.
out
.
printf
(
"ERROR: The given file does not exist.\n"
);
}
}
// Private stuff
// disable constructor
private
Main
()
{}
private
static
LinkedList
<
String
>
readAll
(
String
loc
)
throws
FileNotFoundException
{
LinkedList
<
String
>
ll
=
new
LinkedList
<>
();
try
(
Scanner
scanner
=
new
Scanner
(
new
File
(
loc
)))
{
while
(
scanner
.
hasNextLine
())
{
String
s
=
scanner
.
nextLine
();
if
(
s
.
length
()
>
0
)
{
ll
.
add
(
s
);
}
}
}
return
ll
;
}
}
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/analysis/._Main.java
__MACOSX/03/A3/src/nz/retro_freedom/dsa2016/._analysis
__MACOSX/03/A3/src/nz/retro_freedom/._dsa2016
__MACOSX/03/A3/src/nz/._retro_freedom
__MACOSX/03/A3/src/._nz
__MACOSX/03/A3/._src
03/read me.pdf
Data Structures and Algorithms Aims
• Practice using pipeline processing
1 Using pipes (a) You will work on file A3.
(b) Go to The UC Irvine Machine Learning Database page for the Iris data set. Read the page
– you find it in file (A3 > data) – this is the data set you will be processing in this lab.
(c) Incorporate your work on pipes (both the Pair class and the contents of the pipes folder they are in file (A2 > src > nz > retro_freedom > dsa2016 ) into the project. Make sure that the Ant targets still work.
(d) Using the Javadocs for the code you have in analysis, find answers to the following ques- tions. Do not use any methods from LinkedList to do these tasks.
(i) How many flowers of each kind are there in the data set? (ii) How many iris setosa with a sepal width of less than 5.0 are there in the data set? (iii) How many iris versicolour with a petal length of more than 4.2 are there in the data
set? (iv) How many iris virginica with a petal width of between 1.5 and 2.0 inclusive are there
in the data set? (v) What are the minimum and maximum values of each of petal length, petal width, sepal
length and sepal width in the data set? Hint: Consider using the Pair class for this task.
(vi) Which kind of flower in the data set has the smallest petal length? Petal width? Sepal length? Sepal width?
(vii) What is the average petal length for each kind of flower in the data set? Average petal width? Average sepal length? Average sepal length?
(viii) Which kind of flower has the largest petals? Use petal length times petal width to determine petal size.
(ix) Which kind of flower has the largest sepals? Use sepal length times sepal width to determine sepal size.
1