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
Articles by SaiKrishna Tavva
Page 2 of 8
How to have logarithmic bins in a Python histogram?
In Python, creating a logarithmic histogram involves using logarithmically spaced bins instead of linear ones. This is particularly useful when your data spans several orders of magnitude. We can achieve this using NumPy for generating logarithmic bins and matplotlib for plotting. Logarithmic bins are spaced exponentially rather than linearly, making them ideal for data that follows power-law distributions or spans wide ranges. Basic Example with Logarithmic Bins Let's create a simple histogram with logarithmic bins ? import matplotlib.pyplot as plt import numpy as np # Create sample data data = np.random.exponential(scale=2, size=1000) # ...
Read MoreHow to find the first date of a given year using Python?
The datetime module in Python can be used to find the first day of a given year. This datetime module is widely used for manipulating dates and times in various formats and calculations. Common approaches to find the first day of a given year using Python are as follows ? Datetime Module − Widely used library for manipulating dates and times in various ways. Calendar Module ...
Read MorePython Program to Implement Binary Search without Recursion
To implement binary search without recursion, the program uses an iterative approach with a while loop. Binary search is an efficient algorithm that finds a specific element in a sorted list by repeatedly dividing the search space in half. This method compares the target with the middle element of the current search range. If they match, the search is complete. If the target is smaller, it searches the left half; if larger, it searches the right half. This process continues until the element is found or the search space is exhausted. Algorithm Steps The iterative binary search ...
Read MoreRotate Matrix in Python
Rotating a matrix in Python can be done using various approaches. The most common method is the Transpose and Reverse technique, which converts rows to columns and then reverses each row to achieve a 90-degree clockwise rotation. Original Matrix Let's consider a 3×3 matrix that we want to rotate 90 degrees clockwise ? 1 5 7 9 6 3 2 1 3 Using Transpose and Reverse This method is efficient and involves two main steps ? Transpose the matrix ? Convert rows to columns and ...
Read MoreCheck if one list is subset of other in Python
Python provides various methods to check if one list is a subset of another. A subset means all elements of the smaller list exist in the larger list. We'll explore three effective approaches: all() function, issubset() method, and intersection() method. Using all() Function The all() function returns True if all elements in an iterable are true, otherwise False. We can combine it with a generator expression to check if every element of the sublist exists in the main list − # Define the main list and the sublist main_list = ['Mon', 'Tue', 5, 'Sat', 9] sub_list ...
Read MoreFind Peak Element in Python
A peak element in an array is defined as an element that is greater than its neighbors. We can use some common approaches such as binary search to efficiently locate a peak element and linear search algorithm which involves iterating through an array. What is a Peak Element? A peak element is an element that is greater than or equal to its adjacent neighbors. For elements at the edges of the array, we only consider the existing neighbor ? # Example array with peak elements nums = [1, 3, 20, 4, 1, 0] # Here, 20 ...
Read MoreWord Search in Python
In Python, word search refers to finding if a given word exists in a 2D grid. The word can be formed by sequentially connecting adjacent cells horizontally or vertically. This problem is commonly solved using backtracking and Depth-First Search (DFS) algorithms. Algorithm Overview The word search algorithm follows these key steps: Iterate through each cell in the 2D grid For each cell matching the first character, start a DFS search Use backtracking to explore all four directions (up, down, left, right) ...
Read MoreFile Upload Example in Python
File upload in Python can be implemented using the CGI (Common Gateway Interface) environment. This involves creating an HTML form for file selection and a Python script to handle the server−side file processing. The file upload process consists of two main components: an HTML form that allows users to select files, and a Python CGI script that processes and saves the uploaded files to the server. Creating HTML Form for File Upload The HTML form uses to create a file selection field and for the upload button. The form must include enctype="multipart/form-data" to handle file ...
Read MoreLongest Substring Without Repeating Characters in Python
In Python, finding the longest substring without repeating characters is a classic string problem. We can solve this using different approaches: the Brute Force method which checks all possible substrings, and the more efficient Sliding Window technique using either a set or dictionary to track character positions. Common Methods The main approaches to find the longest substring without repeating characters are: Brute Force: Checks all possible substrings and verifies if they have unique characters. Sliding Window with Set: Uses a set to track characters in the current window and adjusts window boundaries dynamically. ...
Read MorePass by reference vs value in Python
In Python Call by Value and Call by Reference are two types of generic methods to pass parameters to a function. In the Call-by-value method, the original value cannot be changed, whereas in Call-by-reference, the original value can be changed. Call by Value in Python When we pass an argument to a function, it is stored locally (in the stack memory), i.e the scope of these variables lies within the function and these will not affect the values of the global variables (variables outside function). In Python, "passing by value" is possible only with the immutable types ...
Read More