cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 707 Views

Python coding error

I am trying to write a python code  to check if two strings are anagrams of each other or not :

s1= input("enter 1st word or phrase: ")
s2= input("Enter your 2nd word or phrase: ")

s1=s1.lower()
s2=s2.lower()

if len(s1) != len(s2):
    print("Not Anagrams")
else:
     for x in s1:

         if s1.count(x) != s2.count(x):
              print("Not Anagrams")
              break
     else:
       print("Anagrams")

 

This code is working fine for now , for example for strings like : listen ; silent  or medical ;decimal or State;taSte 

but it does not work for strings like : snooze alarms ; alas, no more Z's 

Chetan_Tiwary__0-1756231929765.png

Could you help me fix this code so that it also works for scenarios like this ?

 

Labels (3)
Tags (3)
16 Replies
ric
Flight Engineer Flight Engineer
Flight Engineer
  • 525 Views

Hi, @Chetan_Tiwary_ 

Well, I believe the strings snooze alarms' and alas, no more Z's are not considered anagrams by your Python code, because the second string has a comma after the word "alas" that the second string doesn't have. Do you want to completely ignore any punctuation character (single quote, double quotes, commas, spaces, etc...) in your anagram verification?

Chetan_Tiwary_
Community Manager
Community Manager
  • 523 Views

@ric yes , that will serve my purpose. 

ric
Flight Engineer Flight Engineer
Flight Engineer
  • 510 Views

Thanks for the clarification, @Chetan_Tiwary_ 

Here's a rewritten version of your Python code that ignores non-alphabetic characters (with the help of the web site https://tohtml.com to be able to maintain correct indentation and formatting of the code in this reply of mine):

s1 = input("enter 1st word or phrase: ")
s2 = input("Enter your 2nd word or phrase: ")

s1 = s1.lower()
s2 = s2.lower()

s1 = ''.join([char for char in s1 if char.isalpha()])
s2 = ''.join([char for char in s2 if char.isalpha()])

if len(s1) != len(s2):
    print("Not Anagrams")
else:
     for x in s1:
         if not(x.isalpha()):
            continue

         if ( (s1.count(x) != s2.count(x)) ):
            print("Not Anagrams")
            break
     else:
         print("Anagrams")

 

And here's the execution of the modified Python program for the several examples that you used:

$ python3 anagrams2.py
enter 1st word or phrase: listen
Enter your 2nd word or phrase: silent
Anagrams

$ python3 anagrams2.py
enter 1st word or phrase: medical
Enter your 2nd word or phrase: decimal
Anagrams

$ python3 anagrams2.py
enter 1st word or phrase: State
Enter your 2nd word or phrase: taSte
Anagrams

$ python3 anagrams2.py
enter 1st word or phrase: snooze alarms
Enter your 2nd word or phrase: alas, no more Z's
Anagrams

Chetan_Tiwary_
Community Manager
Community Manager
  • 439 Views

wonderful @ric , this 

''.join([char for char in s1 if char.isalpha()])

 efficiently strips any character that isn't a letter, giving you a clean, alphabet-only string !

kudos! 

TM
Starfighter Starfighter
Starfighter
  • 342 Views

Sorry if I hijack the thread, how to do you type in code like that ?
The toolbow I see just contains: bold, italic, underline, insert/edit link, insert photo and numbered list.

Chetan_Tiwary_
Community Manager
Community Manager
  • 340 Views

@TM while replying , do you see an option insert code </> ? You can use that.

0 Kudos
TM
Starfighter Starfighter
Starfighter
  • 327 Views

No, I do not see it.
Just the 6 I mentionned earlier.

If it matters, I have been lazy and I am still using a Fedora 36 laptop withMozilla Firefox 112.0.1 !

Chetan_Tiwary_
Community Manager
Community Manager
  • 322 Views

@TM weird. yes please try to change the browser and see if it helps. It should come like this :

Chetan_Tiwary__0-1756403976125.png

 

 

0 Kudos
ric
Flight Engineer Flight Engineer
Flight Engineer
  • 226 Views

Hi, @Chetan_Tiwary_ 

I belleve that @TM is right: as far as I can see, we (common users / members) only get the expanded / full toolbar, in the post / message editor, when we start a new discussion.

1 - Screenshot for when we start a new discussion (we get the full toolbar):

ric_0-1756589035534.png


2 - Screenshot for when we reply to an existing discussion
 (we get a toolbar that has only the icons / buttons for Bold, Italic, Underline, Create Hyperlink, Insert Image and Insert Bulleted List):

ric_1-1756589220657.png

 



Join the discussion
You must log in to join this conversation.