Skip to content

Instantly share code, notes, and snippets.

View krrrr38's full-sized avatar
💭
🍣

Ken Kaizu krrrr38

💭
🍣
View GitHub Profile
scala> class FInt(n: Int){
| val factors: List[Int] = {
| def factorsR(n: Int, fs: List[Int]): List[Int] = n match{
| case 1 => fs
| case s => {
| val divn = ps.find(n%_ == 0).get
| factorsR(n/divn, divn +: fs)
| }
| }
| factorsR(n, Nil)
scala> lazy val twiceSquareStream = Stream.from(0).map(n => 2 * math.pow(n,2).toInt)
twiceSquareStream: scala.collection.immutable.Stream[Int] = <lazy>
scala> def isPrimeWithSquare(n: Int) = {
| val tss = twiceSquareStream.takeWhile(_ < n)
| tss.exists(ts => isPrime(n-ts))
| }
isPrimeWithSquare: (n: Int)Boolean
scala> lazy val oddStream: Stream[Int] = Stream.from(3).filter(_%2 != 0)
scala> lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i => ps.takeWhile(j => j*j <= i).forall(i%_ != 0))
ps: Stream[Int] = <lazy>
scala> def isPrime(n: Int) = n match{
| case i if i <= 1 => false
| case i => ps.takeWhile(j => j*j <= i).forall(n%_ != 0)
| }
isPrime: (n: Int)Boolean
scala> def isPrimeTakeLeft(n: Int) = {
type -->[A, B] = PartialFunction[A, B]
// 追記:これPartialFunctionじゃなくて普通にmapで良いやん...
lazy val fizzbuzzIndex: Int --> (Int, String) = (x: Int) => (x % 3, x % 5) match {
case (0, 0) => (x, "fizzbuzz")
case (_, 0) => (x, "buzz")
case (0, _) => (x, "fizz")
case _ => (x, "")
}
scala> type -->[A,B] = PartialFunction[A,B]
scala> val fizzbuzz: Int --> String = ((x: Int) => (x%3, x%5) match {
| case (0, 0) => "fizzbuzz"
| case (_, 0) => "buzz"
| case (0, _) => "fizz"
| })
fizzbuzz: -->[Int,String] = <function1>
scala> (1 to 100).collect(fizzbuzz)
@krrrr38
krrrr38 / numeral.scala
Created February 29, 2012 14:55
Numeral System
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2685
package icpc
import java.util.Scanner
class Numeral(sc: Scanner){
val cnv = Map(1000 -> "m", 100 -> "c", 10 -> "x", 1 -> "i")
def encode(n: Int): String = {
val mcxi = List((1000,n/1000), (100,n%1000/100), (10,n%1000%100/10), (1,n%1000%100%10))
(mcxi filter {_._2 != 0} map {n => n._2 + cnv.get(n._1).get}).mkString.replace("1","")
}
@krrrr38
krrrr38 / persimmon.scala
Created February 29, 2012 04:03
Get Many Persimmon Trees
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2029
package icpc
import java.util.Scanner
class Persimmon(sc: Scanner) {
def run(): Unit = {
def search(n: (Int, Int), trees: Seq[(Int, Int)], s: Int, t: Int): Int =
(for(i <- 0 until s; j <- 0 until t if (trees.contains((n._1+i,n._2+j)))) yield {}).length
while(true) {
@krrrr38
krrrr38 / purse.scala
Created February 28, 2012 21:11
Make Purse Light
// http://www.deqnotes.net/acmicpc/p0006/judge
package icpc
import java.util.Scanner
class Purse(sc: Scanner){
def solve(amount: Int, own: Int): (Int, Int, Int, Int) = {
val diff = own - amount
(diff%500%100%50/10, diff%500%100/50, diff%500/100, diff/500)
}
@krrrr38
krrrr38 / meeting.scala
Created February 28, 2012 20:36
When Can We Meet?
// http://acm.pku.edu.cn/JudgeOnline/problem?id=2028
// 配列のindexなどの使い方がいまいち分からない.
package icpc
import java.util.Scanner
class Meeting(sc: Scanner){
def adjust(n: Int, q: Int): Int = {
val dates = Array.tabulate(101)(n => 0)
for (i <- 1 to n) {
val c = sc.nextInt
@krrrr38
krrrr38 / exploring.scala
Created February 28, 2012 19:53
Exploring Caves
// http://www.deqnotes.net/acmicpc/p0004/judge
package icpc
import java.util.Scanner
import scala.annotation.tailrec
class ExploringCaves(sc: Scanner) {
def findTreasure(routes: List[(Int, Int)]) = routes.sortBy(n => (n._1*n._1 + n._2*n._2)).last
def search(start: (Int, Int)): (Int, Int) = {
@tailrec