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 507 of 2547
Program to find column index where left most 1 is present in a binary matrix in Python?
Suppose we have a 2D binary matrix where each row is sorted in ascending order with 0s coming before 1s. We need to find the leftmost column index that contains the value 1. If no such column exists, return -1. So, if the input is like ? 0 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 Then the output will be 2, as column index 2 has the leftmost 1 in the entire matrix. Algorithm To solve this ...
Read MoreProgram to convert linked list by alternating nodes from front and back in Python
Given a singly linked list, we need to rearrange it by alternating nodes from the back and front. The pattern is: last node, first node, second-last node, second node, and so on. For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8, 9], the output will be [9, 1, 8, 2, 7, 3, 6, 4, 5]. Algorithm To solve this problem, we follow these steps − Store all node values in a list for easy access from both ends Traverse the linked list again and assign values alternately from the back ...
Read MoreProgram to find number of ways we can arrange symbols to get target in Python?
Suppose we have a list of non-negative numbers called nums and also have an integer target. We have to find the number of ways to arrange + and - signs in front of nums such that the expression equals the target. So, if the input is like nums = [2, 3, 3, 3, 2] target = 9, then the output will be 2, as we can have −2 + 3 + 3 + 3 + 2 and 2 + 3 + 3 + 3 − 2. Algorithm Approach This problem can be transformed into a subset sum ...
Read MoreProgram to find number of arithmetic sequences from a list of numbers in Python?
Finding arithmetic sequences from a list of numbers is a common problem in programming. An arithmetic sequence is a sequence where the difference between consecutive numbers remains constant. We need to count all contiguous arithmetic subsequences of length ≥ 3. For example, in the list [6, 8, 10, 12, 13, 14], we have arithmetic sequences: [6, 8, 10], [8, 10, 12], [6, 8, 10, 12], and [12, 13, 14]. Algorithm Approach The key insight is to use a sliding window approach: Track consecutive elements that form arithmetic sequences When a sequence breaks, calculate how many ...
Read MoreHow to restrict argument values using choice options in Python?
When building command-line applications in Python, you often need to restrict user input to specific valid values. Python's argparse module provides the choices parameter to limit argument values to predefined options, preventing invalid input and improving data validation. Basic Argument Parser Without Restrictions Let's start with a simple tennis Grand Slam title tracker that accepts any integer value ? import argparse def get_args(): """Function to parse command line arguments""" parser = argparse.ArgumentParser( description='Tennis Grand Slam title tracker', ...
Read MoreHow to use one or more same positional arguments in Python?
Python's argparse module allows you to handle multiple positional arguments of the same type using the nargs parameter. This is particularly useful when you need exactly N arguments of the same data type, like performing arithmetic operations on numbers. Using nargs for Same Type Arguments The nargs parameter specifies how many command-line arguments should be consumed. When you set nargs=2, argparse expects exactly two values of the specified type. Example: Subtracting Two Numbers Let's create a program that subtracts two integers using positional arguments − import argparse def get_args(): ...
Read MoreHow to plot pie-chart with a single pie highlighted with Python Matplotlib?
Pie charts are one of the most popular visualization types for displaying percentages and proportions. In this tutorial, we'll learn how to create pie charts with highlighted segments using Python's Matplotlib library. Basic Pie Chart Setup First, let's install and import the required library − import matplotlib.pyplot as plt # Sample data: Tennis Grand Slam titles tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Murray', 3)) # Extract titles and player names titles = [title for player, title in tennis_stats] players = [player for player, title in tennis_stats] print("Titles:", titles) print("Players:", players) ...
Read MoreHow to plot 4D scatter-plot with custom colours and cutom area size in Python Matplotlib?
A 4D scatter plot in Matplotlib allows you to visualize four dimensions of data simultaneously: X and Y coordinates, point size (area), and color. This is useful for analyzing relationships between multiple variables in a single visualization. Installing Matplotlib First, install matplotlib using pip ? pip install matplotlib Basic 2D Scatter Plot Let's start with a simple 2D scatter plot using tennis player statistics ? import matplotlib.pyplot as plt # Tennis player data (name, grand slam titles) tennis_stats = (('Federer', 20), ('Nadal', 20), ('Djokovic', 17), ('Sampras', 14), ...
Read MoreHow to extract required data from structured strings in Python?
When working with structured strings like log files or reports, you often need to extract specific data fields. Python provides several approaches to parse these strings efficiently when the format is known and consistent. Understanding Structured String Format Let's work with a structured report format: Report: - Time: - Player: - Titles: - Country: Here's our sample data: report = 'Report: Daily_Report - Time: 2020-10-10T12:30:59.000000 - Player: Federer - Titles: 20 - Country: Switzerland' print(report) Report: Daily_Report - Time: 2020-10-10T12:30:59.000000 - Player: Federer - ...
Read MoreHow to create Microsoft Word paragraphs and insert Images in Python?
Creating Microsoft Word documents programmatically in Python is essential for automating report generation. The python-docx library provides a simple interface to create paragraphs, add text formatting, and insert images into Word documents. Installing python-docx First, install the required library using pip ? pip install python-docx Creating Paragraphs and Adding Text Start by creating a new document and adding paragraphs with text ? import docx # Create a new document word_doc = docx.Document() # Add a paragraph paragraph = word_doc.add_paragraph('1. Hello World, Some Sample Text Here...') run = paragraph.add_run() ...
Read More