How to call a function recursively when using a WHILE loop and break it properly?

Metadata

I am trying to transform a string by removing a letter A with an adjacent letter B or by removing a letter C toghether with an adjacent letter D.

For example 1, given a string "CBACD", it should be transformed as

CBACD -> CCD -> C

Example 2: given a string "CABABD", it should return nothing as the transformation goes like below:

CABABD -> CABD -> CD -> 

Example 3: "ACBDACBD", There are no corresponding adjacent characters to A & C so the entire string should be returned

"ACBDACBD" -> "ACBDACBD"

I have written the following code to do the operation:

object RemoveCharsABCD {

    val s = scala.io.StdIn
    def adjacent(s: String): String = {
        val charSet = ArrayBuffer("AB","BA","CD","DC")
        var i   = 0
        var ret:String = ""
        while(i < s.length-1) {
            if(charSet.contains(s"${s.charAt(i)}${s.charAt(i+1)}")) {
                s.slice(i+2, s.length)
                i += 2
                if(i == s.length-1) ret = s"$ret${s.charAt(i).toString}"
            } else {
                    ret = s"$ret${s.charAt(i).toString}"
                    i += 1
                    if(i == s.length-1) ret = s"$ret${s.charAt(i).toString}"
            }
        }
        println("Ret: " + ret)
        ret
    }

    def main(args: Array[String]): Unit = {
        println("Enter a String: ")
        var s = scala.io.StdIn.readLine().toString
        adjacent(s)
    }
}

The above code works fine for the first iteration which is: CABABD -> CABD For the inputs: ACBDACBD, CBACD, the output is correct but for ACBDACBD, the output is CD. I called the method adjacent before the print statement as below:

if(ret.length >= 2) {
    adjacent(ret)
}
println("Ret: " + ret)

But this goes to infinite loop and give stackoverflow exception. I am unable to call the method: adjacent recursively so that it can work until the end of the string ? Could anyone let me know how can I properly call the method: adjacent recursively so that the entire string is processed until the end ?

jwvh

Seems pretty straight forward.

@annotation.tailrec 
def adjacent(s: String): String = {
  val next = s.replaceAll("AB|BA|CD|DC", "")
  if (s == next) s else adjacent(next)
}

adjacent("CBACD")     //res0: String = C
adjacent("CABABD")    //res1: String =
adjacent("ACBDACBD")  //res2: String = ACBDACBD

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to break a loop using a function

Why is my python function not working properly when I call it recursively?

how to break out of loop when while mocking

How to break a while loop, when duplicate occurs

How can i use "break" properly into a while loop?

How Can I Properly Use While Loop And Not Have it Repeat On Break

How to properly call a recursive function inside a for loop?

How to break a 'while' loop

Python: Unable to call function when in while loop

Exiting A While Loop Using A Function Call

docker function does not call itself recursively when using sudo?

How would I break out of a function inside of a while loop in python?

How to break a while loop when it is false to a certain condition

How to break while loop when see empty line in Java?

How to break while loop when reading a serial port in python

How to break a while loop when input is a particular string?

How to break a while loop when reached limits on Twitter's API?

How to break while loop when a new message arrives?

How to do infinite loop using do while and break if something failed?

How to break a while loop using asyncio in discord python?

How (break; while) and (continue; loop)

How to break a while loop in Kivy?

ESP was not properly saved across a function call when using function pointers

How to call promise function recursively

How to recursively call an asynchronous function?

How to call a function parallelly in a while loop if a condition is met within the loop?

How do I properly use a function in a do/while loop?

how to properly call a function when it is a pointer to a class function but is outside that class

While Loop Doesn't End when i call my function