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.8 LAB: Anagrams

 Anagrams

Anagram Listen = Silent
Anagram Listen = Silent

[1] An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.[1] For example, the word binary into brainy and the word adobe into abode. The original word or phrase is known as the subject of the anagram. Any word or phrase that exactly reproduces the letters in another order is an anagram.

Now, lets see the lab question.

Scenario

An anagram is a new word formed by rearranging the letters of a word, using all the original letters exactly once. For example, the phrases "rail safety" and "fairy tales" are anagrams, while "I am" and "You are" are not.

Your task is to write a program which:

  • asks the user for two separate texts;
  • checks whether, the entered texts are anagrams and prints the result.

Note:

  • assume that two empty strings are not anagrams;
  • treat upper- and lower-case letters as equal;
  • spaces are not taken into account during the check - treat them as non-existent

Test your code using the data we've provided.

Test data

Sample input:

  • Listen
  • Silent

Sample output:

Anagrams

Sample input:

  • modern
  • norman

Sample output:

Not anagrams

Solution Code:

def check(s1, s2):
    s1 = s1.replace(" ", "")
    s2 =  s2.replace(" ", "")

    if(sorted(s1.lower()) == sorted(s2.lower())):
        print("The strings are anagrams.")
    else:
        print("The strings aren't anagrams.")        

s1 = input("Please Enter the First String: ")
s2 = input("Please Enter the Second String: ")
check(s1, s2)

Can you come up with a shorter / better solution? Please mention it in the comments 👀

Remember "Learn Less, Practice More"





References

1. Wikipedia contributors. (2022, February 1). Anagram. Wikipedia. Retrieved February 5, 2022, from https://en.wikipedia.org/wiki/Anagram

What Makes Two Words Anagrams

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, using every original letter exactly once. For example, "Listen" and "Silent" are anagrams, while "modern" and "norman" are not, because they use different sets of letters. LAB 2.5.1.8 asks you to write a program that reads two separate texts and decides whether they are anagrams, printing "Anagrams" or "Not anagrams" accordingly.

The lab adds a few important rules. Two empty strings are never considered anagrams, spaces must be ignored as if they did not exist, and upper- and lowercase letters should be treated as equal. Handling these rules is where most of the practical work lies, because they affect how the two inputs must be prepared before they can be compared.

Comparing by Sorted Letters

The solution in the post centers on a clever trick: two strings are anagrams if and only if they contain the same letters in the same quantities. The simplest way to check that is to sort the characters of both strings and see whether the sorted versions are identical. To apply the rules, the code first removes all spaces from each string with s.replace(" ", ""), then converts everything to uppercase or lowercase with lower() so the comparison ignores case.

Once prepared, the two strings are compared using sorted(s1.lower()) == sorted(s2.lower()). The sorted() function returns a list of the characters arranged in order, and comparing those lists reveals whether both strings are built from the same multiset of letters. If they match, the program prints that the strings are anagrams; otherwise it prints that they are not. This elegant approach avoids any manual counting of individual letters.

Key Takeaways

  • Two strings are anagrams when they use the same letters the same number of times.
  • Spaces are stripped out with replace(" ", "") before comparing.
  • Converting both strings with lower() makes the comparison case-insensitive.
  • Comparing sorted() versions of the two strings reveals whether they share the same letters.
  • The program reports "Anagrams" or "Not anagrams" based on whether the sorted lists are equal.

Frequently Asked Questions

Why does sorting help detect anagrams?

Sorting both strings arranges their characters in the same order. If the two strings are anagrams, their sorted character lists will be identical, since the same letters appear the same number of times.

How are spaces ignored in the check?

The line s.replace(" ", "") removes every space character from the string before comparison, effectively treating spaces as if they did not exist.

Why convert to lowercase first?

Because the lab treats uppercase and lowercase letters as equal, converting each string to lowercase with lower() ensures that letter case does not affect the result.

What makes "modern" and "norman" not anagrams?

The strings contain different letters. "modern" has a d but no second a, while "norman" has two a's and a different set of letters, so their sorted character lists do not match.

Comments

Popular Posts

PROJECT MAVEN | The Architecture of Algorithmic Warfare

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

Open Source: The Invisible Engine of Your Daily Life

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

Python 4.3.1.10 LAB: Converting fuel consumption