Suppose we have an array called nums, containing decimal digits of a number. For example, [2, 5, 6] is for 256. We have to add 1 with this number and return the list in same format as before.So, if the input is like nums = [2, 6, 9], then the output will be [2, 7, 0].To solve this, we will follow these steps −i := size of nums - 1while i >= 0, doif nums[i] + 1 = 0: if nums[i] + 1
Suppose we have a list called reviews and a threshold value t. Each item in reviews[i] has [x, y] means product i had x number of 5-star rating and y number of reviews. We have to find the minimum number of additional 5-star reviews we need so that the percentage of 5-star reviews for those items list is at least t percent.So, if the input is like reviews = [[3, 4], [1, 2], [4, 6]] threshold = 78, then the output will be 7, as in total there were 8 5-star reviews and 12 reviews. To reach 78% 5-star reviews, ... Read More
Suppose we have two strings s and a set of query Q. Where Q[i] contains pair (l, r), for each substring of s from l to r, we have to find number of substrings s from x to y where they are similar. Two strings s and t are similar if they follow these rules −They are of same lengthFor each pair of indices (i, j), if s[i] is same as s[j], then it must satisfy t[i] = t[j], and similarly if s[i] is not same as s[j], then t[i] and t[j] must be different.So, if the input is like ... Read More
Suppose, we have two strings. We want to make a lexically minimum string from those strings. To make the string we compare the first letter of the two strings and extract the lexically smaller letter from one of the strings. In the case of a tie i.e, the letters are the same; we extract the letter from the first string. We repeat this process until both the strings are empty. The minimal string constructed has to be returned.So, if the input is like input_1 = 'TUTORIALS', input_2 = 'POINT', then the output will be POINTTUTORIALSIf we compare the two strings, ... Read More
Suppose we are given an array and asked to perform insertion sort on it. In insertion sort, each element in an array is shifted to its correct position in the array. We have to find out the total number of shifts required to sort an array. The total number of shifts is an integer number and if the array is already sorted, we return 0.So, if the input is like input_array = [4, 5, 3, 1, 2], then the output will be 8[4, 5, 3, 1, 2] = 0 shifts [4, 5, 3, 1, 2] = 0 shifts ... Read More
plt.Show() would help whenever there is no interactive plot.fig.Show() would help to display all the figures if it is interactive.Let's take an example to observe the difference between plt.Show() and fig.Show().StepsOpen iPython shell.Set the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Plot a line using plot() method.Display the figure using Show() method.To display the figure, use Show() method with block=False.Exampleimport numpy as np from matplotlib import pyplot as plt # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create a new figure fig ... Read More
To save a 3D-plot in a PDF with Python, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a new figure or activate an existing figure.Add an 'ax' to the figure as part of a subplot arrangement.Create u, v, x, y and z data points using numpy.Plot a 3D wireframe.Set the title of the plot.Save the current figure using savefig() method.Exampleimport matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111, projection='3d') u, v = np.mgrid[0:2 * np.pi:30j, ... Read More
To control the border of a bar patch in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create a list of heights and a tuple for labels.Use the bar() method with edgecolor in the argument to control the color of the bar patch. Here we have used edgecolor='green'.Set the ticks and labels of the X-axis.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 height = [3, 12, 5, 18, 45] labels = ('P1', 'P2', 'P3', 'P4', ... Read More
To plot two different spaced time series on one same plot using Matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Create x1, y1 and x2 and y2 data points.Create a figure and a set of subplots.Plot the data that contains dates, with (x1, y1) and (x2, y2) data points.Set the major formatter of the X-axis ticklabels.Rotate xtick label by 45 degrees using tick_params() method.To display the figure, use Show() method.Exampleimport matplotlib.pyplot as plt from matplotlib.dates import date2num, DateFormatter import datetime as dt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True ... Read More
To change bar chart values to percentage in matplotlib, we can take the following stepsStepsSet the figure size and adjust the padding between and around the subplots.Make a list of frequencies.Create a new figure or activate an existing figure.Make a bar plot using bar() method.Iterate the bar plot and find the height of each patch and use annotate() method to put values in percentages.To display the figure, use Show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True frequencies = [7, 8, 5, 3, 6] plt.figure() p1 = ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP