0

I got stuck on a homework problem where I need to write a function that takes in one parameter and returns True if there are exactly three 7's in a string. If there are less than three or more than three '7's, the function should return False.

def lucky_seven(a_string):
    if "7" in a_string:
       return True 
    else:
       return False

 print(lucky_seven("happy777bday"))
 print(lucky_seven("happy77bday"))
 print(lucky_seven("h7app7ybd7ay"))      

The result should be True, False, True. I got I to work where only one 7 is in a string. Appreciate if anyone can point me into the right direction.

3
  • 1
    have a look at this question stackoverflow.com/questions/1155617/… Commented Jul 21, 2018 at 21:59
  • as an aside, your if x: return True else: return False can be simplified to return bool(x) or even return x if x is a boolean Commented Jul 21, 2018 at 22:02
  • Does it have to be in a loop? Commented Jul 21, 2018 at 22:05

4 Answers 4

3

you can use str.count:

str.count(sub[, start[, end]])

Return the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

def lucky_seven(a_string):
    if a_string.count("7") == 3:
       return True 
    else:
       return False

print(lucky_seven("happy777bday"))
print(lucky_seven("happy77bday"))
print(lucky_seven("h7app7ybd7ay"))  
Sign up to request clarification or add additional context in comments.

2 Comments

Please read the op's question correctly, didn't you see which he sais If there are less than three or more than three '7's, the function should return False.
You could shorten that to return a_string.count("7") == 3, too, since this expression will always return a bool anyway.
1

You an do it by using regexp easily:-

import re

def lucky_seven(text):
    return len(re.findall("7", text)) is 3

print(lucky_seven("happy777bday"))
print(lucky_seven("happy77bday"))
print(lucky_seven("h7app7ybd7ay"))

2 Comments

I used timeit on your regexp version and again on str.count: regexps are about 6 times slower. Moral: prefer plain ol' string methods when reasonable.
When i say easily i don't mean soon i mean by using a short code and also you can use str.count function if you want, and i think it needs fewer code than mine, but i just suggested you to use re because i didn't remember using str.count is easily, and i didn't counted time to get the result i just found something to get the result, and now you can use what you want and thanks for your comment i take a reserch of my answer.
0

"7" in a_string is True iff there are any 7's in a_string; to check for a certain number of 7s, you could compare each character of a_string to 7 and count how many times they match. You could also do this with a regex.

1 Comment

though regex would be overkill here
0

You could just create your own counter like -

def lucky_seven(a_string):
    count = 0
    for s in a_string:
        if s == "7":
            count += 1
    return True if count == 3 else False

Or you could use python's collections module, which will return a dict of how many times an element has appeared in a list. Since a string is nothing but a list, it will work -

import collections
def lucky_seven(a_string):
    return True if collections.Counter("happy777bday")["7"] == 3 else False

Or a string counter-

def lucky_seven(a_string):
    return True if str.count(a_string, "7") == 3 else False

Or even a list comprehension solution, though really not needed -

def lucky_seven(a_string):
    return True if len([i for i in a_string if i == "7"]) == 3 else False

1 Comment

Please read OPs question fully. There need to be exactly three '7's in the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.