Ask Question
15 December, 13:47

Create a function named common_letters (strings) that returns the intersection of letters among a list of strings. The parameter is a list of strings. For example, you can find the common letter in the domains/words statistics, computer science, and biology.

+3
Answers (1)
  1. 15 December, 13:51
    0
    The solution code is written in Python 3:

    letterList = ['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'] def common_letters (strings) : result = [] for l in letterList: count = 0 for s in strings: if l in s: count = count + 1 if (count = = len (strings)) : result. append (l) return result print (common_letters (["statistics", "computer science", "biology"])) print (common_letters (["apple", "orange", "grape"]))

    Explanation:

    Firstly, we can create a letter list that includes the letters from a to z.

    Next we create a function common_letters that take one parameter (Line 4).

    Create a result list to hold the output the function (Line 5).

    Next, we create a for-loop to traverse through each of the letter in the letterList and then create another for loop to traverse through every single string in the input strings and then check if the letter found in a current string. If so increment the count by one. (Line 7 - 11).

    Logically if the number of occurrence of a single letters equal to number of words in the strings list, this means the letter is common to all the words (Line 13). We can append this letter, l, to result list (Line 14) and return it as the output (Line 16).

    We can test the functions using two sample word list (Line 18-19) and the output is as follows:

    ['i']

    ['a', 'e']
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Create a function named common_letters (strings) that returns the intersection of letters among a list of strings. The parameter is a list ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers