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
// Broken apart to ease understanding. | |
val s1=Seq(1,1,1,2,2,2,3,3,3,3,4,4,4,4,1) | |
val groups=s1.groupBy { identity } | |
val sizes=groups.mapValues { _.size} | |
val maxSize=sizes.values.max | |
val maxUsers=sizes.filter(_._2==maxSize).keys.toSet | |
val pop=s1.filter(maxUsers).zipWithIndex | |
val lastPlaces=pop.groupBy(_._1).mapValues(_.last) |
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
http://aperiodic.net/phil/scala/s-99/p16.scala | |
Mine | |
def dropNth[A](n: Int, ls: List[A])= | |
ls.grouped(n).flatMap(_.take(n-1)).toList | |
--------- | |
val li=List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k) |
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
Alternate solution for | |
http://aperiodic.net/phil/scala/s-99/p19.scala | |
def rotate[A](n: Int, ls: List[A])= | |
if (ls.isEmpty) ls | |
else{ | |
val (first,last)=ls.splitAt((if (n>=0) n else ls.length+n)%ls.length) | |
last:::first | |
} |
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
package example | |
import org.scalajs.dom | |
import scalajs.js.annotation.JSExport | |
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow | |
import dom.ext.Ajax | |
import scalajs.js | |
import js.Dynamic.literal | |
import com.felstar.scalajs.ractive._ | |
@JSExport |
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
object Checkout extends App { | |
val rulesString = """ | |
A,50,3 for 130 | |
B,30,2 for 45 | |
C,20 | |
D,15 | |
""" | |
import CheckoutRuleEngine._ |
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
Exercise | |
Implement a console-based social networking application (similar to Twitter) satisfying the scenarios below. | |
Scenarios | |
Posting: Alice can publish messages to a personal timeline | |
> Alice -> I love the weather today | |
> Bob -> Oh, we lost! | |
> Bob -> at least it's sunny |
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
def pack(s:String):List[String]= | |
if (s.isEmpty()) Nil | |
else{ | |
val (packed, rest) = s span { _ == s.head } | |
packed::pack(rest) | |
} | |
def rle(s:String)= pack(s).map(e=>(e.head,e.length)) | |
def compress(s:String):String= |
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
def kSub(k:Int,nums:Array[Int]):Long={ | |
val count=for { | |
i<-0 to nums.size | |
j<-i to nums.size | |
slice=nums.slice(i, j) | |
sum=slice.sum | |
} yield if (sum!=0 && (sum%k)==0) 1 else 0 | |
count.sum | |
} |
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
def isPangram(strings: Array[String]): String = { | |
val values=for{ | |
string<-strings | |
set=string.toLowerCase().toSet | |
isPangram=('a' to 'z').forall(set.contains) | |
} yield if (isPangram) 1 else 0 | |
values.mkString | |
} | |
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
def closestNumbers(s:String):String={ | |
val sorted=s.split(" ").map(_.toInt).toList.sorted | |
val zip=sorted.zip(sorted.tail) | |
val triples=for { | |
(x,y)<-zip | |
} yield (x,y,Math.abs(x-y)) | |
val (_,_,minDelta)=triples.minBy(_._3) | |