Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:12:00+00:00 2026-05-28T05:12:00+00:00

I have written an anagram solving algorithm, which does not work. for word in

  • 0

I have written an anagram solving algorithm, which does not work.

for word in wordlist: #Checking for equal length
    if sorted(word.replace("\n", "")) == sorted(anagram):
        possible.append(word)

I needed to use len(word) – 1 to take away the \n.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:12:01+00:00Added an answer on May 28, 2026 at 5:12 am

    (1) I don’t understand the “-1” in “len(word)-1” in your first loop.

    (2) Your second loop has several problems:

    It doesn’t check to see whether the letters are the same, it checks to see whether each letter in the anagram is in the word. You’re not using the count information, so you can’t distinguish between bok and book. You’re also removing from a sequence you’re iterating over, which leads to unexpected behaviours.

    For my part I’d simply use

    sorted_anagram = sorted(anagram)
    possibles = [word for word in wordlist if sorted(word) == sorted_anagram]
    

    rather than explicit for loops.

    Note that sorting the words is a kind of canonicalization process — it makes sure that any two words which are anagrams of each other will be in the same format. Another approach to determine whether two things are anagrams would be to make sure that the letter counts are the same:

    >>> from collections import Counter
    >>> Counter('book')
    Counter({'o': 2, 'k': 1, 'b': 1})
    >>> Counter('obko')
    Counter({'o': 2, 'k': 1, 'b': 1})
    >>> Counter('bok')
    Counter({'k': 1, 'b': 1, 'o': 1})
    >>> 
    >>> Counter('book') == Counter('boko')
    True
    >>> Counter('book') == Counter('bok')
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a ruby script which opens up dlink admin page in firefox
I have written an AppleScript which when supplied with a Windows network link, will
I have written a DLL that uses MS Word to spell check the content
have written a stochastic simulation in Java, which loads data from a few CSV
I have written a app in PHP on my localhost server using XAMP, which
I have written an application which is working fine in Emulator . But when
I have written a greasemonkey script which will do GM_xmlhttpRequest to the next 2
I have written this class. This is not complete description of original class to
I have written a small program which uses function pointers to do some numerical
I have written following regex But its not working. Can you please help me?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.