#πŸ”’ Find anagrams

39 messages Β· Page 1 of 1 (latest)

shell drum
#
def find_anagrams(word, candidates):
    exp = []
    for candidate in candidates:
        new = ""

        for letter in candidate:
            if not letter in word:
                break
            
            new += letter
        if new and  new != word:
           if len(new) == len(word):
              exp.append(new)
    return exp


candidates = ["patter"]
print(find_anagrams("tapper", candidates))```
old yachtBOT
#

@shell drum

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

shell drum
lethal ginkgo
#

henlo

shell drum
#

hi

lethal ginkgo
#

ok so you take all from candidates that are anagrams of the word?

shell drum
#

yes

lethal ginkgo
#

so the length must be the same, right?

shell drum
#

now the repeating letters of word is causing me problems

shell drum
lethal ginkgo
#

!d sorted

old yachtBOT
#

sorted(iterable, /, *, key=None, reverse=False)```
Return a new sorted list from the items in *iterable*.

Has two optional arguments which must be specified as keyword arguments.

*key* specifies a function of one argument that is used to extract a comparison key from each element in *iterable* (for example, `key=str.lower`). The default value is `None` (compare the elements directly).

*reverse* is a boolean value. If set to `True`, then the list elements are sorted as if each comparison were reversed.

Use [`functools.cmp_to_key()`](https://docs.python.org/3/library/functools.html#functools.cmp_to_key) to convert an old-style *cmp* function to a *key* function.
lethal ginkgo
#

!e

print(sorted("owns"))
print(sorted("snow"))
old yachtBOT
#

@lethal ginkgo :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | ['n', 'o', 's', 'w']
002 | ['n', 'o', 's', 'w']
shell drum
#

am i on right track?

lethal ginkgo
shell drum
#

so its just one line of code? i dont need looping?

lethal ginkgo
#

well you need to loop through all the candidates

shell drum
#

i mean letters

lethal ginkgo
#

and you should save the sorted word

shell drum
#

i didnt knew sort can solve it

lethal ginkgo
#

do you know list comprehension?

shell drum
shell drum
lethal ginkgo
#

because if you have word "aabb", then your code would say that "abbb" would be an anagram, but that's not true

#

the count of each letter has to be the same too

shell drum
#

ok i will try with sorted thanks

lethal ginkgo
#

πŸ‘

shell drum
lethal ginkgo
#

because you can use it very cleanly in this scenario

#

||

def find_anagrams(word: str, candidates: list[str]) -> list[str]:
    sorted_word = sorted(word)
    return [
        cand
        for cand in candidates
        if sorted(cand) == sorted_word
    ]

||

shell drum
#

yeah ok

#

thanks againn can i close?

lethal ginkgo
#

I don't have anything else to say, the problem is solved

shell drum
#

!close

old yachtBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.