Skip to content

P019 with some Traits to structur the problems #1

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.class
*~
70 changes: 70 additions & 0 deletions src/p019.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
object p019 extends Solutions[SolutionP019]{

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
}
}
}

23 changes: 23 additions & 0 deletions src/p020.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}
26 changes: 26 additions & 0 deletions src/problems.scala
Original file line number Diff line number Diff line change
@@ -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)
}