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.

  • 1
    have a look at this question https://stackoverflow.com/questions/1155617/count-the-number-occurrences-of-a-character-in-a-string – joel Jul 21 '18 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 – joel Jul 21 '18 at 22:02
  • Does it have to be in a loop? – pault Jul 21 '18 at 22:05

4 Answers4

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"))  
jshamble
  • 391
  • 2
  • 14
  • 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**. – Azhy Jul 21 '18 at 22:14
  • 1
    You could shorten that to `return a_string.count("7") == 3`, too, since this expression will always return a bool anyway. – Kirk Strauser Jul 21 '18 at 22:30
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"))
Azhy
  • 704
  • 3
  • 16
  • 1
    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. – Kirk Strauser Jul 21 '18 at 22:55
  • 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. – Azhy Jul 21 '18 at 23:05
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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
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
Sushant
  • 3,499
  • 3
  • 17
  • 34
  • Please read OPs question fully. There need to be *exactly* three `'7'`s in the string. – Joel Jul 21 '18 at 23:29