Create a module, Lab04_yourname_module.py that contains the following functions:

40 Views Asked by At
  1. containsLetter() : checks whether or not the given string word includes the given character ch (Use for loop). The method will return True or False.
  2. ruleChecker() : gets a string sentence and checks whether it conforms to a special rule or not. The method returns -1, if the input string obeys the rule (see below). Otherwise, it returns the index of the first word in which the rule is violated. The method should use the containsLetter method to check whether a word contains the subsequent letter in the alphabet.

The rule is defined as follows: The ith word in the text must contain ith letter of the alphabet.

For example the following sentences conform to the rule (-1 will return for these sentences):

  • A bear can discover precious food disguised beneath ice.
  • Any butcher certainly needs meat freezer.

The following sentence does not follow the rule:

-A beautifully crafted diamond necklace for girl was gifted by the groom. -> rule violated in the 7th word.

(Start counting from 0) Hint: To check the rule, store all of the English alphabetic letters in a string variable and use their index to access them.

what am ı supposed to do?

1

There are 1 best solutions below

0
Cuartero On

First of all, it is necessary to create a CustomString class that inherits from str (python's own class). First, the containsLetter method will receive a string and validate that the letter is in self (remember that self is an object that inherits from str). In this case the .lower() method is applied to both strings so that the method is independent of whether the letters are lowercase or uppercase.

On the other hand, the ruleCheck method will place the alphabet in a variable (importing from the string module) and separate the initial sentence (the object itself) into its words using the sentence spaces. Subsequently, we will iterate over the words and verify that the ith letter of the alphabet is in the word ith. As in the previous case, everything is put in lower case to avoid probAs in the previous case, everything is put in lower case to avoid problems.

Here is the code:

import string

class CustomString(str):

    def containsLetter(self, letter):
        return letter.lower() in self.lower()

    def ruleChecker(self):
        alphabet = list(string.ascii_lowercase)
        lowercase_string = self.lower()
        sentence_words = [x for x in lowercase_string.split(" ")]

        if len(sentence_words) > len(alphabet):
            raise ValueError("Sentence has more words than the alphabet")


        for n in range(len(sentence_words)):
            if not alphabet[n] in sentence_words[n]: 
                return n
        
        return -1

And some examples of usage:

custom_string = CustomString("Hello world")
print(custom_string.containsLetter("h"))
>>> True
custom_string = CustomString("A beautifully crafted diamond necklace for girl was gifted by the groom.")
print(custom_string.ruleChecker())
>>> 7

Hope it helps!