0

Logic which I used to find anagram is as following

first I stored characters of first string in empty array

then I checked one by one if the characters of string2 is present already present in arr which i created

if yes then remove if no then append

at the end if whole of the array is empty then yes string is anagram else not

  t=int(input())
  for i in range(t):
      n1=input()
      n2=input()
      arr=[]


  for ch in n1:
      arr.append(ch)
  for ch in n2:
      if ch in arr:

          arr.pop(ch)
      else:

          arr.append(ch)
  if arr==[]:
      print("yes")
  else:
      print("no")

with this code the error is comming out to be

TypeError: 'str' object cannot be interpreted as an integer how do i rectify this error

  • 3
    Check what argument `list.pop` expects, and compare that with what your code provides it, then consider using `list.remove` instead. – DeepSpace Jul 08 '20 at 13:24
  • Which line of code is the error on? And please include the actual code. This code seems to mix up `n1` and `s1`, for example. – jarmod Jul 08 '20 at 13:25
  • you have posted this question before it was closed now you have posted it again with another account – deadshot Jul 08 '20 at 13:34
  • 1
    are you trying to solve your problem or expecting a better answer this may help https://stackoverflow.com/a/14990938/9050514 – deadshot Jul 08 '20 at 13:37

1 Answers1

2

You get this error because the list.pop waits integer type but it gets string. You should use the list.remove, it can handle the string type (It removes only the first occurrence of element, so the duplicated characters cannot cause turbulence). I have written a little example.

Code:

n1 = input("First word: ")
n2 = input("Second word: ")
arr = []

for ch in n1:
    arr.append(ch)
for ch in n2:
    if ch in arr:
        arr.remove(ch)
    else:
        arr.append(ch)
if not arr:  # True if the list is empty.
    print("Anagram")
else:
    print("NOT Anagram")

Output:

>>> python3 test.py
First word: hello
Second word: lelab
NOT Anagram

>>> python3 test.py
First word: hello
Second word: oelhl
Anagram
milanbalazs
  • 4,811
  • 4
  • 23
  • 45