Created
November 2, 2011 21:03
-
-
Save carlisgg/1334914 to your computer and use it in GitHub Desktop.
7L7W - Scala
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
class TicTacToeBoard() { | |
val winningPositions = List((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)) | |
var XWins = false | |
var OWins = false | |
val board = args(0) | |
if (board == null || board.size != 9) { | |
throw new IllegalArgumentException("The board passed must be a String with 9 positions") | |
} | |
def detectWinner() { | |
winningPositions.foreach {combination => | |
XWins = isXWinner(combination) || XWins | |
OWins = isOWinner(combination) || OWins | |
} | |
if (XWins && !OWins) { | |
println("X is the winner") | |
} else if (OWins && !XWins) { | |
println("O is the winner") | |
} else if (XWins && OWins) { | |
println("there is a tie") | |
} else { | |
println("nobody wins") | |
} | |
} | |
def isXWinner(combination: (Int, Int, Int)): Boolean = { | |
board.apply(combination._1) == 'X' && board.apply(combination._2) == 'X' && board.apply(combination._3) == 'X' | |
} | |
def isOWinner(combination: (Int, Int, Int)): Boolean = { | |
board.apply(combination._1) == 'O' && board.apply(combination._2) == 'O' && board.apply(combination._3) == 'O' | |
} | |
} | |
val ticTacToe = new TicTacToeBoard() | |
ticTacToe.detectWinner |
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
// EXERCISE 1 | |
def computeSize(list: List[String]): Int = { | |
list.foldLeft(0)((size, item) => size + item.size) | |
} | |
val list = List("uno", "dos", "tres") | |
val totalSize = computeSize(list) | |
println(totalSize) | |
//////////////// | |
// EXERCISE 2 | |
import scala.collection.immutable.WrappedString | |
trait Prettify extends WrappedString { | |
val curseWords = Map("shoot" -> "Pucky", "darn" -> "Beans") | |
def prettyContent(): String = { | |
var words = this.self.split(" ") | |
var replacement = "" | |
words.foreach(word => { | |
if (curseWords.contains(word.toLowerCase)) { | |
replacement += curseWords(word) | |
} else { | |
replacement += word | |
} | |
replacement += " " | |
}) | |
replacement | |
} | |
} | |
class PrettyString(override val self: String) extends WrappedString(self) with Prettify { | |
} | |
val content = new PrettyString("this exercise is darn useless, shoot it up") | |
println(content.prettyContent) | |
////////////////////// | |
// EXERCISE 3 | |
import scala.collection.immutable.WrappedString | |
import scala.io.Source | |
import scala.collection.immutable.HashMap | |
trait Prettify extends WrappedString { | |
var curseWords = new HashMap[String, String] | |
val s = Source.fromFile("/desarrollo/7l7w/scala/words.txt") | |
s.getLines.foreach((line) => { | |
val substitution = line.split(",") | |
curseWords += substitution(0).trim() -> substitution(1).trim() | |
}) | |
println(curseWords) | |
def prettyContent(): String = { | |
var words = this.self.split(" ") | |
var replacement = "" | |
words.foreach(word => { | |
if (curseWords.contains(word.toLowerCase)) { | |
replacement += curseWords(word) | |
} else { | |
replacement += word | |
} | |
replacement += " " | |
}) | |
replacement | |
} | |
} | |
class PrettyString(override val self: String) extends WrappedString(self) with Prettify { | |
} | |
val content = new PrettyString("this exercise is darn useless, shoot it up") | |
println(content.prettyContent) |
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 scala.io._ | |
import scala.actors._ | |
import Actor._ | |
// START:loader | |
object PageLoader { | |
def getPageSize(url : String) = Source.fromURL(url).mkString.length | |
def countPageLinks(url : String) : Int = { | |
val contents = Source.fromURL(url).mkString | |
"""<a(.*?)</a>""".r.findAllIn(contents).size | |
} | |
} | |
// END:loader | |
val urls = List("http://www.amazon.com/", | |
"http://www.twitter.com/", | |
"http://www.google.com/", | |
"http://www.cnn.com/" ) | |
// START:time | |
def timeMethod(method: () => Unit) = { | |
val start = System.nanoTime | |
method() | |
val end = System.nanoTime | |
println("Method took " + (end - start)/1000000000.0 + " seconds.") | |
} | |
// END:time | |
// START:sequential | |
def getPageSizeSequentially() = { | |
for(url <- urls) { | |
println("Size for " + url + ": " + PageLoader.getPageSize(url)) | |
} | |
} | |
def getPageLinksSequentially() = { | |
for(url <- urls) { | |
println("Links for " + url + ": " + PageLoader.countPageLinks(url)) | |
} | |
} | |
// END:sequential | |
// START:concurrent | |
def getPageSizeConcurrently() = { | |
val caller = self | |
for(url <- urls) { | |
actor { caller ! (url, PageLoader.getPageSize(url)) } | |
} | |
for(i <- 1 to urls.size) { | |
receive { | |
case (url, size) => | |
println("Size for " + url + ": " + size) | |
} | |
} | |
} | |
// END:concurrent | |
// START:concurrent | |
def getPageLinksConcurrently() = { | |
val caller = self | |
for(url <- urls) { | |
actor { caller ! (url, PageLoader.countPageLinks(url)) } | |
} | |
for(i <- 1 to urls.size) { | |
receive { | |
case (url, size) => | |
println("Links for " + url + ": " + size) | |
} | |
} | |
} | |
// END:concurrent | |
// START:script | |
println("Sequential run:") | |
timeMethod { getPageSizeSequentially } | |
timeMethod { getPageLinksSequentially } | |
println("Concurrent run") | |
timeMethod { getPageSizeConcurrently } | |
timeMethod { getPageLinksConcurrently } | |
// END:script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment