Last active
August 21, 2016 08:09
-
-
Save yyYank/801f5b1638966968fc5716070f0bd558 to your computer and use it in GitHub Desktop.
javajo kotlin exercise https://github.com/ntaro/javajo
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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise01 { | |
/** | |
* 引数を2乗した数を返します。 | |
*/ | |
fun square(n: Int): Int = n * n | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(square(fixture.n), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(0, 0), | |
Fixture(1, 1), | |
Fixture(2, 4), | |
Fixture(10, 100), | |
Fixture(123, 15129), | |
Fixture(-99, 9801) | |
) | |
} | |
data class Fixture(val n: Int, | |
val expected: Int) | |
} |
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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise02 { | |
/** | |
* 引数として与えられた名前に対する挨拶文を返します。 | |
* 例えば`name="たろう"`ならば、`"こんにちは、たろうさん!"`という文字列を返します。 | |
*/ | |
fun greetingMessage(name: String): String = "こんにちは、${name}さん!" | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(greetingMessage(fixture.name), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture("", "こんにちは、さん!"), | |
Fixture("たろう", "こんにちは、たろうさん!"), | |
Fixture("Taro", "こんにちは、Taroさん!") | |
) | |
} | |
data class Fixture(val name: String, | |
val expected: 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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise03 { | |
/** | |
* 引数として与えられた点数(`score`)に対応する結果を返します。 | |
* 点数と結果は下記の関係となります。 | |
* * 80点以上100点以下は「すごい」 | |
* * 60点以上80点未満は「合格」 | |
* * 0点以上60点未満は「失格」 | |
* * それ以外は「何かがおかしい」 | |
* | |
* ヒント: Javaと同じく比較演算子だけでなく && や || が使えます。 | |
* おまけ: return文を1回だけ使って書いてみる。 | |
*/ | |
fun result(score: Int): String = when { | |
score >= 80 && score <= 100 -> "すごい" | |
score >= 60 && score < 80 -> "合格" | |
score >= 0 && score < 60 -> "失格" | |
else -> "何かがおかしい" | |
} | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(result(fixture.score), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(0, "失格"), | |
Fixture(59, "失格"), | |
Fixture(60, "合格"), | |
Fixture(79, "合格"), | |
Fixture(80, "すごい"), | |
Fixture(100, "すごい"), | |
Fixture(101, "何かがおかしい"), | |
Fixture(-1, "何かがおかしい") | |
) | |
} | |
data class Fixture(val score: Int, | |
val expected: 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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise04 { | |
/** | |
* 引数として与えられた整数のリストの要素のうち、 | |
* 奇数のものを全て足しあわせた数を返します。 | |
* 例えば`[1, 2, 3]`というリストが与えられたとき、 | |
* 奇数でない要素は`2`のみなので、結果は`4`となります。 | |
*/ | |
fun sumForOdd(nums: List<Int>): Int = nums.filter { it % 2 != 0}.sum() | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(sumForOdd(fixture.nums), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(listOf(), 0), | |
Fixture(listOf(1), 1), | |
Fixture(listOf(1, 2, 3), 4), | |
Fixture(listOf(2, 4, 8, 16), 0), | |
Fixture(listOf(1, 1, 1, 2), 3) | |
) | |
} | |
data class Fixture(val nums: List<Int>, | |
val expected: Int) | |
} |
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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.Assert.fail | |
import org.junit.Test | |
class Exercise05 { | |
/** | |
* このメソッドの引数や戻り値の型を修正の上、実装してください。 | |
* 引数は2つ持ち、第1引数から第2引数を引いた数を返します。 | |
* 引数の名前や、デフォルト値の設定に関しては、下記のテストコードからヒントを得てください。 | |
* | |
* 実装を終えたら、テストコードのコメントアウトされている行を有効にし、 | |
* fail()を削除(またはコメントアウト)して、テストを実行してください。 | |
* | |
* おまけ: 単一式関数として記述してみよう! | |
*/ | |
fun sub(minuend : Int = 0, subtrahend : Int = 0) = minuend - subtrahend | |
@Test | |
fun 普通に呼び出すやつ() { | |
val got = sub(12, 5) | |
assertThat(got, `is`(7)) | |
} | |
@Test | |
fun 名前で引数を指定するやつ() { | |
val got = sub(subtrahend = 8, minuend = 5) | |
assertThat(got, `is`(-3)) | |
} | |
@Test | |
fun 第2引数のデフォルト値が0であることを期待して省略するやつ() { | |
val got = sub(sub(minuend = 123)) | |
assertThat(got, `is`(123)) | |
} | |
} |
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
import org.hamcrest.Matchers | |
import org.junit.Assert | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise06 { | |
/** | |
* 「Intを引数として取り、その倍の数を返すような関数」を返します。 | |
* | |
* ヒント: この関数はあくまで、関数を返すだけで、倍にするような計算は行いません。 | |
* ヒント: 別途、独自に関数を定義してもかまいません。 | |
*/ | |
fun getDouble(): (Int) -> Int = fun(arg : Int) = arg * 2 | |
@Theory | |
fun test(fixture: Fixture) { | |
val double = getDouble() | |
val got = double(fixture.num) | |
Assert.assertThat(got, Matchers.`is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(0, 0), | |
Fixture(1, 2), | |
Fixture(-2, -4), | |
Fixture(123, 246) | |
) | |
} | |
data class Fixture(val num: Int, | |
val expected: Int) | |
} |
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
import org.hamcrest.Matchers | |
import org.junit.Assert | |
import org.junit.Test | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
/** | |
* 課題: | |
* filterEvenとfilterNotNegativeのコードの重複を、 | |
* 高階関数や関数オブジェクトを使用して、なくしてください。 | |
*/ | |
@RunWith(Theories::class) | |
class Exercise07 { | |
/** | |
* 与えられたリストの要素のうち、偶数のみからなる新しいリストを返します。 | |
*/ | |
fun filterEven(ns: List<Int>, filter : (n : Int) -> Boolean, filteredList : (list : List<Int>, filter : (n : Int) -> Boolean) -> List<Int>): List<Int> { | |
return filteredList(ns,filter) | |
} | |
/** | |
* 与えられたリストの要素のうち、非負の数のみからなる新しいリストを返します。 | |
*/ | |
fun filterNotNegative(ns: List<Int>, filter : (n : Int) -> Boolean): List<Int> { | |
val list = mutableListOf<Int>() | |
for (n in ns) { | |
if (filter(n)) { | |
list += n | |
} | |
} | |
return list.toList() | |
} | |
@Test | |
fun 未挑戦であることを知らせるマーク_削除すること() { | |
} | |
@Theory | |
fun test(fixture: FixtureForFilterEven) { | |
val got = filterEven(fixture.ns, {it % 2 == 0}, {ns, f -> | |
val list = mutableListOf<Int>() | |
for (n in ns) { | |
if (f(n)) { | |
list += n | |
} | |
} | |
list.toList()} | |
) | |
Assert.assertThat(got, Matchers.`is`(fixture.expected)) | |
} | |
@Theory | |
fun test(fixture: FixtureForFilterNotNegative) { | |
val got = filterNotNegative(fixture.ns, {it >= 0}) | |
Assert.assertThat(got, Matchers.`is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES_FOR_FILTER_EVEN = arrayOf( | |
FixtureForFilterEven(listOf(), listOf()), | |
FixtureForFilterEven(listOf(1, 2, 3), listOf(2)), | |
FixtureForFilterEven(listOf(2, 4, 3, 9, 0), listOf(2, 4, 0)) | |
) | |
@DataPoints | |
@JvmField | |
val FIXTURES_FOR_FILTER_NOT_NEGATIVE = arrayOf( | |
FixtureForFilterNotNegative(listOf(), listOf()), | |
FixtureForFilterNotNegative(listOf(0, 1, 2), listOf(0, 1, 2)), | |
FixtureForFilterNotNegative(listOf(0, 5, -5, -2, 0), listOf(0, 5, 0)) | |
) | |
} | |
data class FixtureForFilterEven(val ns: List<Int>, | |
val expected: List<Int>) | |
data class FixtureForFilterNotNegative(val ns: List<Int>, | |
val expected: List<Int>) | |
} |
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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise08 { | |
/** | |
* 引数aとbを足した結果を返します。 | |
* ただし、nullを少なくとも1つ含む場合は、結果もnullとなること。 | |
*/ | |
fun add(a: Int?, b: Int?): Int? = if(a != null && b != null) { | |
a + b | |
} else { | |
null | |
} | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(add(fixture.a, fixture.b), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(0, 0, 0), | |
Fixture(3, 6, 9), | |
Fixture(12, -5, 7), | |
Fixture(4, null, null), | |
Fixture(null, 5, null), | |
Fixture(null, null, null) | |
) | |
} | |
data class Fixture(val a: Int?, | |
val b: Int?, | |
val expected: Int?) | |
} |
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
import org.hamcrest.Matchers.`is` | |
import org.junit.Assert.assertThat | |
import org.junit.experimental.theories.DataPoints | |
import org.junit.experimental.theories.Theories | |
import org.junit.experimental.theories.Theory | |
import org.junit.runner.RunWith | |
@RunWith(Theories::class) | |
class Exercise09 { | |
/** | |
* 引数の文字列のリストを、下記のようなリストに変換してください。 | |
* * すべて大文字にする。ただし、nullのものは「unknown」という文字列にすること。 | |
* * 文字数が3文字以下のものは捨てる。 | |
* | |
* 入力例: ["hi", "hello", null] | |
* 出力例: ["HELLO", "unknown"] | |
* | |
* おまけ: エルビス演算子(?:)を使ってみよう。 | |
* 「A ?: B」は「if (A != null) A else B」と同じです。 | |
*/ | |
fun filterAndMap(list: List<String?>): List<String> = list.filter{it == null || it.length > 3}.map{ if(it != null){ it.toUpperCase()} else { "unknown"} } | |
@Theory | |
fun test(fixture: Fixture) { | |
assertThat(filterAndMap(fixture.list), `is`(fixture.expected)) | |
} | |
companion object { | |
@DataPoints | |
@JvmField | |
val FIXTURES = arrayOf( | |
Fixture(listOf(), listOf()), | |
Fixture(listOf("hi", "hello", null), listOf("HELLO", "unknown")), | |
Fixture(listOf(null, "taro", null, "wa", "wa!"), listOf("unknown", "TARO", "unknown")), | |
Fixture(listOf("unknown", null, "", "null"), listOf("UNKNOWN", "unknown", "NULL")) | |
) | |
} | |
data class Fixture(val list: List<String?>, | |
val expected: List<String>) | |
} |
backpaper0
commented
Aug 21, 2016
エルビス関数についてうらがみさんのヒントをいただいてからですが、こうなりました!
mapを2回やっちゃったのと、it
をここでも使えるなんて気付かなかったです〜
fun filterAndMap(list: List<String?>): List<String> = list.map { l -> l?.toUpperCase() }.map{ l -> l ?: "unknown" }.filter{ l -> 3 < l.length }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment