Skip to content

Instantly share code, notes, and snippets.

@kevin4936
Created April 18, 2012 13:39
Show Gist options
  • Save kevin4936/2413615 to your computer and use it in GitHub Desktop.
Save kevin4936/2413615 to your computer and use it in GitHub Desktop.
Test Succ
object Succ {
def main(args: Array[String]) {
assert(succ("") == "")
assert(succ("3") == "4")
assert(succ("R2D3") == "R2D4")
assert(succ("R293") == "R294")
assert(succ("R2D9") == "R2E0")
assert(succ("A99") == "B00")
assert(succ("Z99") == "AA00")
assert(succ("Zz99") == "AAa00")
assert(succ("9Z") == "10A")
println("finished")
}
def apply(s : String, index : Int, firstCh : String) : String = {
if(index <0) firstCh + s else {
s.apply(index) match {
case 'z' => apply(s.updated(index, 'a'), index -1, "a");
case 'Z' => apply(s.updated(index, 'A'), index -1, "A");
case '9' => apply(s.updated(index, '0'), index -1, "1");
case _ => s.updated(index, (s.apply(index) + 1).toChar);
}
}
}
def succ(s:String): String = if(s.isEmpty) "" else apply(s, s.length()-1, "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment