- containsLetter() : checks whether or not the given string word includes the given character ch (Use for loop). The method will return True or False.
- 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?
First of all, it is necessary to create a
CustomStringclass that inherits fromstr(python's own class). First, thecontainsLettermethod will receive astringand validate that the letter is inself(remember thatselfis an object that inherits fromstr). In this case the.lower()method is applied to bothstringsso that the method is independent of whether the letters are lowercase or uppercase.On the other hand, the
ruleCheckmethod will place the alphabet in a variable (importing from thestringmodule) 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:
And some examples of usage:
Hope it helps!