0

In this Python code (the algorithm itself isn't relevant to my question):

def test_balanced(s):
    # Check the parentheses in the given string are balanced
    count = 0
    for c in s:
        if c == '(':
            count += 1
        elif c == ')':
            if count == 0:
                # Close before open
                return False
            count -= 1
    # Check all open have been closed
    return count == 0

def remove_odd_brackets(s):
    if test_balanced(s):
        return {s}
    
    # Find positions of opening and closing parentheses
    os = [i for i, c in enumerate(s) if c == '(']
    cs = [i for i, c in enumerate(s) if c == ')']
    
    # Progressively remove more parentheses until we find valid solution(s)
    for n in range(1, len(os + cs)):
        result = []
        for c in combinations(sorted(os + cs), n):
            test = [ch for i, ch in enumerate(s) if i not in c]
            if test_balanced(test):
                result.append(''.join(test))
        if result:
            return set(result)
    return {''.join(c for c in s if c not in '()')} 

I don't understand how is it possible to call the function test_balanced(s), first with s as a string, and then as a list of strings of length 1 each.

Is it a strange example of function overloading? (strange for me because there is only one implementation of the function...)

I do not understand what the interpreter is doing in this case "behind the scenes" (briefly).

Thanks in advance!

1
  • 1
    in python you can traverse both array/list/string in same way. for ele in arr = for chr in string. That's what the 2nd line of test_balanced function is doing. Commented Nov 16, 2021 at 15:16

2 Answers 2

1

Check the implementation of test_balanced. It's only argument is s. And what does it do with s?

  • It iterates through the values in s. So s has to be an iterable object.
  • It compares the values in s against some strings. So the values in s should be strings. Of course, this isn't enforced but the function may not work correctly otherwise so let's assume this is implied.

It asserts nothing else on s. So anything that has these two properties will work with test_balanced.

Both str and List[str] have these properties. So the function works for both types. It's called duck-typing. Python is a dynamically typed language where types are only determined at runtime. Hence the function doesn't complain. There isn't any overloading going on here.

Sign up to request clarification or add additional context in comments.

Comments

0

It isn't overloading as there really is only one function. Python is duck-typed which allows for flexibility like what you are describing.

In python you can pass variables of any type that you want into any function that you want. It is up to the function to handle the approprirate variable types, and it's up to the user not to misuse the functions.

In this case, both a string and a list are iterables, so you can call "for c in ..." on both of them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.