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))```
#π Find anagrams
39 messages Β· Page 1 of 1 (latest)
@shell drum
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.
Closes after a period of inactivity, or when you send !close.
henlo
hi
ok so you take all from candidates that are anagrams of the word?
yes
so the length must be the same, right?
now the repeating letters of word is causing me problems
yes
!d sorted
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.
!e
print(sorted("owns"))
print(sorted("snow"))
@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']
am i on right track?
see that these would be equal
oh
so its just one line of code? i dont need looping?
well you need to loop through all the candidates
i mean letters
and you should save the sorted word
i didnt knew sort can solve it
do you know list comprehension?
can i get to the answer following my track am i on right track here?
yes know them rarely use though
I don't know how you would handle duplicate letters that way
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
ok i will try with sorted thanks
π
and why?
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
]
||
I don't have anything else to say, the problem is solved
!close
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.