Last active
August 29, 2015 13:58
-
-
Save berngp/10199642 to your computer and use it in GitHub Desktop.
Scala Scripts Exercises.
This file contains hidden or 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
val width = 5 | |
//width: Int = 5 | |
val tTop = ( 0 until width/2 ) map ( n => 2 * n + 1 ) | |
//tTop: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3) | |
val tBottom = tTop.reverse | |
//tBottom: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 1) | |
val diamond = tTop union Seq(width) union tBottom | |
//diamond: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 3, 1) | |
val offset = 3 | |
//offset: Int = 3 | |
val d = diamond map ( n => ( offset + (width - n)/2, n) ) | |
//d: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((5,1), | |
//(4,3), (3,5), (4,3), (5,1)) | |
def printDiamond( s:Seq[(Int,Int)] ) = { | |
s.foreach{ t => | |
( 0 until t._1 ).foreach( _ => print(".") ) | |
( 0 until t._2 ).foreach( _ => print("*") ) | |
print("\n") | |
} | |
} | |
//printDiamond: (s: Seq[(Int, Int)])Unit | |
printDiamond(d) | |
//.....* | |
//....*** | |
//...***** | |
//....*** | |
//.....* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment