To read an input image and print it into an array in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Read an image from a file into an array. Use plt.imread() method.Print the Numpy array of the image.To turn off the axis, use axis('off') method.To display the figure, use Show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True im = plt.imread("forest.jpg") print("Numpy array of the image is: ", im) im = plt.imshow(im) plt.axis('off') plt.show()OutputIt will produce the following output −On the ... Read More
Suppose, we have to build a string 'str' that is of length n. To build the string, we can perform two operations.A character can be added to the end of str for cost a.A substring sub_str can be added to the end of the str for cost r.We have to calculate the minimum cost of building the string str.So, if the input is like a = 5, r = 4, str = 'tpoint', then the output will be 29.To build the string 'tpoint', the cost is described below −str = 't'; a new character added, therefore the cost is 5. ... Read More
To create minor ticks for a polar plot in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create r (radius) and theta data points using numpy.Add a subplot to the current figure.Iterate the points between 0 to 360 with step=10 and plot them to get the ticks.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # radius and theta for the polar plot r = np.arange(0, 5, 0.1) theta = 2 ... Read More
To plot an animated image matrix in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a figure and a set of subplots.Make an animation by repeatedly calling a function *update*.Inside the update method, create a 6×6 dimension of matrix and display the data as an image, i.e., on a 2D regular raster.Turn off the axes using set_axis_off().To display the figure, use Show() method.Examplefrom matplotlib.animation import FuncAnimation import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() def ... Read More
Suppose we have a binary string s. Now let us consider an operation, where we split the string into two non-empty substrings s1 and s2. The score of this split is the sum of "0"s count in s1 and sum of "1"s count in s2. We have to find the maximum score we can obtain.So, if the input is like s = "011001100111", then the output will be 8, because we can split the string like "01100" + "110111". Then, the score is 3 + 5 = 8.To solve this, we will follow these steps −ones := number of "1"s ... Read More
Suppose we have a given matrix, We have to find a new matrix res, whose dimension is same as the given matrix where each element in res[i, j] = sum of the elements of matrix[r, c] for each r ≤ i, and c ≤ j.So, if the input is like8274then the output will be8101521To solve this, we will follow these steps −if matrix is empty, thenreturn matrixR := row count of matrixC := column count of matrixfor r in range 1 to R - 1, dofor c in range 0 to C - 1, domatrix[r, c] := matrix[r, c] + ... Read More
To put a title for a curved line in Python Matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Create x and y data points such that the line would be a curve.Plot the x and y data points.Place a title for the curve plot using plt.title() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x and y data points x = np.linspace(-1, 1, 50) y = 2**x + 1 # Plot ... Read More
Suppose we have a list called nums and a value k, now let us consider an operation by which we can update the value of any number in the list. We have to find the length of the longest sublist which contains repeated numbers after performing at most k operations.So, if the input is like nums = [8, 6, 6, 4, 3, 6, 6] k = 2, then the output will be 6, because we can change 4 and 3 to 6, to make this array [8, 6, 6, 6, 6, 6, 6], and the length of sublist with all ... Read More
To plot the FFT (Fast Fourier Transform) of a signal with correct frequencies on the X-axis in matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Initialize two variables, N and m, to calculate nu.Create the signal (a sine wave) using numpy. Compute the one-dimensional discrete Fourier Transform.Return the Discrete Fourier Transform sample frequencies.Plot the freq and fourier transform data points.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True N = 256 t = np.arange(N) m ... Read More
Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where the first letter of each words have the same first letter.So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, the longest contiguous sublist is ["she", "sells", "seashells"]. The first letter for each words is 's'.To solve this, we will follow these steps −cnt := 1maxcnt := 0prev_char := blank stringfor each word in words, doif prev_char is empty, thenprev_char := first letter of wordotherwise when ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP