What would be the best approach to solve this using OOP?

Viewed 708

I bombed my interview after I got this OOP question from a company. Can one of you experts help solve this using Python? I had used switch cases (using python dictionary) to solve this problem without OOP.

Original string: abcdefghijklmn

Operations:F -> move curser forward, B -> move curse backward, R -> replace char

Operation string: F2B1F5Rw -> abcdefwhijklmn (expected output)

Moving forward by 2 chars, move backward by 1 char, move forward 5 chars, replace 1 char to be ‘w’

We can assume that curser is at first character at the beginning. How can I add more operations using OOP if required?

But apparently the interviewer wasnt too happy with my switch case approach, and as a follow up asked me to solve the problem using OOP. Any ideas how I could solve this without switch cases? A better approach using OOP principles or perhaps a better Data Structure I am not aware of?

2 Answers

The reason behind the 'dissatisfaction' does not seem to lie in switch ... case or its implementation using dictionary. To me it seems to be related to the concept of OOP. He/she might have wanted you to construct OOP structure of the task.

I would define a class with two state variables: one for the current string and one for the current position. Methods are the actions exerted on the string. For example, replace(char) (or r(char) in the code below) would replace the character at the current position with char. With these in mind, I define a class named Editor, create an object from that, and play with the object.

class Editor():
    def __init__(self, text):
        self.text = text
        self.pos = 0

    def f(self, step):
        self.pos += int(step)

    def b(self, step):
        self.pos -= int(step)

    def r(self, char):
        s = list(self.text)
        s[self.pos] = char
        self.text = ''.join(s)
        # could've just stored the list of chars (instead of string)
        # from the beginning, but that's rather a secondary issue.

    def run(self, command):
        command = list(command)
        while command:
            method = getattr(self, command.pop(0).lower())
            arg = command.pop(0)
            method(arg)

    def __str__(self):
        return self.text

text = 'abcdefghijklmn'
command = 'F2B1F5Rw'

ed = Editor(text)
ed.run(command)

print(ed)

An advantage of OOP is that you can flexibly add many more different (char-wise) actions like upper-lower conversion to the class. That, in my opinion, was what the interviewer was asking for.

You can do this as follows:

orig_string = 'abcdefghijklmn'
slist = list(orig_string)
opstr = 'F2B1F5Rw'
pos = 0
for w,p in zip(opstr[::2],opstr[1::2]):
    if w == 'F': pos += int(p)
    elif w == 'B': pos -= int(p)
    elif w == 'R': slist[pos] = p
    else: print ('Invalid Operational String... Aborting'); break
else:
    print (''.join(slist))

You don't need to use a switch case. You can use a if statement and take care of the current position based on the values.

If the value was: opstr = 'F2B1F5RwX2', then the output will be: Invalid Operational String... Aborting

Related