Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.51 KB | None | 0 0
  1. import scala.annotation.tailrec
  2.  
  3. def take[A](n: Int, xs: List[A]): List[A] = {
  4.   xs match {
  5.     case h :: t if n > 0 => h :: take(n - 1, t)
  6.     case _ => Nil
  7.   }
  8. }
  9.  
  10. assert(take(2, List(1, 2, 3, 5, 6)) == List(1, 2))
  11. assert(take(20, Nil) == Nil)
  12. assert(take(2, List(1, 2)) == List(1, 2))
  13. assert(take(0, List(1, 2, 3, 5, 6)) == Nil)
  14. assert(take(-2, List(1, 2, 3, 5, 6)) == Nil)
  15. assert(take(8, List(1, 2, 3, 5, 6)) == List(1, 2, 3, 5, 6))
  16.  
  17. def drop[A](n: Int, xs: List[A]): List[A] = {
  18.   xs match {
  19.     case _ :: t if n > 0 => drop(n - 1, t)
  20.     case _ => xs
  21.   }
  22. }
  23.  
  24. assert(drop(2, List(1, 2, 3, 5, 6)) == List(3, 5, 6))
  25. assert(drop(0, List(1, 2, 3)) == List(1, 2, 3))
  26. assert(drop(3, List(1, 2, 3)) == Nil)
  27. assert(drop(20, Nil) == Nil)
  28. assert(drop(-2, List(1, 2, 3, 5, 6)) == List(1, 2, 3, 5, 6))
  29. assert(drop(8, List(1, 2, 3, 5, 6)) == Nil)
  30.  
  31. def reverse[A](xs: List[A]): List[A] = {
  32.   def r[A](xs: List[A], acc: List[A]): List[A] = {
  33.     xs match {
  34.       case h :: t => r(t, h :: acc)
  35.       case _ => acc
  36.     }
  37.   }
  38.  
  39.   r(xs, Nil)
  40. }
  41.  
  42. assert(reverse(List("Ala", "ma", "kota")) == List("kota", "ma", "Ala"))
  43. assert(reverse(Nil) == Nil)
  44.  
  45. def initSegment[A](xs: List[A], ys: List[A]): Boolean = {
  46.   (xs, ys) match {
  47.     case (Nil, _) => true
  48.     case (x_h :: x_t, y_h :: y_t) if x_h == y_h => initSegment(x_t, y_t)
  49.     case _ => false
  50.   }
  51. }
  52.  
  53. assert(initSegment(List(1, 2), List(1, 2, 3, 4)))
  54. assert(initSegment(Nil, List(1)))
  55. assert(initSegment(Nil, Nil))
  56. assert(initSegment(List(1, 2), List(1, 2)))
  57. assert(!initSegment(List(1, 2, 3), List(1, 2)))
  58. assert(!initSegment(List(1, 2), List(1, 3, 4, 5)))
  59.  
  60. def replicate(xs: List[Int]): List[Int] = {
  61.   def repNTimes(xs: List[Int], n: Int): List[Int] = {
  62.     (xs,n) match{
  63.       case (h::t,_) if n>0 => h:: repNTimes(xs,n-1)
  64.       case (h1::h2::t,_) => repNTimes(h2::t,h2)
  65.       case _ => Nil
  66.     }
  67.   }
  68.   if(xs==Nil)
  69.     Nil
  70.   else
  71.     repNTimes(xs,xs.head)
  72. }
  73.  
  74. assert(replicate(List(1,0,4,-2,3))==List(1, 4, 4, 4, 4, 3, 3, 3))
  75. assert(replicate(List(0,-1,0,-2))==Nil)
  76. assert(replicate(List(0,-1,0,1,-2))==List(1))
  77. assert(replicate(List(2,1,0,0,-1,-15,2))==List(2,2,1,2,2))
  78.  
  79. def newtonRaphson(a: Double, eps: Double): Double = {
  80.   2.0
  81. }
  82.  
  83. def root3(a:Double) : Double ={
  84.   @tailrec
  85.   def root3(a:Double,x:Double,eps:Double):Double = {
  86.     if (Math.abs(Math.pow(x,3)-a) <= eps * Math.abs(a))
  87.       x
  88.     else
  89.       root3(a,x+(a/(x*x)-x)/3,eps)
  90.   }
  91.   if (a>1)
  92.     root3(a,a/3,10e-15)
  93.   else
  94.     root3(a,a,10e-15)
  95. }
  96.  
  97. assert(root3(-8.0) == -2.0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement