Search code examples
stringscalamultiplication

Multiplying a string k*horizontally and v*times vertically


Problem:

You are given a string of n lines, each substring being n characters long. For example:

s = "abcd\nefgh\nijkl\nmnop"

A k-horizontal scaling of a string consists of replicating k times each character of the string (except '\n').

A v-vertical scaling of a string consists of replicating v times each part of the squared string.

Function scale(strng, k, v) will perform a k-horizontal scaling and a v-vertical scaling.

Example:

a = "abcd\nefgh\nijkl\nmnop"
scale(a, 2, 3) --> "aabbccdd\naabbccdd\naabbccdd\neeffgghh\neeffgghh\neeffgghh\niijjkkll\niijjkkll\niijjkkll\nmmnnoopp\nmmnnoopp\nmmnnoopp".

My problem is that I've already written the function and for my liking it is completely correct.

But when I try and run the program through the tests, there a single bug, thanks to which my program doesn't pass any of them.

I attached the tests to to the bottom of the question, please help.

object Kata {

    def scale(xs: String, k: Int, n: Int): String ={
      if(xs=="")
        ""
      else
      xs.split("\n")
   .map(_.map(_.toString*k).mkString("","","\n") * n)
   .mkString
      }



}
> Test Results:
 KataTest
 scale(abcd\nefgh\nijkl\nmnop, 2, 3) should return aabbccdd\naabbccdd\naabbccdd\neeffgghh\neeffgghh\neeffgghh\niijjkkll\niijjkkll\niijjkkll\nmmnnoopp\nmmnnoopp\nmmnnoopp
Test Failed

>"...pp
mmnnoopp
mmnnoopp[
]" was not equal to "...pp
mmnnoopp
mmnnoopp[]"
 Stack Trace
Completed in 24ms
 scale(, 5, 5) should return
 scale(Kj\nSH, 1, 2) should return Kj\nKj\nSH\nSH
Test Failed

>"Kj
Kj
SH
SH[
]" was not equal to "Kj
Kj
SH
SH[]"
 Stack Trace
 scale(lxnT\nqiut\nZZll\nFElq, 1, 2) should return lxnT\nlxnT\nqiut\nqiut\nZZll\nZZll\nFElq\nFElq
Test Failed

>"...
ZZll
ZZll
FElq
FElq[
]" was not equal to "...
ZZll
ZZll
FElq
FElq[]"
 Stack Trace
Completed in 1ms
 scale(YVjosW\nHGhKGZ\nLHNMLm\nJtcWCj\ngVtjyk\nOJBkOK, 2, 2) should return YYVVjjoossWW\nYYVVjjoossWW\nHHGGhhKKGGZZ\nHHGGhhKKGGZZ\nLLHHNNMMLLmm\nLLHHNNMMLLmm\nJJttccWWCCjj\nJJttccWWCCjj\nggVVttjjyykk\nggVVttjjyykk\nOOJJBBkkOOKK\nOOJJBBkkOOKK
Test Failed

>"...BkkOOKK
OOJJBBkkOOKK[
]" was not equal to "...BkkOOKK
OOJJBBkkOOKK[]"
 Stack Trace
Completed in 1ms

Solution

  • First replica each letter of the line by k times and then replicate each line by v times.

    @ 
    def scale(xs: String, k: Int, n: Int): String = xs.split("\n").flatMap(line => Array.fill(n)(line.flatMap(char => s"$char" * k))).mkString("\n") 
    defined function scale
    
    @ scale("abcd\nefgh\nijkl\nmnop", 2, 3) 
    res11: String = """aabbccdd
    aabbccdd
    aabbccdd
    eeffgghh
    eeffgghh
    eeffgghh
    iijjkkll
    iijjkkll
    iijjkkll
    mmnnoopp
    mmnnoopp
    mmnnoopp"""
    

    Summary:

     def scale(xs: String, k: Int, n: Int): String = xs
     .split("\n")
     .flatMap { line =>
       val repeatKTimes = line.flatMap(char => s"$char" * k) // each char is repeated k times
       Array.fill(n)(repeatKTimes)
     }.mkString("\n")