-1

For interactive parsing purposes, given an input string I need to extract the longest possible substring starting at index 0 and having only matched parentheses.

Example (LISP-like s-expressions)

Input string: (print "hello") (assign a (+ c d)) (assign e (+ f g)

Output substring: (print "hello") (assign a (+ c d))

I would like to make a simple Python function to achieve this.

Kubuntuer82
  • 1,487
  • 3
  • 18
  • 40

1 Answers1

1

Loop over the string while counting parentheses, and at the end just slice the string to the last index where the parenthesis counter was 0:

def max_parseable_substring(text):
    parentheses = 0
    end = 0

    for i, char in enumerate(text):
        if char == "(":
            parentheses += 1
        elif char == ")":
            parentheses -= 1

        if parentheses == 0:
            end = i

    return text[:end]
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    This is almost identical to my answer. – Kubuntuer82 Apr 07 '18 at 16:43
  • @Kubuntuer82 Yes, your answer is pretty similar now that you've incorporated my suggestion. I found it preferable to post my own answer instead of repeatedly giving you improvement suggestions. – Aran-Fey Apr 07 '18 at 16:45
  • 1
    Yes @Aran-Fey but the improved answer was already there when you posted yours. You could have at least reconsidered your judgement of it (you wrote it wasn't a _good_ answer) and at least appreciate it a bit more (considering its score, which is related to first attempt which has been removed) instead of ignoring it and just posting an almost identical algorithm. I appreciate your suggestion and I upvoted it, but honestly I took some more time to improve it and it would have been nicer to not ignore this extra-time-taking effort putting an identical algorithm. – Kubuntuer82 Apr 07 '18 at 16:52
  • 1
    @Kubuntuer82 It's still not a good answer though. It's unpythonic and too hard to read considering how simple of a task this is. You're right that posting a near-identical answer is a questionable practice, but the only reason why they're similar is because they're both based on _my_ ideas. It's not like I copied your code, so what's wrong with me taking credit for my own solution? – Aran-Fey Apr 07 '18 at 17:00
  • Yes @Aran-Fey but I made the effort to make an initial algorithm and you clearly started from my algorithm adding your improvements, which are important improvements (I admit that) but clearly took less time than writing a draft algorithm from scratch. It would have been fine to write your own answer, but at least do not suggest me to improve mine if you know you are going to write yours, because it is extra time spent for no reason. At the end, after I improved it didn't deserve your judgement anymore (it was short and readable as yours, and the question doesn't require it to be _pythonic_). – Kubuntuer82 Apr 09 '18 at 09:08
  • Anyway, I accepted it because from the beginning I said that to be fair I was not going to accept my own answer (which was just a basic one) to give people the chance to provide a better answer. I just suggest you to avoid this behaviour in the future and to be clearer whether you want people to improve their answer or whether you want to put those improvements in your own answer, this can make people save time and energy and is therefore a more respectful behaviour. – Kubuntuer82 Apr 09 '18 at 09:12
  • @Kubuntuer82 I wasn't planning to write my own answer from the start. I thought that your answer would be alright if you got rid of the repeated string concatenation, but after you made the edit I realized that there were still significant improvements to make. I'm sorry I made you waste time improving it. (P.S. I didn't start from your algorithm. What I have posted here is the obvious way to implement it. I really didn't need to copy anything from you.) – Aran-Fey Apr 09 '18 at 09:47