Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sonar test ver pass #11

Open
wants to merge 2 commits into
base: sonar_test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ sonar.language=scala

sonar.scala.scoverage.reportPath=\
target/scala-2.13/scoverage-report

#
60 changes: 27 additions & 33 deletions src/main/scala/com/chatwork/quiz/MyOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sealed trait MyOption[+A] {
* 格納された値を返す。
*
* @return 値
* @throws NoSuchElementException 値が存在しない場合スローする
* @throws 値が存在しない場合 NoSuchElementException をスローする
*/
def get: A

Expand All @@ -29,7 +29,10 @@ sealed trait MyOption[+A] {
* @tparam B 新しい型
* @return 新しい [[MyOption]]
*/
def map[B](f: A => B): MyOption[B] = ???
def map[B](f: A => B): MyOption[B] = this match {
case MyNone => MyNone
case MySome(value) => MySome(f(value))
}

/**
* 値が存在する場合に、値の変換を行う。
Expand All @@ -38,24 +41,21 @@ sealed trait MyOption[+A] {
* @tparam B 新しい型
* @return 新しい [[MyOption]]
*/
def flatMap[B](f: A => MyOption[B]): MyOption[B] = ???
def flatMap[B](f: A => MyOption[B]): MyOption[B] = this match {
case MyNone => MyNone
case MySome(value) => f(value)
}

/**
* 値が存在する場合に、値をフィルタリングする。
*
* @param f フィルターのための述語関数
* @return 新しい [[MyOption]]
*/
def filter(f: A => Boolean): MyOption[A] = ???

/**
* 値が存在する場合に、値をフィルタリングする。
* 本来であれば中間状態を作成しないものだが今回はfilterで実装する
*
* @param f フィルターのための述語関数
* @return 新しい [[MyOption]]
*/
def withFilter(f: A => Boolean): MyOption[A] = filter(f)
def filter(f: A => Boolean): MyOption[A] = this match {
case MyNone => MyNone
case MySome(value) => if (f(value)) this else MyNone
}

/**
* 格納された値を返す。値がない場合は指定された値を返す。
Expand All @@ -64,7 +64,10 @@ sealed trait MyOption[+A] {
* @tparam B 新しい要素型
* @return 値
*/
def getOrElse[B >: A](elseValue: B): B = ???
def getOrElse[B >: A](elseValue: B): B = this match {
case MyNone => elseValue
case MySome(value) => value
}

/**
* 値が存在しない場合に、指定した式を評価し返す。
Expand All @@ -73,7 +76,10 @@ sealed trait MyOption[+A] {
* @tparam B 新しい要素型
* @return elseValueを評価した値
*/
def orElse[B >: A](elseValue: => MyOption[B]): MyOption[B] = ???
def orElse[B >: A](elseValue: => MyOption[B]): MyOption[B] = this match {
case MyNone => elseValue
case MySome(_) => this
}

}

Expand All @@ -82,9 +88,9 @@ sealed trait MyOption[+A] {
*/
case object MyNone extends MyOption[Nothing] {

def get: Nothing = ???
def get: Nothing = throw new NoSuchElementException

def isEmpty: Boolean = ???
def isEmpty: Boolean = true

}

Expand All @@ -96,9 +102,9 @@ case object MyNone extends MyOption[Nothing] {
*/
case class MySome[+A](value: A) extends MyOption[A] {

def get: A = ???
def get: A = value

def isEmpty: Boolean = ???
def isEmpty: Boolean = false

}

Expand All @@ -114,18 +120,6 @@ object MyOption {
* @tparam A 値の型
* @return [[MyOption]]
*/
def apply[A](value: A): MyOption[A] = ???

/**
* for式 練習問題1
* @return [[MyOption]] MySome(6)
*/
def translateToForComprehensions1: MyOption[Int] = ???

/**
* for式 練習問題2
* @return [[MyOption]] MyNone
*/
def translateToForComprehensions2: MyOption[Int] = ???
def apply[A](value: A): MyOption[A] = MySome(value)

}
}
57 changes: 40 additions & 17 deletions src/main/scala/com/chatwork/quiz/collection/MyList.scala
Original file line number Diff line number Diff line change
@@ -1,52 +1,75 @@
package com.chatwork.quiz.collection

import com.chatwork.quiz.MyOption
import com.chatwork.quiz.{ MyNone, MyOption, MySome }

sealed trait MyList[+A] {

// Easy
def length: Int = ???
def length: Int = this.foldRight(0)((_, z) => z + 1)

// Normal
def foldLeft[B](z: B)(f: (B, A) => B): B = ???
def foldLeft[B](z: B)(f: (B, A) => B): B = this match {
case MyNil => z
case MyCons(head, tail) => tail.foldLeft(f(z, head))(f)
}

// 難易度選択制
// Normal: 条件 - 特にありません、気の向くままに実装してください。
// Hard: 条件 - foldLeftを使って実装してください。
def foldRight[B](z: B)(f: (A, B) => B): B = ???
def foldRight[B](z: B)(f: (A, B) => B): B = this match {
case MyNil => z
case MyCons(head, tail) => f(head, tail.foldRight(z)(f))
}

// Normal
// scalastyle:off
def ::[B >: A](b: B): MyList[B] = ???
def ::[B >: A](b: B): MyList[B] = this match {
case MyNil => MyList(b)
case MyCons(_, _) => MyCons(b, this)
}
// scalastyle:on

// Normal
def reverse: MyList[A] = ???
def reverse: MyList[A] = this.foldLeft(MyNil: MyList[A])((z, item) => MyCons(item, z))

// Normal
// scalastyle:off
def ++[B >: A](b: MyList[B]): MyList[B] = ???
def ++[B >: A](b: MyList[B]): MyList[B] = this match {
case MyNil => this
case MyCons(_, _) => this.foldRight(b)((item, z) => MyCons(item, z))
}
// scalastyle:on

// Normal
def map[B](f: A => B): MyList[B] = ???
def map[B](f: A => B): MyList[B] = this.foldRight(MyNil: MyList[B])((item, z) => MyCons(f(item), z))

// Normal
def flatMap[B](f: A => MyList[B]): MyList[B] = ???
def flatMap[B](f: A => MyList[B]): MyList[B] = this.foldRight(MyNil: MyList[B])((item, z) => f(item) ++ z)

// Normal
def filter(f: A => Boolean): MyList[A] = ???
def filter(f: A => Boolean): MyList[A] =
this.foldRight(MyNil: MyList[A])((item, z) => if (f(item)) MyCons(item, z) else z)

// Normal: 条件 - filterと同様の実装でも構いません。
// Hard: 条件 - 中間リストを生成しないように実装してください。
def withFilter(f: A => Boolean): MyList[A] = ???
def withFilter(f: A => Boolean): MyList[A] = this.foldRight(MyNil: MyList[A])((b, z) => if (f(b)) MyCons(b, z) else z)

// Normal
def find(f: A => Boolean): MyOption[A] = ???
def find(f: A => Boolean): MyOption[A] =
this.foldRight(MyNone: MyOption[A])((b, z) => if (f(b)) MySome(b) else z)

// Normal
def startsWith[B >: A](prefix: MyList[B]): Boolean = ???

def startsWith[B >: A](prefix: MyList[B]): Boolean =
if (this.length < prefix.length) false
else
prefix match {
case MyNil => true
case MyCons(pHead, pTail) =>
this match {
case MyNil => false
case MyCons(head, tail) => if (pHead == head) tail.startsWith(pTail) else false
}
}
}

case object MyNil extends MyList[Nothing]
Expand All @@ -56,9 +79,9 @@ case class MyCons[+A](head: A, tail: MyList[A]) extends MyList[A]
object MyList {

// Easy
def empty[A]: MyList[A] = ???
def empty[A]: MyList[A] = MyNil

// Normal
def apply[A](as: A*): MyList[A] = ???
def apply[A](as: A*): MyList[A] = as.foldRight(MyNil: MyList[A])((b, z) => MyCons(b, z))

}
}
42 changes: 27 additions & 15 deletions src/main/scala/com/chatwork/quiz/misc/BTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,21 @@ sealed trait Node {
*/
case class Branch(left: Node, value: Int, right: Node) extends Node {

val size: Int = ???
val size: Int = left.size + right.size + 1

val sum: Int = ???
val sum: Int = left.value + right.value + this.value

val avg: Double = ???
val avg: Double = this.sum / this.size

val max: Int = ???
private val list = List(left.value, right.value, this.value)

val min: Int = ???
val max: Int = list.max

def find(value: Int): Option[Node] = ???
val min: Int = list.min

def find(value: Int): Option[Node] =
if (value == this.value) Some(this)
else left.find(value).orElse(right.find(value)).orElse(None)

}

Expand All @@ -75,17 +79,17 @@ case class Branch(left: Node, value: Int, right: Node) extends Node {
*/
case class Leaf(value: Int) extends Node {

val size: Int = ???
val size: Int = 1

val sum: Int = ???
val sum: Int = value

val avg: Double = ???
val avg: Double = value

val max: Int = ???
val max: Int = value

val min: Int = ???
val min: Int = value

def find(value: Int): Option[Node] = ???
def find(value: Int): Option[Node] = if (value == this.value) Some(this) else None

}

Expand Down Expand Up @@ -121,6 +125,14 @@ object BTree {
* @param values ノードに格納する値の集合
* @return [[BTree]]
*/
def apply(values: List[Int]): BTree = ???

}
def apply(values: List[Int]): BTree = {
values match {
case value :: Nil => BTree(Leaf(value))
case _ => {
val (left, mid :: right) = values.splitAt(values.size / 2)
BTree(Branch(apply(left).node, mid, apply(right).node))
}
}
}

}
10 changes: 8 additions & 2 deletions src/main/scala/com/chatwork/quiz/misc/WordCounter.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.chatwork.quiz.misc

import scala.collection.immutable.HashMap

/**
* ワードをカウントするオブジェクト。
*/
object WordCounter {

val SEPARATOR = " "

/**
* 文字列から単語数をカウントする。
*
* @param words 文字列
* @return 単語がキー、単語数がヴァリューのマップ
*/
def countWords(words: List[String]): Map[String, Int] = ???
def countWords(words: List[String]): Map[String, Int] = {
words.map(elm => elm.split(SEPARATOR)).flatten.groupMapReduce(t => t)(_ => 1)((x, y) => x + y)
}

}
}
Loading