Skip to content

Commit

Permalink
P02 written in Java, Scala, and Haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhargulati committed Nov 29, 2015
1 parent 55a0ac7 commit 836e132
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 20 deletions.
25 changes: 23 additions & 2 deletions haskell/README.md
Expand Up @@ -22,11 +22,32 @@ From the original source:

Lists are recursive type in Haskell.

[Problem 01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/haskell/lists/P01.hs)
### [P01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/haskell/lists/P01.hs)

```haskell
Prelude> last1 ["a","b","c","d"]

"d"
```

We are using `last1` because Haskell already has `last` function.

### [P02 (*) Find the last but one element of a list.](https://github.com/shekhargulati/99-problems/blob/master/haskell/lists/P02.hs)

```haskell
Prelude> secondLast [1, 2, 11, 4, 5, 8, 10, 6]
10
```

When list is empty you should get exception as shown below.

```haskell
Prelude> secondLast []
*** Exception: Can't find secondLast element from a list with less than 2 elements
```

When list has one element

```haskell
Prelude> secondLast [1]
*** Exception: Can't find secondLast element from a list with less than 2 elements
```
12 changes: 12 additions & 0 deletions haskell/lists/P02.hs
@@ -0,0 +1,12 @@
-- Using built-in functions
secondLast :: [a] -> a
secondLast [] = error "Can't find secondLast element from a list with less than 2 elements"
secondLast [x] = error "Can't find secondLast element from a list with less than 2 elements"
secondLast list = last (init list)

