Skip to content

Instantly share code, notes, and snippets.

@notyy
Created April 18, 2012 09:46
Show Gist options
  • Save notyy/2412425 to your computer and use it in GitHub Desktop.
Save notyy/2412425 to your computer and use it in GitHub Desktop.
pig
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 succ1(ch: Char): String = ch match {
case '9' => ("10")
case 'z' => ("aa")
case 'Z' => ("AA")
case x:Char => "0" ++ (x + 1).toChar.toString
}
def add(ch:Char, prevRs:String): String = prevRs.head match {
case '0' => '0'.toString ++ ch.toString + prevRs.tail
case _ => succ1(ch) ++ prevRs.tail
}
def succ(s: String): String = {
s match {
case "" => ""
case _ => {
val tempRs = s.foldRight("1") {(ch,rs) => add(ch,rs)}
if(tempRs.head == '0') tempRs.tail else tempRs
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment