From cb6ba28dcdc4c4068956bb11147ff15110d2f1c3 Mon Sep 17 00:00:00 2001
From: Gabriel Kastenbaum
Date: Thu, 7 Jul 2011 23:34:03 +0200
Subject: [PATCH 1/2] P019 with some Traits to structur the problems
---
src/.gitignore | 2 ++
src/p019.scala | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 92 insertions(+)
create mode 100644 src/.gitignore
create mode 100644 src/p019.scala
diff --git a/src/.gitignore b/src/.gitignore
new file mode 100644
index 0000000..4ee43af
--- /dev/null
+++ b/src/.gitignore
@@ -0,0 +1,2 @@
+*.class
+*~
diff --git a/src/p019.scala b/src/p019.scala
new file mode 100644
index 0000000..b29ba29
--- /dev/null
+++ b/src/p019.scala
@@ -0,0 +1,90 @@
+import scala.util.matching.Regex
+
+trait Solution {
+ val pattern = new Regex("""(.*)\$(.*)\$""", "problem", "solutionName");
+ def simpleName:String=pattern.findFirstMatchIn(this.getClass.getSimpleName).get.group ("solutionName")
+}
+trait SolutionP019 extends Solution {
+ def rotate[T] (n:Int, l:List[T]):List[T]
+}
+
+trait Solutions[Problem] {
+ def doc():String
+ def tests ()
+ def implementations ():List[Problem]
+}
+
+object p019 extends Solutions[SolutionP019]{
+ def main(args: Array[String]) = {
+ println (doc)
+ tests
+ }
+
+ def doc = """
+ http://aperiodic.net/phil/scala/s-99/p19.scala
+ P19 (**) Rotate a list N places to the left.
+ Examples:
+ scala> rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))
+ res0: List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c)
+
+ scala> rotate(-2, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))
+ res1: List[Symbol] = List('j, 'k, 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i)
+ """
+
+ def tests()={
+ for (sol <- implementations) {
+ print ("Test implementation " + sol.simpleName)
+
+
+ val expected=List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c)
+ val result=sol.rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))
+ require(expected==result)
+ println (" : OK ")
+ }
+ }
+
+ def implementations ()=List(DavidBernard, AlexisAgahi, AriéBénichou )
+
+ object DavidBernard extends SolutionP019 {
+ def rotate[A](k:Int, l:List[A]) = if(l.isEmpty) Nil else {
+ val nk = k % l.length
+ val pk = if(k < 0) l.length + k else k
+ l.drop(pk) ::: l.take(pk)
+ }
+ }
+
+ object BenjaminLerman extends SolutionP019 {
+ def rotate[A](k:Int, l:List[A]) = if(l.isEmpty) Nil else {
+ val nk = k % l.length
+ val pk = if(k < 0) l.length + nk else nk
+ l.drop(pk) ::: l.take(pk)
+ }
+ }
+
+ object AlexisAgahi extends SolutionP019 {
+ def rotate[T]( n:Int, list:List[T]):List[T] = {
+ val size = list.size
+ require( size > 0 )
+ val i = if( n < 0) size + n % size else n % size
+ val (l1, l2 ) = list.splitAt( i )
+ l2 ::: l1
+ }
+ }
+
+ object AriéBénichou extends SolutionP019 {
+
+
+ def rotate[T](n: Int, ls: List[T]): List[T] = {
+ if (ls == null)
+ return Nil
+
+ val length = ls.length
+ if (length == 0)
+ return Nil
+
+ val (l, r) = ls.splitAt(if (n < 0) length + n % length else n % length);
+ r ::: l
+ }
+ }
+}
+
From e13dc6e857bcdc1a9f43f080fd2bdcef580a294f Mon Sep 17 00:00:00 2001
From: Gabriel Kastenbaum
Date: Fri, 8 Jul 2011 00:03:06 +0200
Subject: [PATCH 2/2] Better structure
---
src/p019.scala | 20 --------------------
src/p020.scala | 23 +++++++++++++++++++++++
src/problems.scala | 26 ++++++++++++++++++++++++++
3 files changed, 49 insertions(+), 20 deletions(-)
create mode 100644 src/p020.scala
create mode 100644 src/problems.scala
diff --git a/src/p019.scala b/src/p019.scala
index b29ba29..0ac0c70 100644
--- a/src/p019.scala
+++ b/src/p019.scala
@@ -1,24 +1,4 @@
-import scala.util.matching.Regex
-
-trait Solution {
- val pattern = new Regex("""(.*)\$(.*)\$""", "problem", "solutionName");
- def simpleName:String=pattern.findFirstMatchIn(this.getClass.getSimpleName).get.group ("solutionName")
-}
-trait SolutionP019 extends Solution {
- def rotate[T] (n:Int, l:List[T]):List[T]
-}
-
-trait Solutions[Problem] {
- def doc():String
- def tests ()
- def implementations ():List[Problem]
-}
-
object p019 extends Solutions[SolutionP019]{
- def main(args: Array[String]) = {
- println (doc)
- tests
- }
def doc = """
http://aperiodic.net/phil/scala/s-99/p19.scala
diff --git a/src/p020.scala b/src/p020.scala
new file mode 100644
index 0000000..3f8203a
--- /dev/null
+++ b/src/p020.scala
@@ -0,0 +1,23 @@
+
+object p020 extends Solutions[SolutionP020]{
+ def doc = """
+ scala> removeAt(1, List('a, 'b, 'c, 'd))
+ res0: (List[Symbol], Symbol) = (List('a, 'c, 'd),'b)
+ """
+
+ def tests ()={
+ for (sol <- implementations) {
+ print ("Test implementation " + sol.simpleName)
+ val expected=(List('a, 'c, 'd),'b)
+ val result=sol.removeAt(1, List('a, 'b, 'c, 'd))
+ require(expected==result)
+ println (" : OK ")
+ }
+ }
+
+ def implementations ()=List(BenjaminLerman)
+
+ object BenjaminLerman extends SolutionP020 {
+ def removeAt[A](k:Int, l:List[A]) = (l.take(k):::l.drop(k+1), l(k))
+ }
+}
diff --git a/src/problems.scala b/src/problems.scala
new file mode 100644
index 0000000..e6386ad
--- /dev/null
+++ b/src/problems.scala
@@ -0,0 +1,26 @@
+import scala.util.matching.Regex
+
+trait Solution {
+ val pattern = new Regex("""(.*)\$(.*)\$""", "problem", "solutionName");
+ def simpleName:String=pattern.findFirstMatchIn(this.getClass.getSimpleName).get.group ("solutionName")
+}
+trait Solutions[Problem] {
+ def doc():String
+ def tests ()
+ def implementations ():List[Problem]
+
+ def main(args: Array[String]) = {
+ println (doc)
+ tests
+ }
+}
+
+
+
+trait SolutionP019 extends Solution {
+ def rotate[T] (n:Int, l:List[T]):List[T]
+}
+
+trait SolutionP020 extends Solution {
+ def removeAt[T] (n:Int, l:List[T]):(List[T], T)
+}