Sometimes, we require starting index of a word and also the last index of that word. A sentence is made up of words that are separated by spaces. In this Python article, using two different examples, the two different ways of finding the beginning and ending indices of all words in a sentence or a given string, are given. In the first example, the process of simple iteration through all the characters of a string is followed while finding the spaces to mark the beginning of words. In example 2, a Natural Language Toolkit is used for the task of ... Read More
In Golang, like other programming languages, we can code the logic to sort an array that has 0’s, 1’s, and 2’s as elements. Sorting means assigning the data either in increasing order or in decreasing order. This is one of the famous questions on the array asked in interviews. There can be two approaches to achieve this that we are going to explore one by one. For example, we have an array 2, 1, 0, 0, 1, 2, and after sorting the array will look like 0, 0, 1, 1, 2, 2. Method 1 In this example, we are going ... Read More
A number is said to be an Armstrong number if the digits are taken individually and raised to the power of the total digits and then these subparts are added together and they give the number itself. Here, in this Python example, using two different examples, the method of finding the total sum of n-digit Armstrong numbers is given. In example 1, the method to calculate the sum of all the 3 digit Armstrong numbers is given. In example 2, the number of digits can be decided by the user at runtime. This program is tested using digit values 4 ... Read More
In programming, to search for anything from an array, linked List, or from any other data structures we have a few search algorithms, one of which is Linear search. In the linear search, we iterate over the data structure from starting and search for the element till the last index. The advantage of a linear search algorithm is that we can perform this search of both sorted and unsorted data. The disadvantage is that for sorted or unsorted both kinds of data it will take the same amount of time to find an element. For example, we have an array ... Read More
Sometimes the task is to separate the negative and positive numbers or to separate the odd numbers and even numbers from a list of integers. Here, in this Python article, both these tasks are done. First negative numbers are separated into another list. Then the positive odd and positive even numbers are separated into separate lists. To calculate the sum, the numbers from these negative number lists, positive odd number lists, and positive even number lists are taken to calculate sums separately. In the two different examples, the main list of integers is formed. In example 1, the negative and ... Read More
A multiple of a number n is also divisible by the same number n. If a number M is a multiple of a number n, then if we divide M by n, its remainder must be zero. Also to create the multiples of number n within a given range, we can add n multiple times and check that the resulting number is within the given range or not. Another way to make multiples of a given number n, within a given range, is to find the first multiple in the given range for n and then to find the value ... Read More
In this article, the given task is to find the total string weight. To calculate the string weight, we have converted the given strings into the lower form. Considering the weight of the characters, we have taken a=1, b=, 2 and so up to z=26. In this Python article, using two different examples, the approaches to find the given string’s weight are given. In the first example, given characters in a string are fetc, hed, and then their respective weight is added to the updated weight. In example 2, first the frequency of occurrence of a given character in the ... Read More
In programming, there is a coding problem of binary tree that gets asked very frequently in interviews and the problem statement is to find the right view of a binary tree. If we try to understand the problem statement more than what exactly the right view is then we can explain it in a way that all the nodes are visible when you see them by standing on the right side of the tree. Illustration Let us understand more with the help of an example. Suppose we have a below tree and if we stand on the right side ... Read More
In Python, we can replace values in Column based on conditions in Pandas with the help of various inbuilt functions like loc, where and mask, apply and lambda, etc. Pandas is a Python library that is used for data manipulation and work with structured data. In this article, we will replace values in columns based on conditions in Pandas. Method 1: Using loc The loc function is used to access a group of rows and columns in a DataFrame. We can use this function to replace values in a column based on some condition. Syntax df.loc[row_labels, column_labels] The loc ... Read More
In Python, we can replace a word in Excel with another word using a third-party Python library called openpyxl. Microsoft Excel is a useful tool that is used for managing and analyzing data. Using Python we can automate some of the Excel data management tasks. In this article, we will understand how we can replace a word in Excel using Python. Installing openpyxl Before implementing the program to replace Word in Excel we need to install the openpyxl library in our system using the Python package manager. To install openpyxl type the following command on your terminal or command ... Read More