Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 118 of 2547
Program to find running sum of 1d array in Python
A running sum array calculates the cumulative sum at each position. For a given array, the running sum at index i equals the sum of all elements from index 0 to i. For example, if nums = [8, 3, 6, 2, 1, 4, 5], then the running sum will be [8, 11, 17, 19, 20, 24, 29] because: rs[0] = nums[0] = 8 rs[1] = sum of nums[0..1] = 8 + 3 = 11 rs[2] = sum of nums[0..2] = 8 + 3 + 6 = 17 and so on Method 1: Using ...
Read MoreProgram to find Final Prices With a Special Discount in a Shop in Python
Given an array of prices, we need to find the final prices after applying a special discount. For each item at index i, we get a discount equal to the price of the first item at index j where j > i and prices[j] ≤ prices[i]. Problem Understanding The discount rule works as follows ? For item at index i, look for the first item at index j where j > i If prices[j] ≤ prices[i], then discount = prices[j] Final price = prices[i] − prices[j] If no such j exists, no discount is applied ...
Read MoreProgram to delete n nodes after m nodes from a linked list in Python
Suppose we are given a linked list that has the start node as "head", and two integer numbers m and n. We have to traverse the list and delete some nodes such that the first m nodes are kept in the list and the next n nodes after the first m nodes are deleted. We perform this until we encounter the end of the linked list. We start from the head node, and the modified linked list is to be returned. The linked list structure is given to us as ? Node value ...
Read MoreHow to add a variable to Python plt.title?
To add a variable to Python plt.title(), you can use string formatting methods like f-strings, format(), or the % operator. This allows you to dynamically include variable values in your plot titles. Basic Example with Variable in Title Here's how to include a variable in your matplotlib title ? import numpy as np import matplotlib.pyplot as plt # Set figure properties plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create data points x = np.linspace(-1, 1, 10) num = 2 y = num ** x # Plot with variable in title plt.plot(x, y, ...
Read MoreHow do I apply some function to a Python meshgrid?
A meshgrid creates coordinate matrices from coordinate vectors, allowing you to apply functions across all combinations of input values. Python's NumPy provides efficient ways to apply functions to meshgrids using vectorization. Basic Function Application with Lists You can apply functions to coordinate vectors using NumPy's vectorize decorator ? import numpy as np @np.vectorize def foo(a, b): return a + b x = [0.0, 0.5, 1.0] y = [0.0, 1.0, 8.0] print("Function Output:", foo(x, y)) Function Output: [0. 1.5 9. ] Creating and Using Meshgrids ...
Read MoreHow can I plot a single point in Matplotlib Python?
To plot a single data point in matplotlib, you can use the plot() method with specific marker parameters. This is useful for highlighting specific coordinates or creating scatter-like visualizations with individual points. Basic Single Point Plot Here's how to plot a single point with customized appearance ? import matplotlib.pyplot as plt # Define single point coordinates x = [4] y = [3] # Set axis limits and add grid plt.xlim(0, 5) plt.ylim(0, 5) plt.grid(True) # Plot the single point plt.plot(x, y, marker="o", markersize=15, ...
Read MoreHow to normalize a histogram in Python?
To normalize a histogram in Python, we can use the hist() method with the density=True parameter. In a normalized histogram, the area underneath the plot equals 1, making it useful for probability distributions and comparisons. What is Histogram Normalization? Histogram normalization scales the bars so that the total area under the histogram equals 1. This converts frequency counts into probability densities, making it easier to compare datasets of different sizes. Basic Normalization Example Here's how to create a normalized histogram using matplotlib − import matplotlib.pyplot as plt import numpy as np # Sample ...
Read MoreHow to plot a 3D density map in Python with Matplotlib?
A 3D density map visualizes data density across a 2D plane using colors to represent values at different coordinates. Python's Matplotlib provides the pcolormesh() function to create these density maps efficiently. Steps to Create a 3D Density Map To plot a 3D density map in Python with matplotlib, we can take the following steps: Create coordinate data using numpy.linspace() to generate evenly spaced points Generate coordinate matrices using meshgrid() from the coordinate vectors Create density data using mathematical functions (like exponential functions) Plot the ...
Read MoreHow to show an Axes Subplot in Python?
To show an axes subplot in Python, we can use the show() method from matplotlib. This method displays the figure window and renders all the plots that have been created. When multiple subplots are created, show() displays them together in a single window. Basic Subplot Display Steps Import matplotlib.pyplot and numpy Create x and y data points using numpy Plot x and y using plot() method To display the figure, use show() method Example from matplotlib import pyplot as plt ...
Read MoreDrawing multiple figures in parallel in Python with Matplotlib
To draw multiple figures in parallel in Python with Matplotlib, we can create subplots within a single figure window. This technique allows you to display multiple visualizations side by side for easy comparison. Steps to Create Multiple Subplots Create random data using numpy Add subplots to the current figure using subplot(nrows, ncols, index) Display data as an image using imshow() with different colormaps Use show() to display the complete figure Example Here's how to create four subplots in a single row, each ...
Read More