-1

I was trying to write a function encodeM which basically takes a string s and integer move which shifts the ascii value of a character. I tried writing the funtion the normal way which I think works properly. Is there a way I can write the same function with recursion? I have provided the code for the function with normal way:

def encodeM(s,move):
    newStr=""    
    for char in message:
        num=ord(char)
        if char.isalpha():
            num+=move
            if char.isupper():
                if num>ord("Z"):
                    num-= 26
                elif num<ord("A"):
                    num+= 26
             elif char.islower():
                 if num>ord("z"):
                     num-= 26
                 elif num<ord("a"):
                    num+=26
            newStr+=chr(num)
        else:
            newStr+=char

    return newStr    
  • why do you need it recursive? – Netwave Jun 02 '17 at 07:25
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jun 02 '17 at 16:18

2 Answers2

0

It desn't make too much sense, but you just have to erase the loop, check the base case and operate over one element:

def encodeM(s, move):
    if s == "":
        return s
    char = s[0]
    num = ord(char)
    if char.isalpha():
        num += move
        if char.isupper():
            if num > ord("Z"):
                num -= 26
            elif num < ord("A"):
                num += 26
        elif char.islower():
            if num > ord("z"):
                num -= 26
            elif num < ord("a"):
                num += 26
        char += chr(num)
    return char + encodeM(s[1:], move)

Its an ugly and inefficient way of doing this.

Netwave
  • 40,134
  • 6
  • 50
  • 93
0

You can do as following :

def encodeM(s, move, n = 0):
    if n >= len(s):
        return s
    char = s[n]
    num = ord(char)
    if char.isalpha():
        num += move
        if char.isupper():
            if num>ord("Z"):
                num-= 26
            elif num<ord("A"):
                num+= 26
        elif char.islower():
            if num>ord("z"):
                num-= 26
            elif num<ord("a"):
                num+=26
        s = s[:n] + chr(num) + s[n+1:]
    return encodeM(s, move, n+1)

print(encodeM("abcdef",42))  # Just input your string and your move variable

This recursion shifts one letter by one letter.

There is another way, if you want :

def encodeM2(s, move, n = 0):
    if n >= move:
        return s
    newStr = ""
    for char in s:
        num = ord(char)
        if char.isalpha():
            num += 1
            if char.isupper():
                if num>ord("Z"):
                    num-= 26
                elif num<ord("A"):
                    num+= 26
            elif char.islower():
                if num>ord("z"):
                    num-= 26
                elif num<ord("a"):
                    num+=26
            newStr+=chr(num)
        else:
            newStr += char
    return encodeM2(newStr, move, n+1)

This one shifts all the character by 1, then calls itself until it has moved by move each character.

Abrikot
  • 965
  • 1
  • 9
  • 21