Python | Isogram Problem!

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!


图片[1]-Python | Isogram Problem! - 拾光赋-拾光赋
SOLVE IT!


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.

Enter fullscreen mode Exit fullscreen mode


Let’s break down the problem to the 3 steps:

  1. Making a function
  2. We need to use if statement to count if there’s more than one item in our string!
  3. we need to loop over if statement to check every item in our string!.

1.Making a function:

  def isogram(string):
       pass

Enter fullscreen mode Exit fullscreen mode

2.if statement to count if there’s one more item or not:

if string.count(item) > 1 :
            return False

Enter fullscreen mode Exit fullscreen mode

3.for loop over if statement to check every item in given string:

for item in upper_string:

Enter fullscreen mode Exit fullscreen mode

*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

Enter fullscreen mode Exit fullscreen mode

So we have a function:

def isogram(string):

    for item in string:
        if string.count(item) > 1 :
            return False
    return True

Enter fullscreen mode Exit fullscreen mode

If we test our program with a string like this:

abc = isogram("A B C")

Enter fullscreen mode Exit fullscreen mode

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 = "-"

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Let’s test our program with another string:

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

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

原文链接:Python | Isogram Problem!

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容