Skip to content

binary-search: use generics #868

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

Merged
merged 3 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -639,18 +639,6 @@
"unlocked_by": "word-count",
"uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6"
},
{
"core": true,
"difficulty": 6,
"slug": "linked-list",
"topics": [
"algorithms",
"lists",
"generics"
],
"unlocked_by": null,
"uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
},
{
"core": false,
"difficulty": 6,
Expand Down Expand Up @@ -749,6 +737,18 @@
"unlocked_by": "scrabble-score",
"uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361"
},
{
"core": true,
"difficulty": 6,
"slug": "linked-list",
"topics": [
"algorithms",
"lists",
"generics"
],
"unlocked_by": null,
"uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
},
{
"core": false,
"difficulty": 6,
Expand Down
25 changes: 25 additions & 0 deletions exercises/binary-search/.meta/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This exercise introduces [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html).
To make the tests pass you need to construct your class such that it accepts any type of input, e.g. `Integer` or `String`.

Generics are useful because they allow you to write more general and reusable code.
The Java [List](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) and [Map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) implementations are both examples of classes that use generics.
By using them you can construct a `List` containing `Integers` or a list containing `Strings` or any other type.

There are a few constraints on the types used in generics.
One of them is that once you've constructed a `List` containing `Integers`, you can't put `Strings` into it.
You have to specify which type you want to put into the class when you construct it, and that instance can then only be used with that type.

For example you could construct a list of `Integers`:

`List<Integer> someList = new LinkedList<>();`

Now `someList` can only contain `Integers`. You could also do:

`List<String> someOtherList = new LinkedList<>()`

Now `someOtherList` can only contain `Strings`.

Another constraint is that any type used with generics cannot be a [primitive type](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), such as `int` or `long`.
However, every primitive type has a corresponding reference type, so instead of `int` you can use [`Integer`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) and instead of `long` you can use [`Long`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html).

It can help to look at an [example use case of generics](https://docs.oracle.com/javase/tutorial/java/generics/types.html) to get you started.
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@

import java.util.List;

public class BinarySearch<T extends Comparable<T>> {
class BinarySearch<T extends Comparable<T>> {

private List<T> array;
private int arraySize;

public BinarySearch(List<T> array) {
BinarySearch(List<T> array) {
this.array = array;
this.arraySize = array.size();
}

public int indexOf(T value) {
int indexOf(T value) {
return search(value);
}

public List<T> getArray() {
return array;
}

private int search(T value) {
int left = 0;
int right = this.arraySize - 1;
Expand Down
22 changes: 11 additions & 11 deletions exercises/binary-search/src/test/java/BinarySearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ public class BinarySearchTest {

@Test
public void findsAValueInAnArrayWithOneElement() {
List<Integer> listOfUnitLength = Collections.singletonList(6);
List<Character> listOfUnitLength = Collections.singletonList('6');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question as for custom-set; is there a pattern for the generic types being used now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I've tried to use Integer, Character and String and to make sure I've got a couple of tests using each, but I've not got a strict pattern as I didn't think it mattered too much. I could make it a more strict pattern if you think that would be useful?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I think it's good as-is!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change it to cycle through Character, String, Integer as it's probably best to be consistent :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was changing it I remembered why I didn't do that. Some of them can't use Character because they use higher numbers, e.g. 11... So it's only possible to use Character for some of the earlier tests, at least for this exercise. I could make the rest cycle through String and Integer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I was changing it I remembered why I didn't do that. Some of them can't use Character because they use higher numbers, e.g. 11... So it's only possible to use Character for some of the earlier tests, at least for this exercise. I could make the rest cycle through String and Integer?

This sounds like a good compromise to me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will do that :) Will do that for the others too, try to cycle through all three but skip Character if not possible :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly, as it turns out String causes problems as well since it will sort it lexicographically and we're using numbers in all our test cases... So should I instead try to cycle through all three, skip Chracter if not possible and also skip String if not possible? And make sure that Character and String are used at least once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds ideal, yep!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, will do that then :)


BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength);
BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength);

assertEquals(0, search.indexOf(6));
assertEquals(0, search.indexOf('6'));
}

@Ignore("Remove to run test")
@Test
public void findsAValueInTheMiddleOfAnArray() {
List<Integer> sortedList = Collections.unmodifiableList(
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
List<String> sortedList = Collections.unmodifiableList(
Arrays.asList("1", "3", "4", "6", "8", "9", "11")
);

BinarySearch<Integer> search = new BinarySearch<>(sortedList);
BinarySearch<String> search = new BinarySearch<>(sortedList);

assertEquals(3, search.indexOf(6));
assertEquals(3, search.indexOf("6"));
}

@Ignore("Remove to run test")
Expand Down Expand Up @@ -83,13 +83,13 @@ public void findsAValueInAnArrayOfEvenLength() {
@Ignore("Remove to run test")
@Test
public void identifiesThatAValueIsNotIncludedInTheArray() {
List<Integer> sortedList = Collections.unmodifiableList(
Arrays.asList(1, 3, 4, 6, 8, 9, 11)
List<String> sortedList = Collections.unmodifiableList(
Arrays.asList("1", "3", "4", "6", "8", "9", "11")
);

BinarySearch<Integer> search = new BinarySearch<>(sortedList);
BinarySearch<String> search = new BinarySearch<>(sortedList);

assertEquals(-1, search.indexOf(7));
assertEquals(-1, search.indexOf("7"));
}

@Ignore("Remove to run test")
Expand Down