2

Okay so I have two functions, one that checks how many times a character appears in a string and another that is supposed to check if the two strings the user inputs are anagrams:

def function_one(s, ch):
    count = 0
    for c in s:
        if c == ch:
            count = count + 1
    return count

def function_two(s, t):
    while len(s) == len(t):
        y = function_one(s, t)
        if y == 0:
            return True
        else:
            return False

Right now function_two will return True if the two strings are anagrams, but it will also return True if they are anagrams but have different letters capitalized, and will return nothing if they aren't anagrams at all. What do I do?

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
amer_413
  • 33
  • 4
  • 7
    Possible duplicate of [Checking strings against each other (Anagrams)](https://stackoverflow.com/questions/14990725/checking-strings-against-each-other-anagrams) – Austin Oct 11 '18 at 02:18
  • Your code has various problems, you should inspect and test it step by step. Note that you call `function_one()` with two full strings, not a string and a letter-- try calling it this way (directly, so you can inspect the results) and see what happens. – alexis Oct 11 '18 at 07:23

2 Answers2

1

There's a lot of ways of doing this. I think you're complicating things a bit by writing these functions.

The first way of solving this that came in my mind was generating a list with all the letters in each string, sorting them and comparing both lists:

def check_for_anagrams(string1, string2):
    list1 = list(string1.lower())
    list2 = list(string2.lower())
    list1.sort()
    list2.sort()

    return list1 == list2

it might not be the most elegant way to do it, but it's a quick solution. the lower() function turns strings into lowercase the sort() function for lists sorts the list.

if two strings are anagrams, the result of these operations will be two equal lists.

EDIT: Check the possible duplicate thread, there's some nice solutions there!

nnov
  • 489
  • 6
  • 14
  • 1
    by the way, try to use more descriptive names in the variables, otherwise the code is hard to read. – nnov Oct 11 '18 at 02:37
  • 1
    To convert a string into a list of lower characters you simply need to call `list(string.lower())`. There is no need to use a list comprehension. – Sven-Eric Krüger Oct 11 '18 at 07:21
  • 1
    @SvenKrüger i didn't use the list comprehesion to apply the lower function, it was the first thing that came to my mind to create a list out of a string. i said it wasn't very elegant, i did a quick answer :) – nnov Oct 12 '18 at 15:07
0

You can sort out pairs of string that are no anagrams by checking the length of both strings first. In a second stage you can compare the set of characters. This is very fast in Python. In the last stage you have to count the occurences of the single characters in the set to be very sure the strings are anagrams.

CODE

from __future__ import print_function

def isAnagram(s1, s2) :
    s1, s2 = s1.lower(), s2.lower()

    if len(s1) == len(s2) :
        if set(s1) == set(s2) :
            return all([s1.count(c) == s2.count(c) for c in set(s1)])

    return False

string1 = 'Hello'
string2 = 'Hell'    # No anagram of string1
string3 = 'Holle'   # Anagram of string1    

print(isAnagram(string1, string2))
print(isAnagram(string1, string3))

OUTPUT

False
True
Sven-Eric Krüger
  • 1,277
  • 12
  • 19