1

If there is a string = '99888' it should print 'True'. How can you check pairs and threes of a character in a string

I tried using the count function but it only identifies a pair

string = '99888'
for c in '123456789':
    if string.count(c) == 2 and string.count(c) == 3 :
       print('True')

Edit: The string is a always 5 character string and if there is a pair and three of a kind it prints True For example '89899' and '75757' print True. '98726' prints False

Peri
  • 101
  • 1
  • 8
  • 1
    Count cannot be at the same time 2 and 3. – Austin Oct 18 '19 at 01:52
  • any case where your code should print false, what would be output for 234423312356579 ( i write this number arbitarily ) – sahasrara62 Oct 18 '19 at 02:06
  • 1
    @Austin unless it's a Schrodinger's character. – slider Oct 18 '19 at 02:15
  • 2
    I **think** what you mean is, you want the string to contain exactly three of some character, and *also* contain exactly two of a *different* character. Is that correct? Or do the pairs or triplets of digits need to be next to each other; can there be more than 2 or 3 of them, etc. etc. etc. To solve a problem first requires a *precise* description of the problem. – Karl Knechtel Oct 18 '19 at 02:30
  • This question is extremely unclear. Multiple occurrences of which character? How many? Do the occurrences need to be consecutive? – Turksarama Oct 18 '19 at 03:31
  • sorry I should have added this string is a 5 character string and if there is a pair and three of a kind so '88999' it prints true. '98726' prints false – Peri Oct 18 '19 at 04:32
  • If you search in your browser for "Python poker", you'll find references that can explain this much better than we can manage here. – Prune Oct 18 '19 at 19:47

1 Answers1

3
  • Use the Counter class from the collections module
from collections import Counter

def check(inputString):
    x = Counter(inputString)
    list_of_counts = [x[i] for i in x.elements()]

    if (2 in list_of_counts) and (3 in list_of_counts):
        return(True)
    else:
        return(False)

print(check("99888"))
print(check("999888"))
Waqar Bin Kalim
  • 321
  • 1
  • 7