how to iterate with elements stored? (i do not know how to word this..)

Viewed 53

You have found a machine which, when fed with two numbers s and e, produces a strange code consisting of the letters a and b. The machine seems to be using the following algorithm.

  1. Check if s is less than e - 1. If so, continue to step 2. If not, exit.
  2. Increment s by 1
  3. Decrement e by 1
  4. If this is the first letter you're producing, produce a. Otherwise produce a letter different from the one you last produced (only a and b may be produced)
  5. go to step 1.

check below for what i tried

def strangeCode(s, e):
    output = ""

    if s < e-1:
        s = s+1
        e = e-1
        if len(output) % 2:
            output = output+"b"
        else:
            output = output+"a"
        return strangeCode(s,e)
    return output

From my understanding, when i return strangecode(s,e), my output becomes an empty string again, and this is where i am stuck.

Example inputs and outputs are

input: s: 4 e: 8

output: "ab"

3 Answers

You may want to pass the output as parameter on each recursive iteration like this:

def strangeCode(s, e, output=""):
    if s < e - 1:
        s = s + 1
        e = e - 1
        if len(output) % 2:
            output = output + "b"
        else:
            output = output + "a"
        return strangeCode(s, e, output)
    return output

So that

print(strangeCode(4, 10))

Output this:

aba

Important note

Python does not optimize tail-recursion (Please check this), you should always do iterative algorithms. This is an alternative solution:

def strangeCodeIterative(s, e):
    output = ""
    while(True):
        if s > e - 1:
            break

        s = s + 1
        e = e - 1
        if len(output) % 2:
            output = output + "b"
        else:
            output = output + "a"
    return output

output = "" is re-initializing the output variable on each function call so len(output) will always evaluate to 0. I think, with your approach, you have to hand-over the output as a parameter to the function call like strangeCode(s,e,output) and remove the output = ""

You are using a functional approach since you have chosen recursion over iteration. In functional programming the following solution is called tail-reccursion.

    def strangeCode(s, e, output = ""):
        if s < e-1:
            s = s+1
            e = e-1
            if len(output) % 2:
                output = output+"b"
            else:
                output = output+"a"
            return strangeCode(s,e,output)
        return output


    strangeCode(4,8,output = "")

In case you need strictly only "s" and "e" as arguments then you can use a function wrapper:

def strangeCode(s, e):
    def _inner(s,e,output=""):
        if s < e-1:
            s = s+1
            e=e-1
            if len(output) % 2:
                output = output+"b"
            else:
                output = output+"a"
            return _inner(s,e,output=output)
        return output
    return _inner(s,e,output="")
Related