anindya20192020
anindya20192020

Reputation: 55

How to solve missing 1 required positional argument in this python code?

I am testing these two string characters whether they are string permutation or not for the following python code but I am getting error which I have attached after the code.

from collections import Counter

    str_1 = "driving"
    str_2 = "drivign"

    def check_permutation(str_1,str_2):
        if len(str_1) != len(str_2):
            return False
        counter = Counter()
        for c in str_1:
            counter[c] += 1
        for c in str_2:
            if counter[c] == 0:
                return False
            counter[c] -= 1
        return True
    print(check_permutation((str_1, str_2)))

Error:

Traceback (most recent call last):
TypeError: check_permutation() missing 1 required positional argument: 'str_2'

How can I solve this error and print the output in console?

Upvotes: 2

Views: 1332

Answers (1)

Emma
Emma

Reputation: 27723

  • I guess you just had extra parentheses in your code, with minor indentation issue in your inputs str_1 and str_2:

  • Also, it would be best to declare your variables close to where you would be calling or using them.

  • You can also improve a little bit on naming your variables.

Code

from collections import Counter


def check_permutation(str_1, str_2):
    if len(str_1) != len(str_2):
        return False
    count_map = Counter()
    for char in str_1:
        count_map[char] += 1
    for char in str_2:
        if count_map[char] == 0:
            return False
        count_map[char] -= 1
    return True


str_1 = "driving"
str_2 = "drivign"

print(check_permutation(str_1, str_2))

Output

True

I guess, the following code would probably be what you are trying to do:

from collections import Counter


def check_permutation(str_1, str_2):
    if len(str_1) != len(str_2):
        return False
    count_map_1 = Counter(str_1)
    count_map_2 = Counter(str_2)
    return count_map_2 == count_map_1


str_1 = "driving"
str_2 = "drivign"

Upvotes: 1

Related Questions