Skip to content

Instantly share code, notes, and snippets.

View jackliusr's full-sized avatar

Jack Liu Shurui jackliusr

View GitHub Profile
@jackliusr
jackliusr / Manasa and Stones.scala
Created August 3, 2014 15:38
hackerrank Manasa and Stones implementation in Scala
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().drop(1).toList
val tcs = lines.map(l => l.toInt).grouped(3)
def resolve(steps: Int, a: Int, b: Int) : List[String] = {
val maxs = (0 to steps ).map(n => n * a + (steps -n) * b)
maxs.distinct.sorted.toList.map(n => n.toString)
}
tcs.foreach(c => println( resolve(c(0) -1,c(1),c(2)).mkString(" ") ))
}
@jackliusr
jackliusr / Filling Jars.scala
Last active August 29, 2015 14:04
Hackerrank Filling Jars in Scala. need more performant implementation
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().toList
val jars = lines(0).split("\\s+")(0).toInt
val ops = lines.drop(1).map( l => l.split("\\s+").map(s => s.toLong))
val rslt = ops.foldLeft(0L)( (acc, v) => acc + ( (v(1) - v(0) + 1) * v(2) ))
println(rslt / jars)
}
}
@jackliusr
jackliusr / Find Digits.scala
Last active August 29, 2015 14:04
Hackerrank Find Digits in Scala
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().drop(1).toList
lines.foreach( l =>{
val num = l.toInt
println(l.map(c => c.asDigit).count( d => d != 0 && num % d == 0))
} )
}
}
@jackliusr
jackliusr / Maximizing XOR.scala
Last active August 29, 2015 14:04
Hackerrank Maximizing XOR in Scala , several test cases are terminated due to timeout. Possible the number is overflowed.
object Solution {
def maxXor(l: Int, r: Int): Int = {
var maxV :Int = 0;
for(i <- l to r; j <- i to r)
maxV = maxV.max( i ^ j)
maxV
}
def main(args: Array[String]) {
@jackliusr
jackliusr / Game Of Thrones - I.scala
Last active February 9, 2018 02:11
Hackerrank Game Of Thrones - I, Terminated due to timeout
object Solution {
def main(args: Array[String]) {
val line = io.Source.stdin.getLines().filter(_.length > 0).take(1).toList(0)
def existsPalindrome(str:String):Boolean = {
val freqs = str.groupBy( k => k)
freqs.count( { case (k,v ) => v.length %2 == 1 } ) <= 1
}
println (if(existsPalindrome(line)) "YES" else "NO")
@jackliusr
jackliusr / Halloween party.scala
Last active August 29, 2015 14:04
Hackerrank Halloween party in Scala
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().filter(_.length > 0).drop(1)
val nums = lines.map(_.toLong)
nums.foreach( n => println( (n /2) * ( n - n /2) ))
}
}
@jackliusr
jackliusr / Service Lane.scala
Last active August 29, 2015 14:04
Hackerrank Service Lane in Scala
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().toList
val segs = lines(1).split("\\s+").map(_.toInt)
val testcases = lines.drop(2)
for( tc <- testcases){
val p = tc.split("\\s+").map(_.toInt).toList
println(segs.slice(p(0), p(1) + 1).min)
}
}
@jackliusr
jackliusr / Utopian Tree.scala
Last active August 29, 2015 14:04
Hackerrank Utopian Tree in scala
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().filter(_.length > 0).drop(1)
val nums = lines.map(_.toInt)
nums.foreach( n => println( (0 to n).fold(0)( (acc, n) => if(n %2 == 0) acc +1 else acc *2 )) )
}
}
@jackliusr
jackliusr / The Love-Letter Mystery.scala
Last active August 29, 2015 14:04
hackerank The Love-Letter Mystery in Scala
import scala.collection.immutable.StringOps;
object Solution {
def main(args: Array[String]) {
val lines = io.Source.stdin.getLines().toList
for(line <- lines.drop(1))
{
val l = line;
var (first, tmpSecond) = l.splitAt(l.size /2 )
val second = if(first.length() != tmpSecond.length()) tmpSecond.drop(1).reverse
@jackliusr
jackliusr / hackerrank Gem Stones.scala
Last active August 29, 2015 14:04
Resolve Gem Stones in functional programming way
object Solution {
def intersect(a: Set[Char], b: Set[Char]) = a & b
def main(args: Array[String]) {
val lines =io.Source.stdin.getLines().toList
val cases = lines.drop(1)
val caseSets = cases.map(c => c.distinct).map(c => c.toSet)
val resultSet = caseSets.reduce(intersect)
println(resultSet.size)
}
}