Page 364 - Computer Science V2.0 Class 11
P. 364

●  On execution of lines 10–12, for every character in the string, if the character is not included (not present) in the
                 string distinctChars, it is concatenated to distinctChars.
              ●  On execution of line 13, finally, the function returns a string (distinctChars) of distinct characters.

               Program 12.1 Count the distinct characters in a given string.

                01 def distinct(str1):
                02     '''
                03     Objective: To determine unique characters in the given string
                04     Input Parameters:
                05     str1 - string value
                06     Return value: str- string of unique characters
                07     '''
                08     str1 = str1.lower()
                09     distinctChars = ''
                10     for ch1 in str1:
                11         if ch1 not in distinctChars:
                12             distinctChars += ch1
                13     return distinctChars
              12.4.2 The Number of Common Characters in a Pair of Strings
              To compute the count of matching characters in a pair of strings, let us develop a function findCommon() that takes
              a pair of strings str1 and str2 as input parameters.
              ●  On execution of line 9, the two strings may comprise a mix of upper and lowercase characters, we first construct a
                 string of lowercase characters.
              ●  Line 10, we initialize the count of matching characters (count) in the two strings to zero.
              ●  Next, using the function distinctChars, we first construct strings of unique characters from both strings.
              ●  On execution of lines 12–15, The for-loop scans the two strings.

              ●  or every character in the first string, we check whether there is a matching character in the second string.
                 F
              ●  Line 13, if a matching character is found, we increment the count by 1 in line 14.
              ●  On execution of line 15, finally, the function returns the count characters.

               Program 12.2 The number of common characters in a pair of strings.

                01 def findCommon(str1, str2):
                02     '''
                03     Objective: To find the number of common characters in a pair of strings
                04     Input Parameters:
                05         str1 - string value
                06         str2 - string value
                07     Return value: int- count
                08     '''
                09     str1, str2 = str1.lower(), str2.lower()
                10     count = 0
                11     uniqueStr1, uniqueStr2 = distinct(str1), distinct(str2)
                12     for ch1 in uniqueStr1:
                13         if ch1 in uniqueStr2 :
                14                  count += 1
                15     return count
              12.4.3 Searching for a Substring Within Another String
              Let us write a function that searches for a substring in a given string and returns True if the substring is present in the
              given string and False otherwise. To achieve this, let us develop a function isSubString() that takes two strings
              (string and substr) as input parameters.


               350   Touchpad Computer Science (Ver. 2.0)-XI
   359   360   361   362   363   364   365   366   367   368   369