How to check multiple occurrences of a character in a string [duplicate] - python

This question already has an answer here:
Identifying straight, flush and other categories (from Poker) using Python
(1 answer)
Closed 3 years ago.
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

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

Related

What does "s" mean inside of a for loop? [duplicate]

This question already has answers here:
What does enumerate() mean?
(7 answers)
Closed 1 year ago.
def capital_indexes(string):
indexes = []
for i, s in enumerate(string):
if s.isupper():
indexes.append(1)
return indexes
What does the s in the for loop mean?
i is the index in the string, while s is a string of length 1 representing the letter at that index.
See documentation for enumerate

What does string[1:-1] means in python? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 3 years ago.
What does this kind of indexing refer to in case of recursion.
def is_palindrome(s):
if len(s) < 1:
return True
else:
if s[0] == s[-1]:
return is_palindrome(s[1:-1])
else:
return False
a=str(input("Enter string:"))
if(is_palindrome(a)==True):
print("String is a palindrome!")
else:
print("String isn't a palindrome!")
If you look in the documentation, this expression is called slicing : Python: Slicing
You can add three arguments in this syntax.
The first is the start of the sequence, the second is the end (not included) and the third is the step.
When you put a negativ arguments it means that you count from the end of your array.
So for :
s = "Hello World"
s = s[1:-1]
You would have :
s = "ello Worl"
For your case it is recursive to go step by step to the center of the string and each time you check if the string is still a palindrome. When you have only one character or less it returns True because everything before was OK to say that your string is a palindrome

How to find a letter in a string in Python [duplicate]

This question already has answers here:
Python: Find a substring in a string and returning the index of the substring
(7 answers)
Closed 3 years ago.
Find out if word 'dog' is in a string.
I tried doing this code and i dont know where the error is .
y='dogaway'
for i in range(len(y)):
if y[i:i+2]=='dog':
x=x+1
print(x)
I expected output to be 1 but the actual ouput is 0.
You can use count.
y = 'dogaway'
print(y.count('dog')) # Outputs 1
or if you want to fix your code, you are just off by one in your splice:
y = 'dogaway'
x = 0
for i in range(len(y) - 3): # Make sure your range accounts for the look ahead
# In the future add a print to make sure it is doing what you expect:
# print(y[i:i + 2])
if y[i:i + 3] == 'dog': # Here you were off by 1
x = x + 1
print(x)
Even simpler:
if 'dog' in y:
...
You can use the in membership operator in Python.
'dog' in 'dogaway'
returns True

What is the difference between the following two python methods? [duplicate]

This question already has answers here:
List index out of range error on simple python for loop
(3 answers)
Accessing the index in 'for' loops
(26 answers)
Closed 4 years ago.
What is the difference between these 2 fragments of python codes?
I want to check whether an array contains the integers 1,2,3 in sequence as its elements?
def arrayCheck(nums):
for i in nums:
if(i <= (len(nums)-3)):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
def arrayCheck(nums):
for i in range(0,len(nums)-2):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
The first one gives wrong answer for the array:
arrayCheck([1,1,2,1,1,1,1,2,3])
but the second one is correct.
The first block i is elements of the parameter. The second, it's only their indices.
If you wanted the first to iterate over indices rather than the elements, use this
def arrayCheck(nums):
for i, _ in enumerate(nums):
if i <= len(nums)-3:
You can also use list slicing, by the way
if nums[i:i+2] == [1,2,3]:
return True

Changing a collection.Counter during enumeration? [duplicate]

This question already has answers here:
How does a for loop evaluate its argument
(3 answers)
Closed 6 years ago.
counter = Counter()
// fill data into counter
for a, b in counter.most_common():
if (b > 1):
counter[a] = np.log(b)
else:
counter[a] = -np.log((1 / (b+0.01)))
As I see, this is safe, based on my trial. No bad thing happens when I change the collection while I am enumerating it. In other languages, in each cycle of the for, the counter.most_common() value is evaluated.
Doesn't this happen in Python as well?
No, it doesn't. A more illustrative example:
def example():
print("Ping")
return [1,2,3,4]
for x in example():
print(x)
Output:
Ping
1
2
3
4

Categories

Resources