Python Articles

Page 319 of 855

Program to find the indexes where the substrings of a string match with another string fully or differ at one position in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 303 Views

When working with string matching problems, we often need to find substrings that either match exactly or differ by only one character. This article demonstrates how to find starting indexes in a string where substrings match another string completely or differ at exactly one position. Given two strings where the first string is longer than the second, we need to identify all starting positions where substrings from the first string either match the second string exactly or differ by only one character. Problem Example If we have string1 = 'tpoint' and string2 = 'pi', the output should ...

Read More

Program to find maximum possible value of an expression using given set of numbers in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 426 Views

We need to find the maximum possible value of an expression (sum1)² + (sum2)² where sum1 and sum2 are sums of elements from non-empty subsets of two arrays nums1 and nums2. Given two arrays with the same number of elements, we select indices and calculate the sum of corresponding elements from each array, then find the maximum value of the sum of their squares. Problem Understanding For arrays nums1 = [-1, 6] and nums2 = [5, 4], possible calculations are ? Using index 0: (-1)² + (5)² = 1 + 25 = 26 Using index ...

Read More

Program to find out the similarity between a string and its suffixes in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 249 Views

In this problem, we need to find the similarity between a string and all its suffixes. The similarity is defined as the length of the longest common prefix between the original string and each suffix. We then sum up all these similarities. For example, if the string is 'abcd', the suffixes are 'abcd', 'bcd', 'cd', 'd'. We compare each suffix with the original string to find how many characters match from the beginning. Understanding the Problem Let's see how this works with the string 'tpotp' ? Original string: 'tpotp' Suffixes and their similarities: 'tpotp' → ...

Read More

Program to find out the letter at a particular index in a synthesized string in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 173 Views

Suppose, we are given a string input_str. We need to find all possible substrings from the given string, then concatenate all the substrings in lexicographic order into another string. Given an integer value k, our task is to return the letter at index k from the concatenated string. So, if the input is like input_str = 'pqrs', k = 6, then the output will be 'p'. Understanding the Problem The substrings from the given string 'pqrs' in lexicographic order are: p, pq, pqr, pqrs, q, qr, qrs, r, rs, s. If we concatenate these strings, it ...

Read More

How to curve text in a polar plot in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 935 Views

Creating curved text in a polar plot requires plotting lines along curved paths and positioning text elements along angular coordinates. This technique is useful for creating circular labels, curved annotations, and artistic text layouts in matplotlib polar plots. Basic Setup First, let's create a basic polar plot with curved lines ? import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import interp1d plt.rcParams["figure.figsize"] = [8, 6] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection="polar") # Create radial lines at specific angles for degree in [0, 90, 180, 270]: ...

Read More

Matplotlib – Difference between plt.subplots() and plt.figure()

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 2K+ Views

In Matplotlib, plt.figure() and plt.subplots() are two fundamental functions for creating plots, but they serve different purposes and return different objects. Understanding plt.figure() plt.figure() creates a new figure object or activates an existing one. It returns a Figure object but doesn't create any axes by default. import matplotlib.pyplot as plt import numpy as np # Create a figure using plt.figure() fig = plt.figure(figsize=(8, 5)) fig.suptitle("Using plt.figure()") # Add data manually using plt.plot() x = np.linspace(0, 10, 50) y = np.sin(x) plt.plot(x, y, 'b-', label='sin(x)') plt.xlabel('X values') plt.ylabel('Y values') plt.legend() plt.grid(True) plt.show() ...

Read More

Program to find out the sum of numbers where the correct permutation can occur in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 355 Views

Given a number n, we need to find all possible permutations of positive integers up to n, sort them lexicographically, and number them from 1 to n!. When some values in a "special permutation" are forgotten (replaced with 0s), we must find all permutations that could match the original and sum their lexicographic positions. For example, if the special permutation is [0, 2, 0] with n=3, the possible original permutations are [1, 2, 3] (position 2) and [3, 2, 1] (position 5), giving us a sum of 7. Algorithm Steps The solution uses factorial number system and ...

Read More

Matplotlib – How to show the coordinates of a point upon mouse click?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 9K+ Views

In Matplotlib, you can capture mouse click coordinates on a plot by connecting an event handler to the figure's canvas. This is useful for interactive data exploration and annotation. Setting Up Mouse Click Detection The key is to use mpl_connect() to bind a function to the 'button_press_event' ? import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True def mouse_event(event): if event.xdata is not None and event.ydata is not None: print('x: {:.3f} and y: {:.3f}'.format(event.xdata, event.ydata)) ...

Read More

How to read an input image and print it into an array in matplotlib?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 1K+ Views

To read an input image and print it into an array in matplotlib, we can use plt.imread() to load the image as a NumPy array and plt.imshow() to display it. Steps Import matplotlib.pyplot Read an image from a file using plt.imread() method Print the NumPy array representation of the image Display the image using plt.imshow() Use plt.axis('off') to hide axis labels Show the plot using plt.show() Example with Sample Data ...

Read More

Program to determine the minimum cost to build a given string in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 1K+ Views

Suppose we have to build a string str of length n. To build the string, we can perform two operations: Add a character to the end of str for cost a Add a substring that already exists in the current string for cost r We need to calculate the minimum cost of building the string str using dynamic programming. Example If the input is a = 5, r = 4, str = 'tpoint', then the output will be 29. To build the string 'tpoint', the ...

Read More
Showing 3181–3190 of 8,546 articles
« Prev 1 317 318 319 320 321 855 Next »
Advertisements