DEV Community

Discussion on: Python | Isogram Problem!

Collapse
 
rafaacioly profile image
Rafael Acioly • Edited

Hi Banji, you've done a nice work on this challenge, congratulations :)

Here's my approach:

from collections import Counter
import re

def is_isogram(text: str) -> bool:
  content = re.sub(r"-|\s+", "", text, flags=re.UNICODE)
  letter_counter = Counter(content)

  letter, amount = letter_counter.most_common(1)[0]

  return amount == 1

My considerations:

  • I always use the class Counter when i need to check if something repeats
  • Before starts to work on the input i always normalized it first
  • In this case i prefer to use regex to substitute hypens and spaces at once instead of using the method replace twice, like this: text.replace("-", "").replace("-", "")

A few tips:

Every time i need/want to do a challenge that need loop or repetitive tasks i always ask myself:

Can i use some of the standard libraries of python to solve this?

Then i check the itertools, and the collections packages.

The big brain move on my approach was the use of Counter class, this class have a lot useful methods such as subtract, add, and get the most_common letter form the input.

Collapse
 
banji220 profile image
Banji • Edited

Heyyyyyyyyy Rafael ;)
Thank you for sharing your code and your great approach in my Post.
I appreciate it
But if you test your program with a string like:

abc = is_isogram("A a B C)
print(abc)

the program will

__return : True__

but we need to get False cuz there's to letter (A, a).

Finally your comment was so helpful for me cuz I think I really need to change some of my approach in my coding routine like using standard libraries more and etc.
Thanks for giving a light in my jourrney.

Keep Moving Forward

Code with 💛

🅑🅐🅝🅙🅘

Collapse
 
rafaacioly profile image
Rafael Acioly

I didn't see this case, we can fix this normalizing even more the input with .lower() and it will work just fine.

I'm glad to help.