Created
September 6, 2010 10:22
-
-
Save sdb/566880 to your computer and use it in GitHub Desktop.
Scala solutions for the Coding Kata programming exercises
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
/** | |
* Revert a word in 'backward talk' to understand it | |
* | |
* @param word the backwards word | |
* @return the regular word | |
*/ | |
def revert(word: String) = word reverse | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
def gcd(dividend: Int, divisor: Int): Int = (dividend % divisor) match { | |
case 0 => divisor | |
case x => gcd(divisor, x) | |
} | |
/** | |
* Calculate the 'greatest common divisor'. | |
* | |
* @param number1 the first number ('numerator') | |
* @param number2 the second number ('denominator') | |
* | |
* @return GCD of number1 and number2 | |
*/ | |
def calcGCD(n1: Int, n2: Int) = if (n1 == 0) n2 else gcd(n2, n1) | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
/** | |
* Calculate an element of the Fibonacci sequence | |
* | |
* @param n element number | |
* @return n-th element of the Fibonacci sequence | |
*/ | |
def fibonacci(n: Int): Int = n match { | |
case x if x <= 1 => x | |
case x => fibonacci(n-1) + fibonacci(n-2) | |
} | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
/** | |
* Give an answer to the current game | |
* | |
* @param number current number in the game sequence | |
* @return appropriate answer to the current number | |
*/ | |
def answer(number: Int) = { | |
var answer = "" | |
if (number % 3 == 0) answer += "fizz" | |
if (number % 5 == 0) answer += "buzz" | |
if (answer == "") number.toString else answer | |
} | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
/** | |
* Issue the NASA countdown | |
* | |
* @param start number to start with | |
* @return array with numbers from 'start' to 0 | |
*/ | |
def countdown(start: Int) = (for (i <- (start to 0 by -1)) yield new Integer(i)).toArray | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
/** | |
* Welcome a guest | |
* | |
* @param lastName the last name of the guest | |
* @param isWoman true if the guest is female | |
* @param isSir true if the guest was knighted by the queen | |
* | |
* @return issues welcome text to the guest | |
*/ | |
def welcome(lastName: String, isWoman: Boolean, isSir: Boolean) = | |
"Hello %s %s".format(if (isWoman) if (isSir) "Dame" else "Ms." else if (isSir) "Sir" else "Mr.", lastName) | |
} |
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
package org.codingkata.unit; | |
import _root_.org.codingkata.unit.api.BaseKataSolution; | |
class MyKata extends BaseKataSolution { | |
lazy val Slang = """\$([\w ]*)\$""".r | |
/** | |
* Translate a slang text to a TV news-compatible text | |
* | |
* @param text string full of slang (e.g. 'I am $kewl$.') | |
* @param dictionary slang dictionary - in pairs, like {{slang, non-slang}, ...} | |
* | |
* @return the 'cleaned' text | |
*/ | |
def translate(text: String, slang: Array[Array[String]]) = { | |
lazy val dict = List.fromArray(slang).foldLeft(Map.empty[String, String])((d, s) => d + (s(0) -> s(1))) | |
Slang.replaceAllIn(text, m => dict(m.group(1))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment