How to count specific substrings using slice notation - python

I want to count the number of occurrences of the substring "bob" within the string s. I do this exercise for an edX Course.
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+2] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
However it seems that the s[position+1:position+2] statement is not working properly. How do i adress the two chars behind a "b"?

The second slice index isn't included. It means that s[position+1:position+2] is a single character at position position + 1, and this substring cannot be equal to ob. See a related answer. You need [:position + 3]:
s = 'azcbobobegghakl'
counter = 0
numofiterations = len(s)
position = 0
#loop that goes through the string char by char
for iteration in range(numofiterations - 2):
if s[position] == "b": # search pos. for starting point
if s[position+1:position+3] == "ob": # check if complete
counter += 1
position +=1
print("Number of times bob occurs is: " + str(counter))
# 2

You could use .find with an index:
s = 'azcbobobegghakl'
needle = 'bob'
idx = -1; cnt = 0
while True:
idx = s.find(needle, idx+1)
if idx >= 0:
cnt += 1
else:
break
print("{} was found {} times.".format(needle, cnt))
# bob was found 2 times.

Eric's answer explains perfectly why your approach didn't work (slicing in Python is end-exclusive), but let me propose another option:
s = 'azcbobobegghakl'
substrings = [s[i:] for i in range(0, len(s))]
filtered_s = filter(substrings, lambda s: s.startswith("bob"))
result = len(filtered_s)
or simply
s = 'azcbobobegghakl'
result = sum(1 for ss in [s[i:] for i in range(0, len(s))] if ss.startswith("bob"))

Related

What is wrong in my finding frequency of substring in a strng

I am trying to find the no. of occurrence of a substring in a string. I know that count function can be used but it is for non-overlapping occurrences.
Here is my code I found online
string = input("Enter the string: ");
sub_string = input("Enter the substring: ")
count = 0
for i in range(0, len(string)):
for j in range(0, len(sub_string)):
if string[i] == sub_string[j]:
j += 1
else:
j = 0
if j == len(sub_string):
count += 1
print(count)
In this, in the 2nd loop we compare each element of sub_string[j] to string[i] which is same in that loop, then how j will increase & increase the count after checking in next if condition.
So the code you have is wrong, since it would pass the if check as long as the last character matches the matches the string, e.g. string
"sagar is sagar not sa saga sagar r" and substring "sagar" would return 4 rather than 3.
Since you use j both to count the number of matches as well as the index for the substring, it gets overwritten in the inner loop each iteration. This means that it would only need to match the very last letter to pass the check below the loop.
You might want to change it to something that checks each character starting at some position and denies it if it doesn't match, e.g.
string = input("Enter the string: ");
sub_string = input("Enter the substring: ")
count = 0
for i in range(0, len(string) - len(sub_string)):
found = True
for j in range(len(sub_string)):
if string[i + j] != sub_string[j]:
found = False
break
if found:
count += 1
In your case only the last elements are equal then the count will increment by one.
for ex:
if string is seg, and sub string is sag the the count will be one.
this happens because in the second loop when j = len(substring)-1, g from seg and sag will be detected as equal. so then count get increment by one.
string = input("Enter the string: ");
sub_string = input("Enter the substring: ")
count = 0
for i in range(0, len(string)):
j=0
while i+j< len(string) and j <len(sub_string):
if string[i+j] == sub_string[j]:
j += 1
else:
break
else:
count += 1
print(count)
try this code, in there if while loop didn't get break count increment by one.

counting number of occurrence in string

I'm trying to count the number of times "bob" has occurred in a given string. this is what I tried:
s = input("give me a string:")
count = 0
for i in s:
if i=="b":
for j in s:
x=0
if j!="b":
x+=1
else:
break
if s[x+1]=="o" and s[x+2]=="b":
count+=1
print(count)
if I give the string "bob", it gives back 2, and if I give something like "jbhxbobalih", it gives back 0. I don't know why this happens. any idea?
The easiest manual count would probably use indeces and slices. The main difference between this and the much simpler s.count("bob") is that it also counts overlapping occurrences:
# s = "aboboba" -> 2
count = 0
for i, c in enumerate(s):
if s[i:i+3] == "bob":
count += 1
You can try checking 3 consecutive characters, if they are 'bob', just add our counter up, and do nothing otherwise.
Your code should be like this:
s = input("give me a string:")
count = 0
for i in range(0, len(s) - 3):
if s[i] == 'b' and s[i + 1] == 'o' and s[i + 2] == 'b':
count += 1
print(count)
100 % working this will work for all string.
import re
def check(string, sub_str):
count = 0
val = re.findall(sub_str, string)
for i in val:
count+=1
print(count)
# driver code
string = "baadbobaaaabobsasddswqbobdwqdwqsbob"
sub_str = "bob"
check(string, sub_str)
This gives the correct output.

how do i run length encode a pattern, rather than a character?

