flipCoder
BAN USER// 1 -> B
// 13 -> BD, N
// 132 -> BDC, NC
// 112 -> BBC, LC, BM
def stringFinder(s:String):List[String] = {
// convert integer String to a letter
val is2letter: String => String = (si:String) =>('a' + si.toInt).asInstanceOf[Char].toString
implicit def cToS(c:Char):String = c.toString
if(s.length == 1) {
List(is2letter(s))
}else if(s.length == 2){
val s1 = is2letter(s(0)) + is2letter(s(1))
val s2:String = s match{
case a if a.toInt >= 'a'-'a' && a.toInt <= 'z'-'a'=> is2letter(a)
case _ =>""
}
if(!s2.isEmpty) List(s1,s2) else List(s1)
} else {
var results = List[String]()
val s0 = is2letter(s(0))
for(_s:String <- stringFinder(s.tail)) {
results = (s0+_s) :: results
}
if(s0 != "a") {
s.substring(0,2) match{
case a if a.toInt >= 'a'-'a' && a.toInt <= 'z'-'a'=> {
val s1 = is2letter(a)
for(_s:String <-stringFinder(s.tail.tail)) {
results = (s1+_s) :: results
}
}
case _ =>""
}
}
results
}
}
val thouMap = Map(0 -> "",
1 -> "thousand",
2 -> "million",
3 -> "billion")
def recurseThou(i:Int):String = {
// recursive version of chopping off every thousand
// in this version we dont actually know the thousandth factor until the next call chain
// for example
// recursThou(15299)
// (s = 15,fact = 1)
// (15 + thouMap(1) + zeroTo999(299), fact = 0)
def recursThouHelper(i:Int)(factor:Int):(String, Int) = {
if(i/1000 == 0) {
(zeroTo999(i%1000), factor)
} else {
val (s, fact) = recursThouHelper(i/1000)(factor + 1)
( s + " " + thouMap(fact) + " "+ zeroTo999(i%1000), fact -1)
}
}
recursThouHelper(i)(0)._1
}
// alternative to the recusive thousand call
def iterativeThou(i:Int):String = {
var quot1000 = i
var thousandFactor = 0
var word:String = ""
while(quot1000 != 0) {
word = zeroTo999(quot1000%1000) + thouMap(thousandFactor) + word
quot1000 = quot1000/1000
thousandFactor = thousandFactor +1
}
word
}
def zeroTo999(i:Int):String = {
val sMap = Map(
0 ->"zero",
1->"one",
2->"two",
3->"three",
4->"four",
5->"five",
6->"six",
7->"seven",
8->"eight",
9 -> "nine", // digit 9,factor 0
10 -> "ten", // digit 0, factor 0
11 -> "eleven", // digit 1, factor 1, digit 1, factor 0
12 -> "twelve", // digit 1, factor 1, digit 2, factor 0
13 -> "thirteen", // digit 1 factor 1, digit 3, factor 0
14 -> "fourteen",
15 -> "fifteen",
16 -> "sixteen",
17 -> "seventeen",
18 -> "eighteen",
19 -> "nineteen",
20 -> "twenty",
30 -> "thirty",
40 -> "forty",
50 -> "fifty",
60 -> "sixty",
70 -> "seventy",
80 -> "eighty",
90 -> "ninety"
)
if(i > 999){
throw new Exception("Only valid for 0 - 999")
}
if(i <20)
sMap(i)
else if(i>=20 && i <= 99) {
if(i%10 != 0)
sMap(i/10 *10) + sMap(i%10) // i.e. 21, 22, 23
else
sMap(i/10 * 10) // 20, 30, 40
} else {
// 100 - 999
if(i%100 == 0)
sMap(i/100) + " hundred "
else
sMap(i/100) + " hundred " + zeroTo999(i%100)
}
}
implicit val recurse:Int => String = recurseThou
// idea here is that we chop of every 1000 and calculate how to say that with some map of which thousandnth factor
// we are in. We also introduce a string calculator for any word between 0-999
// so we have calculate0to999 + f(thousandthfactor) which returns thousand, million, etc
// i.e. 1,234,001,999
// ^ 1 + thousandthfactor is 3 (billion)
// ^ 234 + thousandthFactor is 2 (million)
// ^ 1 + thousandthFactor is 1 (thousand)
// ^ 999 + thousandthFactor is 0
def equivalentNumtoString(i:Int)(implicit f:Int => String):String = {
f(i)
}
Not very efficient but it does the job when under the pressure and you forgot how to implement a trie
// O(n^2) running time
def wordSeparator2(dict:Set[String], s:String):List[String] = {
val chars = s.toCharArray
var words = List[String]()
var i = 0
var j = i
while(i < chars.size) {
val c = chars(i)
var word = ""
var unusedChars = ""
// be greedy and try to get the longest word
for(j <- i to chars.length - 1) {
if(dict.contains(word + unusedChars + chars(j))) {
word = word + unusedChars + chars(j)
unusedChars = ""
i = j
} else {
unusedChars = unusedChars + chars(j)
}
}
if(word != "")
words = word :: words
i = i +1
}
words
}
110511 -> List(lafl, lafbb, bbafbb, bbafl, bkfl, bkfbb)
- flipCoder January 16, 2015