I normally spend a lot of time println debugging my programs to make sure they do what I believe. However, when I am done with the debugging there are a lot of unnecessary println's everywhere. Is there some way of not throwing this work away and still have it for later? I know that you could write unit test for this and stick the "println's" there, but I would rather not set up a framework for every little program I write.
My suggestion would be to have a debug file and a clean file and just save both. Does anyone else have a nice idea for this? As an example, here is my almost finished scala program for solving the "Sherlock and the Valid String" on Hackerrank see here.
import java.io.PrintWriter
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.{Set => MutSet}
import scala.collection.mutable.{Map => MutableMap}
import scala.collection.mutable.{ListBuffer}
object Solution {
/**
@TODO "Sherlock and the Valid String", Strings
*/
// Complete the isValid function below.
def isValid(s: String): String = {
// Count frequencies
var freqmap = Map[Char, Int]()
var old = 0
for (c <- s) {
old = freqmap.getOrElse(c, 0) + 1
freqmap = freqmap + (c -> old)
}
println(s"Counts of characters in string:\n" + freqmap.mkString(", \n"))
// Check if problems or okay
var oneDeviation = false
var initFreq = freqmap(s(0))
val allowedFreqs = ListBuffer(initFreq)
val counter = MutableMap[Int, Int](initFreq -> 1)
var isFreqOne = false
var freqThatIsOne: FreqIsOne = None
println(s"Init freq: $initFreq")
println(s"counter: " + counter.mkString(", \n"))
println()
for (freq <- freqmap.values) {
println("------------------")
println(s"Freq: $freq")
if (!oneDeviation) {
if (freq != initFreq) {
oneDeviation = true
// greater than 1 diff and no freq is 1 , eg 2 and 4
// if pass this we know diff is 1
if (isBiggerThanOneDiffAndNoOneFreq(initFreq, freq)) {
return "NO"
}
// check if we have a freq that is one
if (initFreq == 1 || freq == 1) {
// one freq which is 1
if (initFreq == 1) {
isFreqOne = true
freqThatIsOne = Init
// only freq allowed
allowedFreqs.remove(0)
allowedFreqs += freq
} else if (freq == 1) {
isFreqOne = true
freqThatIsOne = Freq
// only allowed is initFreq
}
// one diff but larger than 1 freqs, only allowed
// is the lower frequency
} else {
if (freq > initFreq) {
allowedFreqs.remove(0)
allowedFreqs += freq
}
}
// no new freq
} else {
}
} else {
if (!allowedFreqs.contains(freq)) return "NO"
}
old = counter(initFreq)
counter(initFreq) = old + 1
println(s"counter: " + counter.mkString(", "))
}
"YES"
}
def isBiggerThanOneDiffAndNoOneFreq(initFreq: Int, freq: Int): Boolean = {
math.abs(freq - initFreq) > 1 && (initFreq != 1 && freq != 1)
}
sealed trait FreqIsOne
case object Init extends FreqIsOne
case object Freq extends FreqIsOne
case object None extends FreqIsOne
def main(args: Array[String]) {
val stdin = scala.io.StdIn
val s = stdin.readLine
val result = isValid(s)
println(result)
}
}