There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.ExampleThere are instances of '', of '' and of ''. For each query, add an element to the return array, .Function DescriptionComplete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in stringList.matchingStrings has the following parameters:string stringList[n] - an array of strings to searchstring queries[q] - an array of query stringsReturnsint[q]: an array of results for each queryInput FormatThe first line contains and integer , the size of .Each of the next lines contains a string .The next line contains , the size of .Each of the next lines contains a string .Constraints .Contest ends in 2 hoursSubmissions: 86Max Score: 10Difficulty: MediumRate This Challenge: More Python 31#!/bin/python323import math4import os5import random6import re7import sys89#10# Complete the 'matchingStrings' function below.11#12# The function is expected to return an INTEGER_ARRAY.13# The function accepts following parameters:14# 1. STRING_ARRAY stringList15# 2. STRING_ARRAY queries16#1718def matchingStrings(stringList, queries):19 # Write your code here2021if __name__ == '__main__':22 fptr = open(os.environ['OUTPUT_PATH'], 'w')2324 stringList_count = int(input().strip())2526 stringList = []2728 for _ in range(stringList_count):29 stringList_item = input()30 stringList.append(stringList_item)3132 queries_count = int(input().strip())3334 queries = []3536 for _ in range(queries_count):37 queries_item = input()38 queries.append(queries_item)3940 res = matchingStrings(stringList, queries)4142 fptr.write('\n'.join(map(str, res)))43 fptr.write('\n')4445 fptr.close()46
Question
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.ExampleThere are instances of '', of '' and of ''. For each query, add an element to the return array, .Function DescriptionComplete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in stringList.matchingStrings has the following parameters:string stringList[n] - an array of strings to searchstring queries[q] - an array of query stringsReturnsint[q]: an array of results for each queryInput FormatThe first line contains and integer , the size of .Each of the next lines contains a string .The next line contains , the size of .Each of the next lines contains a string .Constraints .Contest ends in 2 hoursSubmissions: 86Max Score: 10Difficulty: MediumRate This Challenge: More Python 31#!/bin/python323import math4import os5import random6import re7import sys89#10# Complete the 'matchingStrings' function below.11#12# The function is expected to return an INTEGER_ARRAY.13# The function accepts following parameters:14# 1. STRING_ARRAY stringList15# 2. STRING_ARRAY queries16#1718def matchingStrings(stringList, queries):19 # Write your code here2021if name == 'main':22 fptr = open(os.environ['OUTPUT_PATH'], 'w')2324 stringList_count = int(input().strip())2526 stringList = []2728 for _ in range(stringList_count):29 stringList_item = input()30 stringList.append(stringList_item)3132 queries_count = int(input().strip())3334 queries = []3536 for _ in range(queries_count):37 queries_item = input()38 queries.append(queries_item)3940 res = matchingStrings(stringList, queries)4142 fptr.write('\n'.join(map(str, res)))43 fptr.write('\n')4445 fptr.close()46
Solution
The provided text is a Python code. It's a problem statement where you need to complete the function matchingStrings(stringList, queries). This function should return an array of integers representing the frequency of occurrence of each query string in stringList.
Here is a step-by-step guide to solve this problem:
-
First, understand that
stringListis a list of strings where we will search for the queries, andqueriesis a list of strings that we will search for instringList. -
We need to iterate over each query in
queriesand count how many times it appears instringList. We can use thecount()method of a list in Python, which returns the number of times a specific element appears in the list. -
For each query, we append the count to a result list
res. -
Finally, we return the result list
res.
Here is the completed function:
def matchingStrings(stringList, queries):
res = []
for query in queries:
res.append(stringList.count(query))
return res
This function works by iterating over each query and counting its occurrences in stringList using the count() method. The count is then appended to the result list res. After all queries have been processed, res is returned.
Similar Questions
Complete the find_string(sentence, string_to_find) function that takes two string parameters, namely sentence and string_to_find. This function should return the number of times that the parameter, string_to_find, appears in the sentence.
Ram is a supermarket employee. He works in the stock maintenance department. He wants to determine how frequently these items are repeated. Assist developers in writing code to address the problem.Input format :The first line of the input consists of the number of productThe second line of the input consists of the product nameThe third line of the input consists of the searched element.Output format :The output displays the frequency of the searched element.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :7milkfruit breadjammilkeggsFrozen FoodsCheesemilkOutput 1 :2
We are given a function countWordOccurrences that takes two string parameters sentence and word."Sentence" is a string made up of multiple words separated by spaces.We have to find how many times 'word' occurs in the given 'sentence'.The 'word' should be an exact match.Note: The case (uppercase/lowercase) of the letters in the word does NOT matter.InputFirst line contains space separated series of words which represents the string "sentence".Second line contains a single word "word".Constraints1 ≤ length(sentence), length(word) ≤ 104length(word) ≤ length(sentence)OutputWe have to return an integer which is the number of occurrences of the single word 'word' in the string 'sentence'.
Suppose that an array of strings contains ["blueberry", "chocolate", "coconut", "coffee", "mint", "strawberry", "vanilla"]. How many compares are used for a successful search for "mint" with sequential search?1 point23456
Define the function count_substring(string, substring) that finds how many times the substring appears in the given string.Example:string = "Python is a powerful language. I want to learn Python"substring = "Python"Expected Output:2
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.