Scala解答例

練習問題

解答例は次の環境でテストしています

ほとんどの解答例はideone.comでそのまま実行できます

つっこみはTopの避難所まで

ループ練習

問1

object Main extends App {
  for (i <- 1 to 5) {
    println("Hello World!")
  }
}

別解

object Main extends App {
  (1 to 5) foreach { _ =>
    println("Hello World!")
  }
}

問2

object Main extends App {
  val j = args(0).toInt

  for (i <- 1 to j) {
    println("Hello World! " + i)
  }
}

別解

object Main extends App {
  val j = args(0).toInt

  (1 to j) foreach { i =>
    println("Hello World! " + i)
  }
}

FizzBuzz

object Main extends App {
  def fizzBuzz(n: Int) =
    if (n % 15 == 0) {
      "FizzBuzz"
    } else if (n % 5 == 0) {
      "Buzz"
    } else if (n % 3 == 0) {
      "Fizz"
    } else {
      n.toString
    }

  (1 to 100).map(fizzBuzz).foreach(println)
}

素数判定

判定

object Main extends App {
  def prime(n: Int) =
    2 <= n && !(2 until (math.sqrt(n) + 1.0).toInt).exists(n % _ == 0)

  val n = args(0).toInt

  println(n.toString + "は" + (if (prime(n)) "素数" else "非素数"))
}

列挙

object Main extends App {
  def primes(xs: Stream[Int] = Stream.from(2)): Stream[Int] =
    xs.head #:: primes(xs.tail.filter(_ % xs.head != 0))

  primes() takeWhile {
    _ <= 100
  } foreach {
    println
  }
}

うるう年測定

import java.util.GregorianCalendar

object Main extends App {
  val cal = new GregorianCalendar()

  while (true) {
    print("西暦を入力してください> ")
    val y = readInt()
    if (y <= 0) sys.exit()

    printf("%dはうるう年%s\n", y,
      if (cal.isLeapYear(y)) "です" else "ではありません")
  }
}

転置行列

import scala.annotation.tailrec

object Main extends App {
  type Mx = List[Array[String]]

  @tailrec
  def matrix(xs: Mx = Nil): Mx = {
    val sep = """\s+""".r
    val s = readLine()
    if (s.length <= 0) xs else matrix(xs :+ sep.split(s))
  }

  matrix().transpose.foreach(x => println(x.mkString(" ")))
}

カレンダー出力

name := "vipcal"
    
version := "1.0"
    
scalaVersion := "2.10.2"

libraryDependencies ++= Seq(
    "org.scala-lang" % "scala-swing" % "2.10.2",
    "org.swinglabs.swingx" % "swingx-core" % "1.6.5-1"
)
import scala.swing._
import org.jdesktop.swingx.{JXDatePicker, JXMonthView}
import java.awt.event.{ActionEvent, ActionListener}

object Main extends SimpleSwingApplication {
  override lazy val top = new MainFrame with ActionListener {
    self =>

    val monthView = new JXMonthView()
    val datePicker = new JXDatePicker(monthView.getToday)

    datePicker.addActionListener(self)

    contents = new BoxPanel(Orientation.Vertical) {
      contents += Component.wrap(datePicker)
      contents += Component.wrap(monthView)
    }

    def actionPerformed(e: ActionEvent) {
      val d = datePicker.getDate
      monthView.setFirstDisplayedDay(d)
      monthView.setFlaggedDates(d)
    }
  }
}
カレンダー

配列いじり

object Main extends App {
  def tailZero(xs: Array[Int]) =
    xs.head +: Array.fill(xs.tail.size){ 0 }

  val a = Array(3, 5, 2, 4, 2)

  println(a.mkString(" "))
  println(tailZero(a).mkString(" "))
}

Caesar暗号解読

object Main extends App {
  val ciphertext =
    """qdq-gi.q-a ziatmxxitmdqibtqi-ustbi ri.qmoqrcxi.qbubu zir
      |-ibtqi-qp-qaai ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi rruoumxakir
      |-ibtqiqzmobyqzbkii-q.qmxi -imyqzpyqzbi rixmeaki -puzmzoqai
      |-i-qscxmbu zaimzpir -i btq-iymbbq-a;iz -iatmxximzgi.q-a
      |zinqiuzimzgiemgipuao-uyuzmbqpimsmuzabir -ia. za -uzsiacotiimi.qbubu zj""".stripMargin.filter(_ != '\n')

  val chars = """abcdefghijklmnopqrstuvwxyz .,-"""

  (for (i <- 1 until chars.length)
    yield chars.zip(chars.splitAt(chars.length - i - 1) match {
      case (a, b) => b + a
    }).toMap
  ) map {
    p => ciphertext.map(c => p.getOrElse(c, c))
  } filter(_.contains("person")) foreach {
    println
  }
}

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS