sidproquo
BAN USER//Scala code
import scala.collection.mutable._
object BracketCombinations{
def bracketCombinations(n: Int) = {
def bracketCombinationsInternal(currN: Int,
balanceStack: Stack[Char] = new Stack[Char],
curr: String = "",
acc: List[String] = Nil): List[String] =
currN match {
case 0 if balanceStack.isEmpty => acc:+curr
case 0 if balanceStack.nonEmpty=>
balanceStack.pop
bracketCombinationsInternal(currN, balanceStack, curr+")", acc)
case _ if balanceStack.isEmpty=>
balanceStack.push('(')
bracketCombinationsInternal(currN-1, balanceStack, curr+"(", acc)
case _ if balanceStack.nonEmpty=>
val newStack = new Stack[Char]
balanceStack.foreach(newStack.push(_))
newStack.push('(')
balanceStack.pop
bracketCombinationsInternal(currN-1, newStack, curr+"(", acc) ++
bracketCombinationsInternal(currN, balanceStack, curr+")", acc)
}
bracketCombinationsInternal(n)
}
}
//Language: Scala
object Add2HugeNumbers{
val NoCarry = 0
val FixedLength = 4
def fourDigitPerTerm(ls: List[Int]): Boolean = ls match {
case Nil => true
case h::t => if (h.toString.length == FixedLength) fourDigitPerTerm(t) else false
}
def addNumbers(ls1: List[Int], ls2: List[Int]) = {
require(fourDigitPerTerm(ls1++ls2))
val ls1Reversed = ls1.reverse
val ls2Reversed = ls2.reverse
def internalAddNumbers(ls1: List[Int],
ls2: List[Int],
carry: Int = NoCarry,
acc: List[String] = Nil): List[String] = (ls1, ls2) match {
case (Nil, Nil) if carry == NoCarry => acc
case (Nil, Nil) => s"000$carry"::acc
case (_, _) => val elem = carry +
ls1.headOption.getOrElse(0) +
ls2.headOption.getOrElse(0)
val elemLength = elem.toString.length
val (c, addedElem) =
if (elemLength > FixedLength) (1, elem.toString.tail)
else (NoCarry, elem.toString)
internalAddNumbers(
if(ls1.isEmpty) Nil else ls1.tail,
if(ls2.isEmpty) Nil else ls2.tail, c,
"0"*(FixedLength-elemLength)+addedElem::acc)
}
internalAddNumbers(ls1Reversed, ls2Reversed)
}
}
RepMiaDavis, abc at 8x8
I have excellent communication skills that allow me to effectively lead my team and convey my ideas to them so ...
RepWilliamWiltz, Call Girls in Chawri Bazar at Amdocs
Hi , I am William, a Medical technologist with extensive knowledge around sample examination and analyzing results. Selected from a pool ...
RepPeterRox, Java Developer at Cleartrip.com
I am Peter, a punctual, problem-solving, and communicative individual who possesses administration, finance, sales, computer skills and experience. I enjoy ...
Modified the solution to print the shortest paths to the final index.
- sidproquo August 29, 2016