I am trying to do the function count without using count() it is working and everything okay but when I try to search two letters in one word its returning 0. When I try to search 1 word in a letter its working normal.
def count(str, sub):
found = 0
for key in str:
if key == sub:
found += 1
return found
str = input("Enter a string: ") #or we can initialize a string
sub = input("Enter a substring: ") #or we can initialize a substring
count(str, sub)
print ("letter: ", sub)
print ("count: ", count(str, sub))
Following your method I suggest you do something like this:
def count(string, sub):
found = 0
size = len(sub)
for i in range(len(string) + 1 - size):
if string[i:i+size] == sub:
found += 1
return found
That way you can use it for any size of sub.
def count(string, sub):
found = 0
for c in range(len(string)):
start = c
end = -(len(string)-len(sub)-c) if -(len(string)-len(sub)-c) != 0 else None
if string[start:end] == sub:
found += 1
return found
string = input("Enter a string: ") #or we can initialize a string
sub = input("Enter a substring: ") #or we can initialize a substring
print ("letter: ", sub)
print ("count: ", count(string, sub))
Below should work for all length sub strings.
def count(str, sub):
found = 0
for i in range(1,len(str)+1): #iterate for all length substrings
for j in range(len(str)-i+1): #iterate for different starting positions of substrings.
key = str[j:j+i]
if key == sub:
found += 1
return found
Related
I'm trying to make it so that my text alternates between upper and lower case like the question ask. It seems to skip 3 in the indexing and I can't figure out why.
sentence = input("Write a sentence")
newList = []
for i in range(len(sentence)):
if sentence[i] != " ":
newList.append(sentence[i])
listJoint = "".join(newList)
newList2 = []
for i in range(len(listJoint)):
if (listJoint.index(listJoint[i]) % 2) == 0:
print(listJoint.index(listJoint[i]))
newList2.append(listJoint[i].upper())
elif (listJoint.index(listJoint[i]) % 2) != 0:
print(listJoint.index(listJoint[i]))
newList2.append(listJoint[i].lower())
print(newList2)
#newListJoint = "".join(newList2)
#print(newListJoint[::-1])
Thanks in advance
List index doesn't go 0 1 2 3 4
The function .index() finds the first occurrence of that letter. 'L' occurs at index 2 and 3 so it would return 2 for both L's.
Iterate through each character of the string and alternate upper/lower methods.
sentence = "Hello"
alternated_sentence = ''
for i, char in enumerate(sentence):
if i % 2:
alternated_sentence += char.upper()
else:
alternated_sentence += char.lower()
print(alternated_sentence)
#hElLo
sentence = input("Write a sentence:")
# Remove spaces (as per your question)
sentence = sentence.replace(' ', '')
# Reverse the string order (as per your question)
sentence = sentence[::-1]
result = []
for i in range(len(sentence)):
if(i%2==1):
result.append(sentence[i].lower())
else:
result.append(sentence[i].upper())
print(''.join(result))
Here's the solution. The above code would give output as follows:
Write a sentence: Hello world
DlRoWoLlEh
I never realised index method referenced the first instance of the character. This works:
sentence = input("Write a sentence")
newList = []
for i in range(len(sentence)):
if sentence[i] != " ":
newList.append(sentence[i])
listJoint = "".join(newList)
newList2 = []
for i, value in enumerate(newList):
if i % 2 == 0:
newList2.append(listJoint[i].upper())
elif i % 2 !=0:
newList2.append(listJoint[i].lower())
newListJoint = "".join(newList2)
print(newListJoint[::-1])
I want to convert my sequence from format(AAAABBCCdd) to (A4B2C2d2) and it is case sensitive.
This is my code but it doesn't return the number for last letter.
I appreciate it if you help me fix my mistake.
#[AAABBBCCCDDDDDD] -> [A3B3C3D]
def compressStToNum(A):
Ans = [A[0]]
count = 0
for letter in A:
if letter in Ans:
count += 1
else:
Ans = Ans + [count]
Ans = Ans + [letter]
count = 1
list_of_strings = [str(s) for s in Ans]
joined_string = "".join(list_of_strings)
print(joined_string)
print(compressStToNum("AAABBBBCCCSSS"))
This works for me:
#[AAABBBCCCDDDDDD] -> [A3B3C3D5]
def compressStToNum(sequence):
previous_letter = sequence[0]
compressed_sequence = ''
count = 1
for letter in sequence[1:]+' ':
if letter == previous_letter:
count += 1
continue
compressed_sequence += previous_letter + str(count)
previous_letter = letter
count = 1
return compressed_sequence
print(compressStToNum("AAABBBBCCCSSS"))
Another way using Counter in sorter version,
from collections import Counter
def compressStToNum(input):
res = Counter(input)
res = sorted(res.items())
res = ''.join([f"{tups[0]}{tups[1]}" for tups in res])
return res
print(compressStToNum("AAABBBBCCCSSS"))
Here's a small change to fix that problem in your code but this code has the problem of not dealing with empty strings. There are much better ways to write this.
# [AAABBBCCCDDDDDD] -> [A3B3C3D] def compressStToNum(A):
def compressStToNum(A):
Ans = [A[0]]
count = 0
for letter in A:
if letter in Ans:
count += 1
else:
Ans = Ans + [count]
Ans = Ans + [letter]
count = 1
if count > 0:
Ans = Ans + [count]
list_of_strings = [str(s) for s in Ans]
joined_string = "".join(list_of_strings)
print(f"'{A}' -> '{joined_string}'")
return joined_string
compressStToNum("AAABBBBCCCSSS")
compressStToNum("AAABBBBCCCS")
compressStToNum("ABC")
compressStToNum("ABBBBS")
compressStToNum("S")
compressStToNum("")
Produces this output:
IndexError: string index out of range
'AAABBBBCCCSSS' -> 'A3B4C3S3'
'AAABBBBCCCS' -> 'A3B4C3S1'
'ABC' -> 'A1B1C1'
'ABBBBS' -> 'A1B4S1'
'S' -> 'S1'
A better version:
def compress_str_to_num(string_to_compress):
cur_letter = None
cur_count = 0
answer = ""
for letter in string_to_compress:
if letter is cur_letter:
cur_count += 1
else:
if cur_count > 0:
answer += f"{cur_letter}{cur_count}"
cur_letter = letter
cur_count = 1
if cur_count > 0:
answer += f"{cur_letter}{cur_count}"
print(f"'{string_to_compress}' -> '{answer}'")
return answer
compress_str_to_num("AAABBBBCCC")
compress_str_to_num("ABC")
compress_str_to_num("ABBBBS")
compress_str_to_num("S")
# These are an issue in the orginal code:
compress_str_to_num("")
compress_str_to_num("AAABBBBCCCSSSAAAA")
Produces this output:
'AAABBBBCCCS' -> 'A3B4C3S1'
'ABC' -> 'A1B1C1'
'ABBBBS' -> 'A1B4S1'
'S' -> 'S1'
'' -> ''
'AAABBBBCCCSSSAAAA' -> 'A3B4C3S3A4'
There are two issues in your code:
you only add the previous count when you reach a new letter but not at the end of the loop.
you are comparing new letters to all preceding letters instead of just the last one which will count repeated letters as par of the current one (e.g. "AAABBAAACC" -> "A3B5C")
You could fix these issues, or you could use the zip function to obtain a list of indexes where letters are changing and then use zip again to combine those indexes into repetition numbers of the initial letter:
def compressIt(S):
breaks = [0]+[i for (i,c0),c1 in zip(enumerate(S,1),S[1:]) if c0 != c1]+[len(S)]
return "".join(S[s]+str(e-s) for s,e in zip(breaks,breaks[1:]))
compressIt("AAABBBCCCDDDDDD") # 'A3B3C3D6'
My goal is to write a function which change every even letter into upper letter and odd to lower (space also count as a one element).
This is my code
def to_weird_case(s):
for i in s:
if len(i) % 2 == 0:
s[i] = i.upper() + s(i+1)
else:
s[i] = i.lower() + s(i+2)
return i
I think it should be quite correct, but it gives me error.
line 7, in to_weird_case
s[i] = i.lower() + s(str(i)+2)
TypeError: must be str, not int
EDIT:
I have a sugesstion but I don't know how to make it. I try it for myself and back here.
This needs to definitly explicietly state that the zero indexing uppercase is for each word.
Do you know guys how to make it?
So we can analyze your code and just explain what you typed:
def to_weird_case(s):
for i in s: # s is your string, and i is the actual character
if len(i) % 2 == 0: # if your length of the character can be divided by 2. Hmm this is weird
s[i] = i.upper() + s(i+1) # s[i] change a character in the string but you should provide an index (i) so an integer and not a character. But this is not supported in Python.
else:
s[i] = i.lower() + s(i+2)
return i # This will exit after first iteraction, so to_weird_case("this") will return "t".
So what you need to is first create a output string and fill that. And when iteration over s, you want the index of the char and the char value itself.
def to_weird_case(s):
output = ""
for i, myChar in enumerate(s):
if i % 2 == 0:
output += myChar.upper()
else:
output += myChar.lower()
return output
my_sentence = "abcdef"
print(to_weird_case(my_sentence))
And when you want to ignore spaces, you need to keep track of actual characters (excluding spaces)
def to_weird_case(s):
output = ""
count = 0
for myChar in s:
if myChar.isspace():
output += myChar
else:
if count % 2 == 0:
output += myChar.upper()
else:
output += myChar.lower()
count += 1
return output
my_sentence = "abc def"
print(to_weird_case(my_sentence))
Test this yourself
def to_weird_case(s):
for i in s:
print (i)
After doing this you will find that i gives you characters.
if len(i) % 2 == 0:
This line is incorrect as you are trying to find the length of a single character. len(s) would be much better.
So the code will be like
def to_weird_case(s):
s2 = "" #We create another string as strings are immutable in python
for i in range(len(s)):
if i % 2 == 0:
s2 = s2 + s[i].upper()
else:
s2 = s2 + s[i].lower()
return s2
From #RvdK analysis, you'ld have seen where corrections are needed. In addition to what has been pointed out, I want you to note that s[i] will work fine only if i is an integer, but in your case where (by assumption) i is a string you'll encounter several TypeErrors. From my understanding of what you want to do, it should go this way:
def to_weird_case(s):
for i in s:
if s.index(i) % 2 == 0:
s[s.index(i)] = i.upper() + s[s.index(i)]
elif s.index(i) % 2 == 1:
s[s.index(i)] = i.lower() + s[s.index(i)]
return i # or possibly return s
It is possible to do in a single line using a list comprehension
def funny_case(s):
return "".join([c.upper() if idx%2==0 else c.lower() for idx,c in enumerate(s)])
If you want to treat each word separately then you can split it up in to a list of words and "funny case" each word individually, see below code
original = "hello world"
def funny_case(s):
return "".join([c.upper() if idx%2==0 else c.lower() for idx,c in enumerate(s) ])
def funny_case_by_word(s):
return " ".join((funny_case(word) for word in s.split()))
print(funny_case_by_word(original))
Corrected code is as follows
def case(s):
txt=''
for i in range(len(s)):
if i%2==0:
txt+=s[i].upper()
else:
txt+=s[i].lower()
return txt
String assignment gives error in Python therefore i recommend considering my approach
When looping over elements of s, you get the letter itself, not its index. You can use enumerate to get both index and letter.
def to_weird_case(s):
result = ''
for index, letter in enumerate(s):
if index % 2 == 0:
result += letter.upper()
else:
result += letter.lower()
return result
correct code:
def to_weird_case(s):
str2 = ""
s.split() # through splitting string is converted to list as it is easy to traverse through list
for i in range(0,len(s)):
n = s[i] # storing value in n
if(i % 2 == 0):
str2 = str2 + n.upper()
else:
str2 = str2 + n.lower()
return str2
str1 = "hello world"
r = to_weird_case(str1)
print(r)
I have this code that I found on another topic, but it sorts the substring by contiguous characters and not by alphabetical order. How do I correct it for alphabetical order? It prints out lk, and I want to print ccl. Thanks
ps: I'm a beginner in python
s = 'cyqfjhcclkbxpbojgkar'
from itertools import count
def long_alphabet(input_string):
maxsubstr = input_string[0:0] # empty slice (to accept subclasses of str)
for start in range(len(input_string)): # O(n)
for end in count(start + len(maxsubstr) + 1): # O(m)
substr = input_string[start:end] # O(m)
if len(set(substr)) != (end - start): # found duplicates or EOS
break
if (ord(max(sorted(substr))) - ord(min(sorted(substr))) + 1) == len(substr):
maxsubstr = substr
return maxsubstr
bla = (long_alphabet(s))
print "Longest substring in alphabetical order is: %s" %bla
s = 'cyqfjhcclkbxpbojgkar'
r = ''
c = ''
for char in s:
if (c == ''):
c = char
elif (c[-1] <= char):
c += char
elif (c[-1] > char):
if (len(r) < len(c)):
r = c
c = char
else:
c = char
if (len(c) > len(r)):
r = c
print(r)
Try changing this:
if len(set(substr)) != (end - start): # found duplicates or EOS
break
if (ord(max(sorted(substr))) - ord(min(sorted(substr))) + 1) == len(substr):
to this:
if len(substr) != (end - start): # found duplicates or EOS
break
if sorted(substr) == list(substr):
That will display ccl for your example input string. The code is simpler because you're trying to solve a simpler problem :-)
You can improve your algorithm by noticing that the string can be broken into runs of ordered substrings of maximal length. Any ordered substring must be contained in one of these runs
This allows you to just iterate once through the string O(n)
def longest_substring(string):
curr, subs = '', ''
for char in string:
if not curr or char >= curr[-1]:
curr += char
else:
curr, subs = '', max(curr, subs, key=len)
return max(curr, subs, key=len)
s = 'cyqfjhcclkbxpbojgkar'
longest = ""
max =""
for i in range(len(s) -1):
if(s[i] <= s[i+1] ):
longest = longest + s[i]
if(i==len(s) -2):
longest = longest + s[i+1]
else:
longest = longest + s[i]
if(len(longest) > len(max)):
max = longest
longest = ""
if(len(s) == 1):
longest = s
if(len(longest) > len(max)):
print("Longest substring in alphabetical order is: " + longest)
else:
print("Longest substring in alphabetical order is: " + max)
In a recursive way, you can import count from itertools
Or define a same method:
def loops( I=0, S=1 ):
n = I
while True:
yield n
n += S
With this method, you can obtain the value of an endpoint, when you create any substring in your anallitic process.
Now looks the anallize method (based on spacegame issue and Mr. Tim Petters suggestion)
def anallize(inStr):
# empty slice (maxStr) to implement
# str native methods
# in the anallize search execution
maxStr = inStr[0:0]
# loop to read the input string (inStr)
for i in range(len(inStr)):
# loop to sort and compare each new substring
# the loop uses the loops method of past
# I = sum of:
# (i) current read index
# (len(maxStr)) current answer length
# and 1
for o in loops(i + len(maxStr) + 1):
# create a new substring (newStr)
# the substring is taked:
# from: index of read loop (i)
# to: index of sort and compare loop (o)
newStr = inStr[i:o]
if len(newStr) != (o - i):# detect and found duplicates
break
if sorted(newStr) == list(newStr):# compares if sorted string is equal to listed string
# if success, the substring of sort and compare is assigned as answer
maxStr = newStr
# return the string recovered as longest substring
return maxStr
Finally, for test or execution pourposes:
# for execution pourposes of the exercise:
s = "azcbobobegghakl"
print "Longest substring in alphabetical order is: " + anallize( s )
The great piece of this job started by: spacegame and attended by Mr. Tim Petters, is in the use of the native str methods and the reusability of the code.
The answer is:
Longest substring in alphabetical order is: ccl
In Python character comparison is easy compared to java script where the ASCII values have to be compared. According to python
a>b gives a Boolean False and b>a gives a Boolean True
Using this the longest sub string in alphabetical order can be found by using the following algorithm :
def comp(a,b):
if a<=b:
return True
else:
return False
s = raw_input("Enter the required sting: ")
final = []
nIndex = 0
temp = []
for i in range(nIndex, len(s)-1):
res = comp(s[i], s[i+1])
if res == True:
if temp == []:
#print i
temp.append(s[i])
temp.append(s[i+1])
else:
temp.append(s[i+1])
final.append(temp)
else:
if temp == []:
#print i
temp.append(s[i])
final.append(temp)
temp = []
lengths = []
for el in final:
lengths.append(len(el))
print lengths
print final
lngStr = ''.join(final[lengths.index(max(lengths))])
print "Longest substring in alphabetical order is: " + lngStr
Use list and max function to reduce the code drastically.
actual_string = 'azcbobobegghakl'
strlist = []
i = 0
while i < len(actual_string)-1:
substr = ''
while actial_string[i + 1] > actual_string[i] :
substr += actual_string[i]
i += 1
if i > len(actual_string)-2:
break
substr += actual-string[i]
i += 1
strlist.append(subst)
print(max(strlist, key=len))
Wow, some really impressing code snippets here...
I want to add my solution, as I think it's quite clean:
s = 'cyqfjhcclkbxpbojgkar'
res = ''
tmp = ''
for i in range(len(s)):
tmp += s[i]
if len(tmp) > len(res):
res = tmp
if i > len(s)-2:
break
if s[i] > s[i+1]:
tmp = ''
print("Longest substring in alphabetical order is: {}".format(res))
Without using a library, but using a function ord() which returns ascii value for a character.
Assumption: input will be in lowercase, and no special characters are used
s = 'azcbobobegghakl'
longest = ''
for i in range(len(s)):
temp_longest=s[i]
for j in range(i+1,len(s)):
if ord(s[i])<=ord(s[j]):
temp_longest+=s[j]
i+=1
else:
break
if len(temp_longest)>len(longest):
longest = temp_longest
print(longest)
Slightly different implementation, building up a list of all substrings in alphabetical order and returning the longest one:
def longest_substring(s):
in_orders = ['' for i in range(len(s))]
index = 0
for i in range(len(s)):
if (i == len(s) - 1 and s[i] >= s[i - 1]) or s[i] <= s[i + 1]:
in_orders[index] += s[i]
else:
in_orders[index] += s[i]
index += 1
return max(in_orders, key=len)
s = "azcbobobegghakl"
ls = ""
for i in range(0, len(s)-1):
b = ""
ss = ""
j = 2
while j < len(s):
ss = s[i:i+j]
b = sorted(ss)
str1 = ''.join(b)
j += 1
if str1 == ss:
ks = ss
else:
break
if len(ks) > len(ls):
ls = ks
print("The Longest substring in alphabetical order is "+ls)
This worked for me
s = 'cyqfjhcclkbxpbojgkar'
lstring = s[0]
slen = 1
for i in range(len(s)):
for j in range(i,len(s)-1):
if s[j+1] >= s[j]:
if (j+1)-i+1 > slen:
lstring = s[i:(j+1)+1]
slen = (j+1)-i+1
else:
break
print("Longest substring in alphabetical order is: " + lstring)
Output: Longest substring in alphabetical order is: ccl
input_str = "cyqfjhcclkbxpbojgkar"
length = len(input_str) # length of the input string
iter = 0
result_str = '' # contains latest processed sub string
longest = '' # contains longest sub string alphabetic order
while length > 1: # loop till all char processed from string
count = 1
key = input_str[iter] #set last checked char as key
result_str += key # start of the new sub string
for i in range(iter+1, len(input_str)): # discard processed char to set new range
length -= 1
if(key <= input_str[i]): # check the char is in alphabetic order
key = input_str[i]
result_str += key # concatenate the char to result_str
count += 1
else:
if(len(longest) < len(result_str)): # check result and longest str length
longest = result_str # if yes set longest to result
result_str = '' # re initiate result_str for new sub string
iter += count # update iter value to point the index of last processed char
break
if length is 1: # check for the last iteration of while loop
if(len(longest) < len(result_str)):
longest = result_str
print(longest);
finding the longest substring in alphabetical order in Python
in python shell 'a' < 'b' or 'a' <= 'a' is True
result = ''
temp = ''
for char in s:
if (not temp or temp[-1] <= char):
temp += char
elif (temp[-1] > char):
if (len(result) < len(temp)):
result = temp
temp = char
if (len(temp) > len(result)):
result = temp
print('Longest substring in alphabetical order is:', result)
s=input()
temp=s[0]
output=s[0]
for i in range(len(s)-1):
if s[i]<=s[i+1]:
temp=temp+s[i+1]
if len(temp)>len(output):
output=temp
else:
temp=s[i+1]
print('Longest substring in alphabetic order is:' + output)
I had similar question on one of the tests on EDX online something. Spent 20 minutes brainstorming and couldn't find solution. But the answer got to me. And it is very simple. The thing that stopped me on other solutions - the cursor should not stop or have unique value so to say if we have the edx string s = 'azcbobobegghakl' it should output - 'beggh' not 'begh'(unique set) or 'kl'(as per the longest identical to alphabet string). Here is my answer and it works
n=0
for i in range(1,len(s)):
if s[n:i]==''.join(sorted(s[n:i])):
longest_try=s[n:i]
else:
n+=1
In some cases, input is in mixed characters like "Hello" or "HelloWorld"
**Condition 1:**order determination is case insensitive, i.e. the string "Ab" is considered to be in alphabetical order.
**Condition 2:**You can assume that the input will not have a string where the number of possible consecutive sub-strings in alphabetical order is 0. i.e. the input will not have a string like " zxec ".
string ="HelloWorld"
s=string.lower()
r = ''
c = ''
last=''
for char in s:
if (c == ''):
c = char
elif (c[-1] <= char):
c += char
elif (c[-1] > char):
if (len(r) < len(c)):
r = c
c = char
else:
c = char
if (len(c) > len(r)):
r = c
for i in r:
if i in string:
last=last+i
else:
last=last+i.upper()
if len(r)==1:
print(0)
else:
print(last)
Out:elloW
```python
s = "cyqfjhcclkbxpbojgkar" # change this to any word
word, temp = "", s[0] # temp = s[0] for fence post problem
for i in range(1, len(s)): # starting from 1 not zero because we already add first char
x = temp[-1] # last word in var temp
y = s[i] # index in for-loop
if x <= y:
temp += s[i]
elif x > y:
if len(temp) > len(word): #storing longest substring so we can use temp for make another substring
word = temp
temp = s[i] #reseting var temp with last char in loop
if len(temp) > len(word):
word = temp
print("Longest substring in alphabetical order is:", word)
```
My code store longest substring at the moment in variable temp, then compare every string index in for-loop with last char in temp (temp[-1]) if index higher or same with (temp[-1]) then add that char from index in temp. If index lower than (temp[-1]) checking variable word and temp which one have longest substring, after that reset variable temp so we can make another substring until last char in strings.
s = 'cyqfjhcclkbxpbojgkar'
long_sub = '' #longest substring
temp = '' # temporarily hold current substr
if len(s) == 1: # if only one character
long_sub = s
else:
for i in range(len(s) - 1):
index = i
temp = s[index]
while index < len(s) - 1:
if s[index] <= s[index + 1]:
temp += s[index + 1]
else:
break
index += 1
if len(temp) > len(long_sub):
long_sub = temp
temp = ''
print(long_sub)
For comprehensibility, I also add this code snippet based on regular expressions. It's hard-coded and seems clunky. On the other hand, it seems to be the shortest and easiest answer to this problem. And it's also among the most efficient in terms of runtime complexity (see graph).
import re
def longest_substring(s):
substrings = re.findall('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*', s)
return max(substrings, key=len)
(Unfortunately, I'm not allowed to paste a graph here as a "newbie".)
Source + Explanation + Graph: https://blog.finxter.com/python-how-to-find-the-longest-substring-in-alphabetical-order/
Another way:
s = input("Please enter a sentence: ")
count = 0
maxcount = 0
result = 0
for char in range(len(s)-1):
if(s[char]<=s[char+1]):
count += 1
if(count > maxcount):
maxcount = count
result = char + 1
else:
count = 0
startposition = result - maxcount
print("Longest substring in alphabetical order is: ", s[startposition:result+1])
I am trying to figure out how many times a string occurs in a string. For example:
nStr = '000123000123'
Say the string I want to find is 123. Obviously it occurs twice in nStr but I am having trouble implementing this logic into Python. What I have got at the moment:
pattern = '123'
count = a = 0
while pattern in nStr[a:]:
a = nStr[a:].find(pattern)+1
count += 1
return count
The answer it should return is 2. I'm stuck in an infinite loop at the moment.
I was just made aware that count is a much better way to do it but out of curiosity, does anyone see a way to do it similar to what I have already got?
Use str.count:
>>> nStr = '000123000123'
>>> nStr.count('123')
2
A working version of your code:
nStr = '000123000123'
pattern = '123'
count = 0
flag = True
start = 0
while flag:
a = nStr.find(pattern, start) # find() returns -1 if the word is not found,
#start i the starting index from the search starts(default value is 0)
if a == -1: #if pattern not found set flag to False
flag = False
else: # if word is found increase count and set starting index to a+1
count += 1
start = a + 1
print(count)
The problem with count() and other methods shown here is in the case of overlapping substrings.
For example: "aaaaaa".count("aaa") returns 2
If you want it to return 4 [(aaa)aaa, a(aaa)aa, aa(aaa)a, aaa(aaa)] you might try something like this:
def count_substrings(string, substring):
string_size = len(string)
substring_size = len(substring)
count = 0
for i in xrange(0,string_size-substring_size+1):
if string[i:i+substring_size] == substring:
count+=1
return count
count_substrings("aaaaaa", "aaa")
# 4
Not sure if there's a more efficient way of doing it, but I hope this clarifies how count() works.
import re
pattern = '123'
n =re.findall(pattern, string)
We can say that the substring 'pattern' appears len(n) times in 'string'.
In case you are searching how to solve this problem for overlapping cases.
s = 'azcbobobegghaklbob'
str = 'bob'
results = 0
sub_len = len(str)
for i in range(len(s)):
if s[i:i+sub_len] == str:
results += 1
print (results)
Will result in 3 because: [azc(bob)obegghaklbob] [azcbo(bob)egghaklbob] [azcbobobegghakl(bob)]
I'm pretty new, but I think this is a good solution? maybe?
def count_substring(str, sub_str):
count = 0
for i, c in enumerate(str):
if sub_str == str[i:i+2]:
count += 1
return count
string.count(substring) is not useful in case of overlapping.
My approach:
def count_substring(string, sub_string):
length = len(string)
counter = 0
for i in range(length):
for j in range(length):
if string[i:j+1] == sub_string:
counter +=1
return counter
You are not changing a with each loop. You should put:
a += nStr[a:].find(pattern)+1
...instead of:
a = nStr[a:].find(pattern)+1
def count_substring(string, substring):
c=0
l=len(sub_string)
for i in range(len(string)):
if string [i:i+l]==sub_string:
c=c+1
return c
string=input().strip()
sub_string=input().strip()
count= count_substring(string,sub_string)
print(count)
As mentioned by #João Pesce and #gaurav, count() is not useful in the case of overlapping substrings, try this out...
def count_substring(string, sub_string):
c=0
for i in range(len(string)):
if(string[i:i+len(sub_string)]==sub_string):
c = c+1
return c
def countOccurance(str,pat):
count=0
wordList=str.split()
for word in wordList:
if pat in word:
count+=1
return count
Usually i'm using enumerate for this kind of problems:
def count_substring(string, sub_string):
count = 0
for i, j in enumerate(string):
if sub_string in string[i:i+3]:
count = count + 1
return count
def count(sub_string,string):
count = 0
ind = string.find(sub_string)
while True:
if ind > -1:
count += 1
ind = string.find(sub_string,ind + 1)
else:
break
return count
def count_substring(string, sub_string):
count = 0
len_sub = len(sub_string)
for i in range(0,len(string)):
if(string[i:i+len_sub] == sub_string):
count+=1
return count