Hey everone, I wanna share my last challenge that I solved, it took me around 2 days!
I know it could be easy for some of you even for beginners but it doesn't matter for me cause my goal is improving my coding skill and problem solving every single day.
Long story short!
Let's dive to the Isogram Challenge!
Problem:
Determine if a word or phrase is an isogram.
An isogram (also known as a "nonpattern word"),
is a word or phrase without a repeating letter,
however spaces and hyphens are allowed to appear
multiple times.
Examples of isograms:
- lumberjacks
- background
- downstream
- six-year-old
The word **isograms**,
is not an isogram, because the s repeats.
Let's break down the problem to the 3 steps:
- Making a function
- We need to use if statement to count if there's more than one item in our string!
- we need to loop over if statement to check every item in our string!.
1.Making a function:
def isogram(string):
pass
2.if statement to count if there's one more item or not:
if string.count(item) > 1 :
return False
3.for loop over if statement to check every item in given string:
for item in upper_string:
*after this steps we need to combine number 2 and number 3, like this:
for item in upper_string:
if upper_string.count(item) > 1 :
return False
So we have a function:
def isogram(string):
for item in string:
if string.count(item) > 1 :
return False
return True
If we test our program with a string like this:
abc = isogram("A B C")
It will return False !
Cause our program count spaces so we have two spaces in "A B C" string!
If your read the Problem part carefully it says:
spaces and hyphens are allowed to appear
multiple times!
So:
spaces = " "
hyphens = "-"
We need another step to complete our solution which is:
.
.
.
4.writing a __if statement_ for spaces and hyphens to return True if there's more than once_:
spaces = " "
hyphens = "-"
if string.count(spaces)>1 or string.count(hyphens)>1:
return True
and our string() function will be look like this:
def isogram(string):
for item in string:
spaces = " "
hyphens = "-"
if string.count(item) > 1 :
return False
elif string.count(spaces) > 1 or string.count(hyphens) > 1:
return True
else:
pass
return True
Let's test our program with another string:
abc = isogram("A a B C")
print(abc)
Our program will return True!!!
But we have two letter "A" and "a"!
That's obvious cause there is a difference between Capital letters and small letter in every programming language!
and if you try :
print(A is a)
the answer would be False.
So in our program we can make all letters Upper-Case(upper()) or Lower-Case(lower()), it's totally up to you, I prefer upper!
def is_isogram(string):
upper_string = string.upper()
for item in upper_string:
spaces = " "
hyphens = "-"
if upper_string.count(item) > 1 :
return False
elif string.count(spaces) > 1 or string.count(hyphens) > 1:
return True
else:
pass
return True
I need to say thank you my brother Amir, cause you're helping me a lot in this way.
Finally we completed this challenge, if you have any suggestion to improve this solution and write more Pythonic, cleaner and more readable just let me know and leave a comment below my post, I'll appreciate it.
Thank you so much for reading my post.
Keep Moving Forward γ
Code with π
π π π π π
Top comments (10)
Hey Vidit Sarkar, tnx for sharing your amazing solution with us, I have problem to understand :
could ya explaing me why we need to write return with
len(set()) == len()
?
actually let me know what's happening behind the scene.
Happy Coding ;)
Let's take the example ,
s = "Six-Year- Old"
.replace('-', '')
,replace(' ', '')
will going to replace all-
's and spaces with empty character respectively.lower()
will lowercase all charecters.So,
cleaned
will be,cleaned = 'sixyearold'
.Now,
set(cleaned)
stores all unique characters ofcleaned
. So, if count of all unique characters is same as length ofcleaned
then all characters ofcleaned
are unique.len(set(cleaned)) == len(cleaned)
will beTrue
if all characters ofcleaned
are unique, elseFalse
.Tnx Vidit Sarkar for your amazing explanation .
I appreciate that.
Keep Moving Forward γ
Code with π
π π π π π
As I understand that code,
sets
only accept unique characters, and since he cleaned out thehyphens
andspaces
if thelen
of theset(cleaned)
isn't thelen
ofcleaned
that means we have a duplicate character, hence it's not an isogram. π€Please correct me if I'm wrong. π
Yes, you are right!
Fun thing, the same code works in Javascript too with some small modifications.
Hi Banji, you've done a nice work on this challenge, congratulations :)
Here's my approach:
My considerations:
Counter
when i need to check if something repeatsregex
to substitute hypens and spaces at once instead of using the methodreplace
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: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 themost_common
letter form the input.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:
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 π
π π π π π
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.