To enforce axis range in matplotlib, we can take the following steps −Set x and y limits using xlim and ylim methods, respectively.Create x and y points for the curve using numpy.Plot x and y using the plot() method.To show the figure, use the show() method.Exampleimport matplotlib.pyplot as plt import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)]) y = np.random.randint(100, size=x.shape) plt.plot(x, y) plt.show()Output
In this tutorial, we are going to write a program that finds the length of union of segments of a line.We are given starting and ending points of the line segment and we need to find the length of union of segments of the line.The algorithm that we are going to use is called klee's algorithm.Let's see the steps to solve the problem.Initialise the array with coordinates of all the segments.Initialise a vector called points with double the size of segments array.Iterate over the segments array.Fill the values of points array at index i * 2 with the first point ... Read More
In this tutorial, we are going to write a program that checks whether the given number is Keith Number or not.The number n is called Keith number if it appears in the sequence generated using its digits. The sequence has first n terms as digits of the number n and other terms are recursively evaluated as sum of previous n terms.Let's see the steps to solve the problem.Initialise the number n.Initialise an empty vector elements to store the sequence.Count the digits and add every digit to the vecor.Reverse the digits vector.Initialise a variable with 0 called next element.Write a loop ... Read More
In this tutorial, we are going to write a program that finds whether the given number is kaprekar number or not.Take a number. Find the square of that number. Divide the number into two parts. If the sum of the two parts is equal to the original number, then the number is called kaprekar number.Let's see the steps to solve the problem.Initialise the n.Find the square of the n.Find the number of digits in the square of the n and store it in a variable.Divide the square of n with 10, 100, 1000, etc.., until the digits count.Check whether sum ... Read More
In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Initialise a empty ordered set.Iterate over the array and insert each element to the array.Iterate over the set from 0 to k - 1.Return the value.ExampleLet's see the code. Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) { set set; for (int i = 0; i < n; i++) { set.insert(arr[i]); } auto it = set.begin(); for (int ... Read More
In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.Let's see the steps to solve the problem.Initialise the array and k.Sort the array using sort method.Return the value from the array with the index k - 1.ExampleLet's see the code. Live Demo#include using namespace std; int findKthSmallestNumber(int arr[], int n, int k) { sort(arr, arr + n); return arr[k - 1]; } int main() { int arr[] = { 45, 32, 22, 23, 12 }, n = 5, k = 3; cout
To plot a time series in Python using matplotlib, we can take the following steps −Create x and y points, using numpy.Plot the created x and y points using the plot() method.To display the figure, use the show() method.Exampleimport matplotlib.pyplot as plt import datetime import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.array([datetime.datetime(2021, 1, 1, i, 0) for i in range(24)]) y = np.random.randint(100, size=x.shape) plt.plot(x, y) plt.show()Output
In this tutorial, we are going to write a program that finds the k-th least element from the min-heap.We will use priority queue to solve the problem. Let's see the steps to complete the program.Initialise the min-heap with correct values.Create a priority queue and insert the root node of the min-heap.Write a loop that iterates k - 1 times.Pop the least element from the queue.Add the left and right nodes of the above node into the priority queue.The greatest element in priority queue is the k-th greatest element now.Return it.ExampleLet's see the code. Live Demo#include using namespace std; struct Heap ... Read More
In this tutorial, we are going to write a program that finds the k-th boom number.The number that contains only 2 and 3 are called boom number.Let's see the steps to solve the above problem.Initialise the value of k.Initialise a queue of strings.Push the empty string to the queue.Initialise a counter variable to 0.Write a loop that iterates till counter is less than or equal to the given k.Get the front of the queue.Pop the element from the queue.Store the front of the queue in a variable.Push the number after appending 2 to the front.Increment the counter and check whether ... Read More
In this tutorial, we are going to write a program that finds out the smallest element after removing some integers from the natural numbers.We have given an array of elements and k value. Remove all the elements from natural numbers that are present in the given array. And then find the k-th smallest number from the remaining natural numbers.Let's see the steps to solve the problem.Initialise the array and k.Initialise an array and initialise all the elements with 0 except the elements present in the given array.Write a loop that iterates till the size of the given array.Decrement the value ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP