Introduction This is a frequently asked question in Python. In this article, I will only provide a short summary. For more details, you may refer to the full article here. So what is the deal about the yield keyword in Python ? The yield keyword in Python has to do with generator functions. Let us
Introduction Assume we have a directory structure (ex. product source code) and we want to share it with someone. To do that on Unix, we can tar the directory structure (convert to a single file archive) then compress the file. If we do not pay due attention, original file and directory paths can be retained
Problem In this Python code snippet, we are going to implement a naive run length encoding algorithm to compress strings Solution The idea is very simple, text has redundancy so instead of sending plain text, we can compress text by sending characters and their counts. This is a typical text processing problem that gets asked
Problem Write a Python function that checks if two strings are rotation of each other Solution Rotating a string means inserting a number of characters from the end of the string to the beginning of the string. Any easy trick to check if two strings are rotation of each other is to concatenate one string
Problem Implement an algorithm in Python to check if a given input string is a permutation of another palindrome string Solution If the number of occurrences of all characters in the string is even then the input string can be a permutation of a palindrome string. There is only once exception which is the character
Problem Write an algorithm in Python to check if two strings are permutations of each other Solution Let us differentiate between permutations and combinations. An easy way is to think about permutations in terms of lists and combinations in terms of sets. Take a look at the code snippets below… Code Thanks for visiting. Please
Problem In this beginner level code snippet, we are going to test multiple variables against a value in Python. Check out the code snippet below Code Thanks for visiting. Please use the comments section below for feedback
Problem Given a string of characters. Implement an algorithm in Python to check if the string has all unique characters. Solution One solution is to get the unique characters in the string using a set. If the length of the input string is the same as the length of the set then the string has
Introduction Python programming language does not have a main function to mark the entry point of the program as in C++. Any Python script (i.e. module) can have executable code, function and class definitions. When a script is passed to the Python interpreter, the executable code gets run automatically. In other words, a module can
Introduction Welcome to a new code snippets post. Today, we are going to summarize the use of with statement and context managers in Python. In coding, a typical scenario is to acquire a resource, use it and finally release the resource. If an exception occurs, the program may not have a chance to free the