Why is count() method giving strange answer? - python-3.x

s = "ANANAS"
print(s.count("ANA"))
print(s.count("AN"))
print(s.count("A"))
"ANA" occurs two times in "ANANAS" but python prints 1 whereas
"AN" occurs two times and python prints 2. "A" occurs three times and python prints 3 as output. Why is this strange behaviour?

Straight from the documentation:
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.
The two occurences of "ANA" in "ANANAS" are overlapping, hence s.count("ANA") only returns 1.

This is because in your sub string ANA will be only counted twice if it's something like "testANAANAAN " I.e two full occurrences of ANA .
As, in your case if it already checked first full substring it will not use that string part again from full string and will look for matching substring in rest of string.

Related

what will be the dp and transitions in this problem

Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n.
Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets ax points for erasing substring of length x.
Vasya wants to maximize his total points, so help him with this!
https://codeforces.com/problemset/problem/1107/E
i was trying to get my head around the editorial,but couldn't understand it... can anyone tell an easy way to do it?
input:
7
1101001
3 4 9 100 1 2 3
output:
109
Explanation
the optimal sequence of erasings is: 1101001 → 111001 → 11101 → 1111 → ∅.
Here, we consider removing prefixes instead of substrings. Why?
We try to remove a consecutive prefix of a particular state which is actually a substring in the main string. So, our DP states will be start index, end index, prefix length.
Let's consider an example str = "1010110". Here, initially start=0, end=7, and prefix=1(the first '1' will be the only prefix now). we iterate over all the indices in the current state except the starting index and check if str[i]==str[start]. Here, for example, str[4]==str[0]. Now we divide the string into "010" with prefix=1(010) && "110" with prefix=2(1010110). These two are now two individual subproblems. So, when there remains a string with length 1, we return aprefix.
Here is my code.

Checking if a list starts / ends with any one of many strings in a second list

