Number of Times Sub-string Appears in String, Where One Character May be Different, in Python 3

Question:

I found this problem on codingbat.com under the Python section. The problem title is: String-2 > count_code.

Return the number of times that the string “code” appears anywhere in a given string, except we’ll accept any letter for the ‘d’, so “cope”, “cooe”, “coze”, etc will count. But “co5e”, “co)e”, etc will not count because only a letter is allowed in the ‘d’ index.

I added the part in bold (it’s not in the original question). I was wondering if there was a short way of writing the code without listing all letters of the alphabet. My code currently looks like:

def count_code(string):
   count = 0
   for i in range(len(string) - 3):
      if string[i:i+2] == "co" and (string[i+2] == "a" or string[i+2] == "b" or str[i+2] == "c" or str[i+2] == "d" or str[i+2] == "e" or #you get the idea) and str[i+3] == "e":
         count += 1
   return count    

I hope this is explanatory. The link to the original problem is: https://codingbat.com/prob/p186048
I would appreciate all answers.

Asked By: Goriola

||

Answers:

EDIT: The original problem link does not allow imports, so the re module cannot be used. Please refer to Jarvis’s solution for it to work with that particular site. However, using regex is the preferred method for such problems so I will be leaving this answer here.


You should use Regular Expressions for problems where such pattern matching is needed. This is the RegEx HOWTO from the official docs, but you can find numerous tutorials online.

The expression needed to solve this is quite basic, and since the problems are meant to help you learn, please look at the answer below only after you have attempted it yourself.

‘co[a-zA-z]e’

You’ll be using the above to create a Pattern, then call the findall method on your input string. This will return a list of matches, so the number of matches would be len(resultlist).

Answered By: shriakhilc

Maybe this code will help:

def count_code(str):
   i, ans = 0, 0
   while i < len(str) - 3:
      if str[i:i+2] == 'co' and str[i+3] == 'e':
         # the catch
         if str[i+2] >= 'a' and str[i+2] <= 'z':
            i += 4
            ans += 1
         else:
            i += 1
      else:
          i += 1
   return ans

I would advise you not to ask any questions from competitive programming sites on Stack Overflow, this platform is totally for different purposes.

Answered By: Jarvis

This code passed all the tests.

def count_code(str):
  n = len(str)
  if n <= 3:
    return 0
  count = 0
  for i in range(n - 3):
    temp = str[i: i + 4]
    if temp[0:2] == 'co':
      if (ord(temp[2]) >= ord('a') and ord(temp[2]) <= ord('z')) or (ord(temp[2]) >= ord('A') and ord(temp[2]) <= ord('Z')):
        if temp[3] == 'e':
          count += 1

  return count

I have used the ascii values to check the third character.

Answered By: gopiariv
def count_code(str):
  c = 0
  for i in range(0, len(str) - 3, 1):
    if str[i:i+2] == "co" and str[i+3] == "e":
      c += 1
  return c
Answered By: post_lupy
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.