Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
P02 written in Java, Scala, and Haskell
- Loading branch information
1 parent
55a0ac7
commit 836e132
Showing
11 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/lists/LinkedListUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
java8/src/main/java/com/shekhargulati/ninetynine_problems/java8/lists/P02.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()))); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
java8/src/test/java/com/shekhargulati/ninetynine_problems/java8/lists/P02Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
scala/src/main/scala/com/shekhargulati/ninetynine_problems/scala/lists/P02.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
scala/src/test/scala/com/shekhargulati/ninetynine_problems/scala/lists/P02Test.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} | ||
|
||
} |