ProblemJavaScript function that takes in two arrays, pushed and popped, as the first and the second argument. Both these arrays are guaranteed to consist of unique elements.Our function should return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack, false otherwise.For example, if the input to the function is −const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1];Then the output should be −const output = true;Output ExplanationWe might do the following sequence −push(1), push(2), push(3), push(4), pop() -> ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.A move consists of choosing any arr[i], and incrementing it by 1. Our function is supposed to return the least number of moves to make every value in the array arr unique.For example, if the input to the function is −const arr = [12, 15, 7, 15];Then the output should be −const output = 1;Output ExplanationBecause if we increment any 15 to 16, the array will consist of all unique elements.ExampleThe code for this will be − Live Democonst ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the first and the only argument.Our function should check whether the input array is a centrally peaked array or not. If it is a centrally peaked array, we should return true, false otherwise.The conditions for being a centrally peaked array are −arr.length >= 3There exists some i with 0 < i < arr.length - 1 such that:arr[0] < arr[1] < ... arr[i-1] < arr[i]arr[i] > arr[i+1] > ... > arr[arr.length - 1]For example, if the input to the function is −const arr = ... Read More
To show the origin, we can take the following Steps −Create the points x, y1 and y2 using numpy.Plot the sine and cosine curves using plot() methods.Plot the vertical line, i.e., x=0.Plot the horizontal line, i.e., y=0.Intersection point of (Step 3 and 4), could be the origin.To display the label of lines, use legend() method.To display the figure, use show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 10, 50) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, c="orange", label="y=sin(x)") plt.plot(x, y2, c="green", label="y=cos(x)") plt.axvline(x=0, c="red", label="x=0") plt.axhline(y=0, c="yellow", ... Read More
To change the color of data points based on some variable in matplotlib, we can take the following steps −Create x, y and c variables using numpy.Plot the scatter points using x, y and for color, use c (Step 1).To display the image, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(1, 20, 50) y = np.log(x) c = np.random.randint(x) plt.scatter(x, y, c=c) plt.show()Output
To put a legend outside the plot with Pandas, we can take the following Steps −Make a dictionary d with keys Column1 and Column2.Make a data frame using DataFrame (d).Plot the data frame with a list of styles.Using legend(), place a legend on the figure. The bbox_to_anchor keyword gives a great degree of control for manual legend placement. For example, if you want your axes legend located at the figure's top right-hand corner instead of the axes' corner, simply specify the corner's location, and the coordinate system of that location.To display the figure, use the show() method.Exampleimport pandas as pd from ... Read More
To save image for maximized window instead of default size, we can use the following Steps −Create figure with figsize=(7.50, 3.50), using the figure() method.Plot the lines using the plot() method with list, color=”red”, and linewidth=2.Save the figure using the savefig() method.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() plt.plot([1, 3, 7, 3, 1], c="red", lw=2) plt.savefig("full_image.png") plt.show()Output
To show multiple figures in matplotlib, we can take the following Steps −To create a new figure, or activate an existing figure, use the figure() method. (Create two figures namely, Figure1 and Figure2).Plot the lines with the same lists (colors red and green and linewidth 2 and 5).Set the title of the plot over both the figures.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig1 = plt.figure("Figure 1") plt.plot([1, 3, 7, 3, 1], c="red", lw=2) plt.title("I am the part of figure 1") fig2 = plt.figure("Figure 2") plt.plot([1, 3, ... Read More
To position a textbox automatically in matplotlib, we can take the following Steps −Create xpoints from 1 to 2 and 100 samples.Create y1points and y2points using xpoints (Step 1) and numpy.Plot xpoints, y1points and y2points using the plot() method.To set the label, use the legend() method. It will help to position the text box.To display the figure, use the show() method.Exampleimport numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True xpoints = np.linspace(1, 2, 100) y1points = np.log(xpoints) y2points = np.exp(xpoints) plt.plot(xpoints, y1points, label="Log") plt.plot(xpoints, y2points, label="Exp") plt.legend() plt.show()OutputRead More
Definingadd_axes − Add an axes to the figure.add_subplot − Add an axes to the figure as part of a subplot arrangement.StepsCreate a new figure, or activate an existing figure, using the figure() method.Add an axes to the figure as part of a subplot arrangement where nrows=2, ncols=2. At index 1, add the title "subtitle1" and at index 2, add the title "subplot2".Create points for four rectangles and use add_axes() method to add an axes to the figure.To display the figure, use the show() method.Examplefrom matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() fig.add_subplot(221) plt.title("subplot1") fig.add_subplot(222) ... Read More