Software engineering and personal development

Tag: learn (Page 1 of 4)

How to Quickly Check if 2 Strings Are Anagrams using Counter

Anagrams are strings that have the same letters but in a different order for example abc, bca, cab, acb, bac are all anagrams, since they all contain the same letters.

We can check whether two strings are anagrams in Python in different ways. One way to do that would be to use Counter from the collections module.

From the documentation:

 A Counter is a dict subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

In plain English, with Counter, we can get a dictionary that represents the frequency of elements in a list. Let us see this in an example:

 from collections import Counter
 ​
 print(Counter("Hello"))  # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})

Now we can use Counter to quickly check whether two strings are anagrams or not:

 from collections import Counter
 ​
 ​
 def check_if_anagram(first_string, second_string):
     first_string = first_string.lower()
     second_string = second_string.lower()
     return Counter(first_string) == Counter(second_string)
 ​
 ​
 print(check_if_anagram('testinG', 'Testing'))  # True
 print(check_if_anagram('Here', 'Rehe'))  # True
 print(check_if_anagram('Know', 'Now'))  # False

We can also check whether 2 strings are anagrams using sorted():

 def check_if_anagram(first_word, second_word):
     first_word = first_word.lower()
     second_word = second_word.lower()
     return sorted(first_word) == sorted(second_word)
 ​
 print(check_if_anagram("testinG", "Testing"))  # True
 print(check_if_anagram("Here", "Rehe"))  # True
 print(check_if_anagram("Know", "Now"))  # False

That’s basically it.

I hope you find this useful.

How to Iterate a Dictionary in a Single Line in Python

man in black long sleeve shirt wearing black headphones sitting on chair
Photo by Nubelson Fernandes on Unsplash

Python is known to be the language that gives you the ability to write a few lines of code that can do a lot of things.

Just a few Python lines can be enough to pull data from an API that you can then also visualize in a fancy diagram.

Or even a single line of code can be enough for you to do plenty of things.

There are a lot of Python one-liners online that you can find.

Continue reading

How to Quickly Change the Case of Strings in Python

laptop on brown wooden table
Photo by XPS on Unsplash

Strings are pretty common in our day-to-day programming lives. One common task that we may need to do is perform a switch from one case to another one.

Of course, it’s not supposed to be a standalone task in Jira. Rather, it can be just a tiny portion of a larger task. 

Since your time is valuable and you can invest it in more useful things, it is worth knowing that you can do such switches quickly in Python with built-in functions without having you do any implementation.

Continue reading
« Older posts

© 2024 Fatos Morina

Theme by Anders NorenUp ↑