In excel, what's the best way to check if a list of strings in a column start or end with another list of strings?
Example:
First List:
Reddy
CodeRed
Zabby
KaBlueY
Second List: Red, Blue, Blop, Blurp
The solution should return:
Reddy - TRUE (because it contains 'red' from the second list in the start or end position)
CodeRed - TRUE (because it contains 'red' from the second list in the start or end position)
Zabby - FALSE (because it does not contain any strings from the second list in the start or end position.
KaBlueY - FALSE (because it does not contain any strings from the second list in the start or end position.
Edited Answer:
The question has been changed, and so should my answer to make it current:
If the second argument of SEARCH function is a range, you may use this formula.
Solution #4: Check if any of the strings in a range contain a substring
=OR(ISNUMBER(SEARCH("red",range)))*1
Result: 1
=OR(ISNUMBER(SEARCH("redX",range)))*1
Result: 0
Same explanation as Solution #1 below but instead of searching for a substring in a "parent" string, it searches multiple strings and is made possible by creating an array formula and can only be done by pressing CTRL+SHIFT+ENTER. Check for {} around your formula in the formula bar to make sure that you created an array formula.
Since there are multiple results in an array formula, wrap it with OR function to see if any of the strings in a range contain the string that you're looking for. Finally, simply multiply it by 1 to convert the resulting boolean values to its numerical values.
Hope it helps!
Original Answer:
There are number of ways to do this. You may use SEARCH or SUBSTITUTE function, to parse the string, in combination with other functions such as those that returns boolean values, to check the expected result against the return value of the former. Finally, to convert boolean values into its numerical values, simply multiply it by 1.
Here are some examples to get you started:
Solution #1
=ISNUMBER(SEARCH("red","reddy"))*1
Result: 1
=ISNUMBER(SEARCH("redX","reddy"))*1
Result: 0
If it is able to find the substring red within the "parent" string reddy, the SEARCH function returns a number—the position of the substring you're looking for. Otherwise, like in the case of redX, it returns #VALUE!. To hide the ugly #VALUE! error message as well as to show a more appropriate message than simply showing the position of the substring, wrap it with ISNUMBER function to return TRUE or FALSE. And if you'd like to convert it to its numerical values, multiply it by 1.
Solution #2
=IF(SUBSTITUTE("reddy","red","anyText")="reddy",FALSE,TRUE)*1
Result: 1
=IF(SUBSTITUTE("reddy","redX","anyText")="reddy",FALSE,TRUE)*1
Result: 0
Here, the resulting string from the SUBSTITUTE function is compared against the "parent" string by wrapping it with IF function, which in turn returns a boolean value that can be converted into a numerical value by multiplying it by 1.
Solution #3
=NOT(EXACT("reddy",SUBSTITUTE("reddy","red","anyText")))*1
Result: 1
=NOT(EXACT("reddy",SUBSTITUTE("reddy","redX","anyText")))*1
Result: 0
This is just a variation of the formula presented in Solution #2. It uses EXACT function to check if the resulting string from the SUBSTITUTE function is exactly the same as the old string (which I called the "parent string" in Solution #1). If it is exactly the same, it means nothing were substituted because it didn't find the string you're looking for. Since EXACT function returns TRUE if the two strings are an exact match, which means nothing has changed, which also means, it didn't find the string you're looking for, you need to reverse the result by wrapping it with NOT function. Again, if you'd like to convert it to its numerical form, simply multiply it by 1.
Hope it helps!
References:
SEARCH
ISNUMBER
OR
SUBSTITUTE
IF
EXACT
NOT

How to count occurrence of substring elements in string in python (even when repeated)?

I tried the following code
s = "BANANA"
print(s.count('ANA'))
>> 1
The output was 1. It did not consider A which was previously counted.
I want the output to be 2 since 'ANA' occurs twice. Ia their a function I don't know about.

Regular expressions and automata

I'm studying Regular Expressions by reading Aho's book. I don't understand two of the statements in the book:
Question A:
1(0+1)*1 + 1 : denotes the set of all strings beginning and ending with a 1.
My question why is +1 added at end of the regular expression? Shouldn't 1(0+1)*1 be sufficient?
I'm also having trouble with the following:
Question B:
The set of strings containing only 0's and 1's that have atmost one 1 as below
0*+0*10*
Can you explain how the solution 0*+0*10* is arrived at, step by step?
As to question a: 1(0+1)*1 does not match the one-character string 1, which begins and ends with 1. One needs a special case for it, which the example does.
As to question b: I cannot speak for the author. However... Any string that contains at most one 1 is a string that either has no 1s or has exactly one 1. Assuming that the alphabet is {0,1}, the former means any string that contains zero or more 0s, that is, 0*. The latter, with the same assumption, means any string that contains zero or more 0s followed by one 1 followed by zero or mpre 0s, that is, 0*10*. Combining these yields the example.
For Question a: 1(0+1)*1 denotes set of all strings beginning and ending with one but does not contain string 1 which has length one and starts and ends with one.
For Question b:
Set of strings containing atmost one 1 = A + B where
A is set of all strings containing zero 1s and
B is the set of all strings containing exactly one 1
So A is 0* and B is 0*10*
Hence we get the answer as 0* + 0*10*
For the first example, the string that is accepted by the + 1 but not by the rest is 1. The rest of the expressions can handle 11, but not a string where the first and last character are the same.
It's similar reasoning for the second string - 0* handles strings of all zeroes, 0*10* handles strings of 1 one.

How to obtain the n-th letter of the alphabet

Is there a short formula to get the n-th letter of the alphabet?
For example, if I give parameter 5 to the function, I would get the letter e.
There is a function CHAR which gives a character with the specified code:
CHAR(96 + 5)
will yield your "e".
But there is no direct way to get a character of the alphabet.
An alternate, although not as short as the CHAR function, is the CHOOSE function
=CHOOSE(5,"a","b","c","d","e","f","g","h","I","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x","y","z")
The index number '5' returns the fifth value in the list. The list could be an Excel range of data e.g. (A1:A26).
If the index number is outside the range, #VALUE! is returned
you could use an ascii function since every letter has a numeric value in ascii
Not sure what language your using... in T-SQL you can use an ASCII and CHAR functions:
PRINT CHAR(ASCII('A') + #i) -- where #i is your numeric value
There is also another simpler way: CHAR(CODE("A")+TRUNC(RAND()*26)).
This gives you the position of the letter in question (C3, for example) if it is capital or not.
=IF(AND(CODE(C3)>=65,CODE(C3)<=90),CODE(C3)-64,IF(AND(CODE(C3)>=97,CODE(C3)<=122),CODE(C3)-96,"Error"))

Resources