Skip to content

Instantly share code, notes, and snippets.

@sis-yoshiday
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sis-yoshiday/335cff874a12db54de09 to your computer and use it in GitHub Desktop.
Save sis-yoshiday/335cff874a12db54de09 to your computer and use it in GitHub Desktop.
Scalaハンズオン Day1
// https://github.com/bati11/learn-scala/tree/master/getting_started/day1
// おさらい
// List(1,2,3)とList(10,100,1000)の各要素を掛け合わせた結果からなるコレクションをつくるfor式を書いてください
for (x <- 1 to 3; y <- 1 to 3) yield x * Math.pow(10, y.toInt)
//掛け合わせた結果が50以上になるコレクションをつくるfor式を書いてください。
(for (x <- 1 to 3; y <- 1 to 3) yield x * Math.pow(10, y.toInt).toInt).filter(x => x >= 50)
//100までの偶数の中で、7で割った余りが3になる数値からなるコレクションをつくってください。
(1 to 100 by 2).toList.filter(x => x % 7 == 3).foreach(x => println(x))
//List("Apple", "Banana", "Pineapple", "Lemon") から、各要素の頭文字からなるコレクションをつくってください。
List("Apple", "Banana", "Pineapple", "Lemon").map(s => s.charAt(0)).foreach(s => println(s))
//FizzBuzzを書いてください。
(1 to 100)
.map(x => if (x.toInt % 15 == 0) "Fizz Buzz" else if (x.toInt % 3 == 0) "Fizz" else if (x.toInt % 5 == 0) "Buzz" else String.valueOf(x.toInt))
.foreach(s => println(s))
//要素数の異なるリストをzipでつなげるとどうなりますか?
res52: List[(Int, String)] = List((1,a), (2,b)) // 一番短い要素数のリストになった
//次のすべての条件を満たす直角三角形を見つけるメソッドを書いてください。直角三角形はタプルで表現してください。
// 3辺の長さはすべて整数である
// 各辺それぞれの長さはメソッドの引数で渡された数値以下である
// 周囲の長さは24に等しい
(for (l1 <- 1 to 10; l2 <- 1 to 10; l3 <- 1 to 10) yield (l1.toInt, l2.toInt, l3.toInt))
.filter(t => t._1 + t._2 + t._3 == 24)
.filter(t => t._1 * t._1 + t._2 * t._2 == t._3 * t._3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment