Skip to content

Commit

Permalink
some little fixes. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanM authored and shekhargulati committed Nov 22, 2016
1 parent 336c42f commit 047d11d
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.shekhargulati.ninetynine_problems._01_lists

import scala.annotation.tailrec

/**
* Find the number of elements of a list
*/
Expand All @@ -10,6 +12,7 @@ object P04 {
def length1[T](list: List[T]): Int = list.map(_ => 1).sum

def lengthRecursive[T](list: List[T]): Int = {
@tailrec
def rec(list: List[T], length: Int): Int = list match {
case x :: xs => rec(xs, length + 1)
case Nil => length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import scala.annotation.tailrec
* <p>
* <pre>
* compress(List(a,a,a,a,b,c,c,a,a,d,e,e,e,e))
* [a,b,c,d,e]
* [a,b,c,a,d,e]
* </pre>
*/
object P08 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object P16 {
if (n == 0) {
list
} else {
list.zipWithIndex.withFilter(t => if ((t._2 + 1) % n == 0) false else true).map(_._1)
list.zipWithIndex.withFilter(t => (t._2 + 1) % n != 0).map(_._1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object P28 {
The objective is to sort the elements of InList according to their length.
E.g. short lists first, longer lists later, or vice versa
*/
def lsort[T](list: List[List[T]]): List[List[T]] = list.sortWith((l1, l2) => l1.length - l2.length < 0)
def lsort[T](list: List[List[T]]): List[List[T]] = list.sortBy(_.length)

/*
Again, we suppose that a list (InList) contains elements that are lists themselves.
Expand All @@ -19,8 +19,8 @@ object P28 {
*/
def lfsort[T](list: List[List[T]]): List[List[T]] =
lsort(list)
.groupBy(l => l.length).values.toList
.sortWith((l1, l2) => l1.length - l2.length < 0)
.groupBy(_.length).values.toList
.sortBy(_.length)
.flatten

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object P31 {
if (number < 2)
false
else
!((2 to Math.sqrt(number).toInt) exists (f => number % f == 0))
!((2 to Math.sqrt(number).toInt) exists (number % _ == 0))


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import com.shekhargulati.ninetynine_problems._01_lists.P10
*/
object P33 {

def primeFactorsMult(number: Int): List[(Int, Int)] = P10.encode(P32.primeFactors(number)).map { case (t1, t2) => (t2, t1) }
def primeFactorsMult(number: Int): List[(Int, Int)] = P10.encode(P32.primeFactors(number)).map(_.swap)

}

0 comments on commit 047d11d

Please sign in to comment.