0

Bit stuck on a coding challenge here! I'm writing a function that takes two arguments (strings, queries) and prints the number of times each query string occurs in the input string. I think I'm quite close to figuring this out but my function is currently insensitive to query strings with spaces before/after a query string.

Version 1 (insensitive to query strings containing spaces):

strings = ['ab', ' ab', 'abc']
queries = ['ab', ' abc', ' bc']

def matchingStrings(strings, queries):
    for i in range(len(queries)):]
        n_matches = strings.count(queries[i])
        print(n_matches)

matchingStrings(strings,queries)

Current Output:

1
0
0

Version 2 (attempt to retain quotation marks):

def matchingStrings(strings, queries):
    for i in range(len(queries)):
        query_to_match = '\'%s\'' % queries[i]
        n_matches = strings.count(query_to_match)
        print(n_matches)


matchingStrings(strings,queries)

Current Output:

0
0
0

Expected Output:

2
1
0
M_Oxford
  • 361
  • 4
  • 11
  • 1
    The single quotes do not exist as part of the strings in your lists. They are used to define/delimit the strings. If you want single quotes as part of those strings then: strings = ['\'ab\'', '\' ab\'', '\'abc\''] – DarkKnight Jan 11 '22 at 08:55

2 Answers2

1

This will work by using regex, albeit slower as it iterates through two lists:

def matching_strings(strings, queries):
    for query in queries:
        count = 0
        for string in strings:
            if re.match(query.strip(), string):
                count += 1
        print(count)

Running the function on your input will provide the desired output! This works by checking if there is a match on the query string (without whitespace with .strip()).

Here is my output:

>>> strings = ['ab', ' ab', 'abc']
>>> queries = ['ab', ' abc', ' bc']
>>> matching_strings(strings, queries)
2
1
0
Sam
  • 773
  • 4
  • 13
1

So this solution is close to the right answer, but there are a few things going on. First, to compare all of the queries with all of the strings, we will need two for loops. Here is the pseudocode to help visualize what is going on:

  1. For each query in queries: Start a count to count how many words in strings match the current query. Will reset for each query.
  2. For each word that we want to compare to the current query: We don't care about whitespace, so we will strip it from both the query and the string.
  3. If the word and query are the same after stripping them:
  4. Add one to the counter.
  5. After going though all the words, print the count, which holds how many of the words match the current query.
  6. Move on to the next query.

Here is the python if you would like to see it.

strings = ['ab', ' ab', 'abc']
queries = ['ab', ' abc', ' bc']

def matchingStrings(strings, queries):
    for query in queries:
        count = 0 
        # need to iterate through ALL strings. 
        for string in strings:
            if query.strip() == string.strip():
               count += 1
        print(count)

matchingStrings(strings, queries)

With the output:

2
1
0
Cole Agard
  • 41
  • 4