heres my current RLE code
import re
def decode(string):
if string == '':
return ''
multiplier = 1
count = 0
rle_decoding = []
rle_encoding = []
rle_encoding = re.findall(r'[A-Za-z]|-?\d+\.\d+|\d+|[\w\s]', string)
for item in rle_encoding:
if item.isdigit():
multiplier = int(item)
elif item.isalpha() or item.isspace():
while count < multiplier:
rle_decoding.append('{0}'.format(item))
count += 1
multiplier = 1
count = 0
return(''.join(rle_decoding))
def encode(string):
if string == '':
return ''
i = 0
count = 0
letter = string[i]
rle = []
while i <= len(string) - 1:
while string[i] == letter:
i+= 1
count +=1
#catch the loop on last character so it doesn't got to top and access out of bounds
if i > len(string) - 1:
break
if count == 1:
rle.append('{0}'.format(letter))
else:
rle.append('{0}{1}'.format(count, letter))
if i > len(string) - 1: #ugly that I have to do it twice
break
letter = string[i]
count = 0
final = ''.join(rle)
return final
the code might have gotten fucked up when I removed all my comments, but the current code isn't too important. the problem is, I am running RLE on hexadecimal values, that have all been converted to letters so that 0-9 becomes g-p. the problem is that there are a lot of patterns like 'kjkjkjkjkjkjkjkjlmlmlmlmlmlmlm' which doesn't compress at all, because of their not single characters. how would I, if even possible, be able to run my program so that it encodes patterns as well?

Python, how to not require an additional space at the end of the list?

I've written a program which can compress a sequence of characters.
def compress(string):
output = ""
counter = 1
firstLoop = True
for element in range(0, len(string)):
# if statement checking if current character was last character
if string[element] == string[element - 1]:
# if it was, then the character has been written more than one
# time in a row, so increase counter
counter = counter + 1
else:
# when we detect a new character reset the counter
# and also record the character and how many times it was repeated
if not firstLoop:
output = output + string[element - 1] + str(counter)
counter = 1
firstLoop = False
return output
data = "aaaabbbchhtttttttf"
print(data)
compressedData = compress(data)
print(compressedData)
The program outputs:
aaaabbbchhtttttttf
a4b3c1h2t7
So, it finds that there's '4' entries of 'a' so it writes 'a4', then 'b3' for three entries of b.
The issue is that it forgets about the 'f1' at the end of the string. I know this is because of the line:
output = output + string[element - 1] + str(counter)
Since string[element-1] refers to the position in the string before the current element, thus, it will never reach the final position which is where 'f' is. The program doesn't work without the '-1' since it doesn't write the correct letter.
How can I get around this problem and make it able to include f?
The correct output should be a4b3c1h2t7f1.
Thanks :)
Edit: I forgot to mention that the program works if I include an additional character after the 'f', such as just a blank space. But that's of course because the final character in my string is just a space rather than a letter.
You could do this all with itertools.groupby and sum and avoid all counting and keeping track of indexes:
from itertools import groupby
def compress(string):
return ''.join(k + str(sum(1 for _ in g)) for k, g in groupby(string))
>>> compress("aaaabbbchhtttttttf")
'a4b3c1h2t7f1'
You could make it simpler and add a character at the end:
def compress(string):
output = ""
counter = 0
string = string + '|'
for element in range(0, len(string)):
# if statement checking if current character was last character
if string[element] == string[element - 1]:
# if it was, then the character has been written more than one
# time in a row, so increase counter
counter = counter + 1
elif element != len(string):
output = output + string[element - 1] + str(counter)
counter = 1
return output[2:]
data = "aaaabbbchhtttttttf"
print(data)
compressedData = compress(data)
print(compressedData)
def compress(string):
output = ""
counter = 1
for element in range(1, len(string)):
# if statement checking if current character was last character
if string[element] == string[element - 1]:
# if it was, then the character has been written more than one
# time in a row, so increase counter
counter = counter + 1
else:
# when we detect a new character reset the counter
# and also record the character and how many times it was repeated
output = output + string[element - 1] + str(counter)
counter = 1
return output + string[-1] + str(counter)
Also note that you need to start counting form 1 not 0 and get rid of firstLoop
Try changing the loop to for element in range(0, len(string) + 1) and adding an extra if condition:
for element in range(0, len(string) + 1):
if element == len(string):
output = output + string[element-1] + str(counter)
# if statement checking if current character was last character
elif string[element] == string[element - 1]: ...
In the spirit of fixing your code you just needed to simply add the element first to output before adding the counter on change. You can use a neat treat called else with for loops that will run at the end, which will add the final counter to f. No need to buffer or import anything special, you were fairly close:
def compress(string):
output = ""
counter = 0
firstLoop = True
for i in range(len(string)):
# if statement checking if current character was last character
if firstLoop:
counter += 1
output += string[i]
else:
if string[i] == string[i - 1]:
counter += 1
else:
output += str(counter) + string[i]
counter = 1
firstLoop = False
else:
output += str(counter)
return output
data = "aaaabbbchhtttttttf"
print(data)
compressedData = compress(data)
print(compressedData)

Find the longest substring in alphabetical order

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

Categories