Featured Posts

Start Your Journey with Linux Command Line

Image
Start Your Journey with the Linux Command Line: A Comprehensive Guide Whether you are a software developer, system administrator, analytics engineer, or cybersecurity enthusiast, mastering the Linux command line (terminal) is one of the single most effective skills you can acquire. While graphical user interfaces (GUIs) offer visual convenience, the command line interface (CLI) delivers unmatched speed, fine-grained system control, and seamless automation capabilities. Linux Command Line This tutorial breaks down more than 35 core Linux commands into structured, practical modules complete with real-world examples, flags, and command-chaining techniques. By building muscle memory around these fundamentals, you will elevate your daily technical workflow from basic navigation to advanced command orchestration. Why Learn the Command Line? The Linux CLI is not merely a legacy tool—it remains the foundation of modern cloud architecture, server maintenance, DevOps pipelines, and enterp...

Python 2.5.1.10 LAB: Find a word!

 Find a Word in a Combination of Characters

Find a word game
Find a word game

Objectives

improving the student's skills in operating with strings;

using the find() method for searching strings.

Scenario

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:

  • if the second string is given as "vcxzxduybfdsobywuefgas", the answer is yes;
  • if the second string is "vcxzxdcybfdstbywuefsas", the answer is no (as there are neither the letters "d", "o", or "g", in this order)

Hints:

  • you should use the two-argument variants of the pos() functions inside your code;
  • don't worry about case sensitivity.

Test your code using the data we've provided.

Test data

Sample input:

donor

Nabucodonosor

Sample output:

Yes

Sample input:

donut

Nabucodonosor

Sample output:

No

Solution Code

word = input("Please Enter a word: ").lower()
text = input("Please Enter a text: ").lower()
found = True
start = 0


for ch in word:
    pos = text.find(ch , start)
    if pos <0:
        found = False
    break
    start = pos + 1
if found:
    print("Yes")
else:
    print("No")

How the Word Search Works

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.

Tracking the Search Position

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.

Key Takeaways

  • The lab checks whether letters of a word appear inside another string in the correct order, not merely whether they all exist.
  • Converting both strings with lower() makes the comparison case-insensitive.
  • text.find(ch, start) uses the two-argument form to decide where each search begins.
  • Updating start = pos + 1 after every match ensures the next letter is looked for only after the previous one.
  • A result of -1 from find() means the letter cannot be placed, so the answer becomes No.

Frequently Asked Questions

Why can't I just use the in operator for each letter?

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.

What does the second argument of find() do?

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.

Why do we let start equal pos + 1 instead of pos?

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.

What happens if find() returns -1?

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

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

Open Source: The Invisible Engine of Your Daily Life

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst

Python 4.3.1.10 LAB: Converting fuel consumption

Python for Windows Beginners: Build Your First Automated Workflow in 10 Minutes