Start Your Journey with Linux Command Line
| Find a word game |
improving the student's skills in operating with strings;
using the find() method for searching strings.
Let's play a game. We will give you two strings: one being a word (e.g., "dog") and the second being a combination of any characters.
Your task is to write a program which answers the following question: are the characters comprising the first string hidden inside the second string?
For example:
Hints:
Test your code using the data we've provided.
Sample input:
donor
Nabucodonosor
Sample output:
Yes
Sample input:
donut
Nabucodonosor
Sample output:
No
word = input("Please Enter a word: ").lower()
text = input("Please Enter a text: ").lower()
found = True
start = 0
In LAB 2.5.1.10 Find a word!, the task is to decide whether the letters of one string appear, in the correct order, somewhere inside a longer combination of characters. This is not about simple containment such as checking whether one whole text is a substring of another. Instead, you must scan through the larger string and confirm that each letter of the target word shows up after the previous one. The example "donor" hidden in "Nabucodonosor" returns Yes, while "donut" in the same text returns No, because after the letters d, o, and n you cannot find a u, then a t, in the required order.
The elegant approach is to search for the letters one at a time using the find() method, advancing a starting position each time. You convert both strings to lowercase so the comparison is case-insensitive, then loop over each character of the word. For every character you call text.find(ch, start), where start is the position just after the previous match. The two-argument form of find() tells Python where to begin the search, which guarantees the letters are checked in order rather than anywhere in the text.
The key to the algorithm is a variable, start, initialized to 0. For each character in the word, text.find(ch, start) returns the index of the first occurrence at or after start, or -1 if there is none. If the result is -1, the searched-for letter cannot be placed in order, so the program sets a found flag to False and stops checking further. When a match is found, you set start = pos + 1 so the next letter must appear strictly after the current one, preserving the sequence required by an anagram-like hidden word.
Notice how the two-argument find() differs from checking membership with the in operator. A plain if ch in text would only report whether the letter exists somewhere, not whether the letters appear in the right relative order. Advancing start is what turns a collection of individual letters into a proper ordered check, which is exactly the subtlety this lab wants you to master.
lower() makes the comparison case-insensitive.text.find(ch, start) uses the two-argument form to decide where each search begins.start = pos + 1 after every match ensures the next letter is looked for only after the previous one.find() means the letter cannot be placed, so the answer becomes No.The in operator only reports whether a letter exists somewhere in the text. It does not respect order, so letters could be scattered incorrectly and the program would still say the word is present.
It sets the starting position for the search. text.find(ch, start) looks for ch only at indexes equal to or greater than start, which is what enforces the ordering requirement.
Using pos + 1 forces the next letter to begin strictly after the current match, so two letters of the word cannot occupy the same position, keeping the sequence valid.
A return value of -1 means the requested letter cannot be found from the given starting position. The program sets the found flag to False, so the final output is No.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.