-- Using recursion
secondLastRecursion :: [a] -> a
secondLastRecursion [] = error "Can't find secondLast element from a list with less than 2 elements"
secondLastRecursion [x] = error "Can't find secondLast element from a list with less than 2 elements"
secondLastRecursion (x:(y:[])) = x
secondLastRecursion (x:xs) = secondLastRecursion xs
26 changes: 24 additions & 2 deletions java8/README.md
Expand Up @@ -38,7 +38,7 @@ public static <T> List<T> tail(LinkedList<T> elements) {

> Java 8 does not support pattern matching so you have to use if-else in your code.
### [Problem 01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/lists/P01.java)
### [P01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/lists/P01.java)

```java
@Test
Expand All @@ -47,4 +47,26 @@ public void shouldFindLastElementFromAListOfAlphabets() throws Exception {
}
```

All JUnit test cases related to this problem are [here](https://github.com/shekhargulati/99-problems/blob/master/java8/src/test/java/com/shekhargulati/ninetynine_problems/java8/lists/P01Test.java).
### [P02 (*) Find the last but one element of a list.](https://github.com/shekhargulati/99-problems/blob/master/java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/lists/P02.java)

```java
@Test
public void shouldFindSecondLastElementFromAList() throws Exception {
List<Integer> numbers = asList(1, 2, 11, 4, 5, 8, 10, 6);
assertThat(P02.secondLast(numbers), is(equalTo(10)));
}
```

The method should throw `NoSuchElementException` when list is either empty or has one element.

```java
@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenListEmpty() throws Exception {
P02.secondLast(Collections.emptyList());
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenListHasSingleElement() throws Exception {
P02.secondLast(Arrays.asList(1));
}
```
@@ -0,0 +1,13 @@
package com.shekhargulati.ninetynine_problems.java8.lists;

import java.util.LinkedList;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toCollection;

public abstract class LinkedListUtils {

public static <T> LinkedList<T> linkedList(T... elements) {
return Stream.of(elements).collect(toCollection(LinkedList::new));
}
}
@@ -1,6 +1,5 @@
package com.shekhargulati.ninetynine_problems.java8.lists;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -48,17 +47,4 @@ public static <T> T lastRecursive(List<T> elements) {
}


public static <T> LinkedList<T> linkedList(T... elements) {
return Stream.of(elements).collect(toCollection(LinkedList::new));
}

public static <T> List<T> tail(LinkedList<T> elements) {
if (elements == null || elements.isEmpty()) {
throw new NoSuchElementException();
}
if (elements.size() == 1) {
return Collections.emptyList();
}
return elements.subList(1, elements.size());
}
}
@@ -0,0 +1,25 @@
package com.shekhargulati.ninetynine_problems.java8.lists;

import java.util.LinkedList;
import java.util.List;

public class P02 {

public static <T> T secondLast(List<T> list) {
if (list.size() < 2) {
throw new IllegalArgumentException("Can't find secondLast element from a list with less than 2 elements");
}
return list.get(list.size() - 2);
}

public static <T> T secondLastRecursion(LinkedList<T> list) {
if (list.size() < 2) {
throw new IllegalArgumentException("Can't find secondLast element from a list with less than 2 elements");
}
if (list.size() == 2) {
return list.get(0);
}
return secondLastRecursion(new LinkedList<>(list.subList(1, list.size())));

}
}
Expand Up @@ -4,6 +4,7 @@

import java.util.LinkedList;

import static com.shekhargulati.ninetynine_problems.java8.lists.LinkedListUtils.linkedList;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
Expand All @@ -18,7 +19,7 @@ public void shouldFindLastElementFromAListOfAlphabets() throws Exception {

@Test
public void shouldFindLastElementFromALinkedListOfAlphabets() throws Exception {
LinkedList<String> alphabets = P01.linkedList("a", "b", "c", "d");
LinkedList<String> alphabets = linkedList("a", "b", "c", "d");
assertThat(P01.last(alphabets), is(equalTo("d")));
}

Expand Down
@@ -0,0 +1,39 @@
package com.shekhargulati.ninetynine_problems.java8.lists;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static com.shekhargulati.ninetynine_problems.java8.lists.LinkedListUtils.linkedList;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class P02Test {

@Test
public void shouldFindSecondLastElementFromAList() throws Exception {
List<Integer> numbers = asList(1, 2, 11, 4, 5, 8, 10, 6);
assertThat(P02.secondLast(numbers), is(equalTo(10)));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenListEmpty() throws Exception {
P02.secondLast(Collections.emptyList());
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenListHasSingleElement() throws Exception {
P02.secondLast(Arrays.asList(1));
}

@Test
public void shouldFindSecondLastElementFromALinkedList() throws Exception {
LinkedList<Integer> numbers = linkedList(1, 2, 11, 4, 5, 8, 10, 6);
assertThat(P02.secondLastRecursion(numbers), is(equalTo(10)));
}
}
25 changes: 24 additions & 1 deletion scala/README.md
Expand Up @@ -25,7 +25,7 @@ In Scala, default List[T] is an immutable LinkedList with O(1) head access and O

**TODO: Add more later**

### [Problem 01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/scala/src/main/scala/com/shekhargulati/ninetynine_problems/scala/lists/P01.scala)
### [P01 (*) Find the last element of a list](https://github.com/shekhargulati/99-problems/blob/master/scala/src/main/scala/com/shekhargulati/ninetynine_problems/scala/lists/P01.scala)

We are using scalatest library for test cases.

Expand All @@ -35,3 +35,26 @@ it("should find last element") {
last should be("d")
}
```

### [P02 (*) Find the last but one element of a list.](https://github.com/shekhargulati/99-problems/blob/master/scala/src/main/scala/com/shekhargulati/ninetynine_problems/scala/lists/P02.scala)

```scala
it("should give second last element when list has more than two elements") {
val numbers = List(1, 2, 11, 4, 5, 8, 10, 6)
P02.secondLast(numbers) should be(10)
}
```

The method should throw exception when list is empty or has single element.

```scala
it("should throw exception when list is empty") {
val thrown = the[NoSuchElementException] thrownBy P02.secondLast(List())
thrown.getMessage should equal("Can't find secondLast element from a list with less than 2 elements")
}

it("should throw exception when list has one element") {
val thrown = the[NoSuchElementException] thrownBy P02.secondLast(List(1))
thrown.getMessage should equal("Can't find secondLast element from a list with less than 2 elements")
}
```
@@ -0,0 +1,13 @@
package com.shekhargulati.ninetynine_problems.scala.lists

object P02 {

def secondLast[T](list: List[T]): T = list match {
case x :: (_ :: Nil) => x
case x :: xs => secondLast(xs)
case _ => throw new NoSuchElementException("Can't find secondLast element from a list with less than 2 elements")
}

def secondLast1[T](list: List[T]): T = if (list.isEmpty) throw new NoSuchElementException else list.init.last

}
@@ -0,0 +1,31 @@
package com.shekhargulati.ninetynine_problems.scala.lists

import java.util.NoSuchElementException

import org.scalatest.{FunSpec, Matchers}

class P02Test extends FunSpec with Matchers {

describe("SecondLast Element in a List Spec") {
it("should give first element when list has only two elements") {
val numbers = List(10, 6)
P02.secondLast(numbers) should be(10)
}

it("should give second last element when list has more than two elements") {
val numbers = List(1, 2, 11, 4, 5, 8, 10, 6)
P02.secondLast(numbers) should be(10)
}

it("should throw exception when list is empty") {
val thrown = the[NoSuchElementException] thrownBy P02.secondLast(List())
thrown.getMessage should equal("Can't find secondLast element from a list with less than 2 elements")
}

it("should throw exception when list has one element") {
val thrown = the[NoSuchElementException] thrownBy P02.secondLast(List(1))
thrown.getMessage should equal("Can't find secondLast element from a list with less than 2 elements")
}
}

}

0 comments on commit 836e132

Please sign in to comment.