2
\$\begingroup\$
list1 = input("Enter words =").split()
list2 = input("Enter words =").split()

def twincheck(l):
   
    if sorted(l[0])==sorted(l[-1]):
        st=True
        print(True)
    else:
        st=False
        print("\nFalse")
        
    if st==True:
        for i in range(len(l[0])):
            for j in range(len(l[-1])):
              if l[0][i]==l[1][j]:
                print('('+str(i)+',',end="")
                
                print(str(j)+')',end="")
                   
               
                   
twincheck(list1)  

twincheck(list2)
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Your function seems to be doing two things (which is not good) but it's doing more than just checking if two strings are anagrams of each other.. what were you trying to achieve? \$\endgroup\$
    – JeffUK
    Dec 10 '21 at 9:22
  • \$\begingroup\$ This function consists of one substantive line: sorted(l[0])==sorted(l[-1]) and a bunch of diagnostic printing. As such, there's not enough here to be worthy of a code review. Voting to close under the "Other" category. \$\endgroup\$
    – FMc
    Dec 10 '21 at 17:04
  • \$\begingroup\$ Let us continue this discussion in chat. (Moving to chat to clean up comments) \$\endgroup\$
    – Peilonrayz
    Dec 10 '21 at 22:34
  • 4
    \$\begingroup\$ I think it unfortunate that you prompt for entry of two lines/lists of words. The way twincheck() (lacking a docstring) is called, only the first and last word of each is used. It is "difficult" to tell what is and isn't intentional with no documentation, no tests, no example of input and required output. \$\endgroup\$
    – greybeard
    Dec 11 '21 at 12:33
3
\$\begingroup\$

A quick note about time complexity here: most of the time, Jakub's implementation of the is_anagram function is going to be pretty fast, but for large inputs, it can be slower than an implementation based on a hash-based data structure like Counter. Here is a small benchmark on strings of 10000 characters (this benchmark is pretty simple, and those results should be interpreted with caution):

import timeit
from collections import Counter


def is_anagram_counter(first_str: str, second_str: str) -> bool:
    """
    >>> is_anagram_counter("", "")
    True
    >>> is_anagram_counter("aba", "baa")
    True
    >>> is_anagram_counter("a", "b")
    False
    """
    return Counter(first_str) == Counter(second_str)


def is_anagram_sorted(first_str: str, second_str: str) -> bool:
    return sorted(first_str) == sorted(second_str)


def main():
    for implementation in "is_anagram_sorted", "is_anagram_counter":
        t = timeit.timeit(
            f"{implementation}(first, second)",
            setup=f"""
import random
from __main__ import is_anagram_sorted, is_anagram_counter
count = 10000


def generate_random_str(length):
    return "".join(map(str, (random.randint(0, 26) for _ in range(length))))


first = generate_random_str(count)
second = generate_random_str(count)
""",
            number=10000
        )
        print(f"{implementation}: {t} s")


if __name__ == "__main__":
    main()

On my machine, I got:

is_anagram_sorted: 22.178317 s
is_anagram_counter: 9.081907600000001 s
\$\endgroup\$
1
  • \$\begingroup\$ Yes, the counter should be much faster than sorting. Suggested sorted as it's dead easy to remember for anyone who is just starting. Sort them in the right order and check if they are the same :D Great work ;) \$\endgroup\$
    – Jakub
    Dec 17 '21 at 22:09
1
\$\begingroup\$

This is all you need to check if a string is an anagram of another:

def is_anagram(first_str: str, second_str: str) -> bool:
    return sorted(first_str) == sorted(second_str)

Your function doesn't test anything as it takes only one parameter. If your input will contain only one element it will decide input is an anagram. Simpler way is to use my function and call it:

is_anagram("test", "input what you want")

or

f_input = input()
s_input = input()
is_anagram(f_input, s_input)

This will work for both, single and multiple element inputs.

\$\endgroup\$
2
  • \$\begingroup\$ Actually in code i also have to return the index where alphabet of first string is present in string two. \$\endgroup\$
    – King _ AJ
    Dec 17 '21 at 15:22
  • \$\begingroup\$ You can add that bit to this code. like if statement. if is_anagram(string_1, string_2): and then rest of the code to check indexes, so that would only run if they are anagrams. Next time please add a better description as from the title you only saying about checking if they are anagrams. We can only guess why the code is there if the description doesn't say anything about it. A lot of times people ( I did it too) write code what does nothing, or just been left and suits no purpose. \$\endgroup\$
    – Jakub
    Dec 17 '21 at 22:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.