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
Server Side Programming Articles
Page 581 of 2109
Count and Say in Python
The Count and Say sequence is a fascinating pattern where each term describes the previous term by counting consecutive identical digits. Let's understand how this sequence works and implement it in Python. Understanding the Sequence The Count and Say sequence starts with "1" and each subsequent term describes the previous term ? Term 1: "1" Term 2: "11" (one 1) Term 3: "21" (two 1s) Term 4: "1211" (one 2, one 1) Term 5: "111221" (one ...
Read MoreImplement strStr() in Python
The strStr() function finds the first occurrence of a substring within a string and returns its index. This is similar to the strstr() function in C programming. If the substring is not found, it returns -1. Algorithm To implement strStr(), we use a two-pointer approach with the following steps ? Initialize i = 0 (for main string), j = 0 (for substring) If substring is empty, return 0 While i < length of string and remaining characters >= substring length: If characters match, compare the entire substring If complete match found, return starting index Otherwise, ...
Read MoreMerge Two Sorted Lists in Python
Merging two sorted lists is a common programming problem where we combine two ordered sequences into a single sorted sequence. Python provides several approaches including recursion, iteration, and built-in methods like heapq.merge(). For example, if A = [1, 2, 4, 7] and B = [1, 3, 4, 5, 6, 8], the merged result will be [1, 1, 2, 3, 4, 4, 5, 6, 7, 8]. Using Recursion with Linked Lists The recursive approach works by comparing the first elements of both lists and selecting the smaller one ? class ListNode: def ...
Read MoreLongest Common Prefix in Python
Finding the longest common prefix among a set of strings is a common programming problem. The longest common prefix is the initial substring that appears at the beginning of all strings in the array. If no common prefix exists, we return an empty string. For example, given the array ["school", "schedule", "scotland"], the longest common prefix is "sc" since it appears at the start of all three strings. Algorithm Approach We start by taking the first string as our reference. Then we iterate through each remaining string, comparing characters one by one. When we find a mismatch, ...
Read MoreRoman to Integer in Python
Converting Roman numerals to integers is a common programming problem. Roman numerals use specific symbols with defined values, and some combinations follow subtraction rules. Roman Numeral Values The basic Roman numeral symbols and their integer values are ? Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Subtraction Rules Some Roman numerals use subtraction instead of addition ? I can be placed before V (5) and X (10) ...
Read MoreTwo Sum in Python
The Two Sum problem is a classic coding challenge where you find two numbers in an array that add up to a specific target. Given an array of integers and a target sum, return the indices of the two numbers that add up to the target. For example, if the array is [2, 8, 12, 15] and the target is 20, the solution returns indices [1, 2] because 8 + 12 = 20. Algorithm Approach We use a hash map (dictionary) to store each number and its index as we iterate through the array. For each element, ...
Read MoreAlternate range slicing in list (Python)
Slicing is used to extract a portion of a sequence (such as a list, tuple, or string) or other iterable objects. Python provides a flexible way to perform slicing using the slice notation with optional start, stop, and step parameters. Syntax list[start:stop:step] What is Alternate Range Slicing? Alternate range slicing retrieves elements from a list by skipping elements at a fixed interval using the step parameter. For example, if we want every second element from a list, we set step=2. Basic Alternate Slicing Example 1: Every Second Element Extract every second ...
Read MoreBoolean Indexing in Pandas
Boolean indexing in Pandas allows you to select data from DataFrames using boolean vectors. This powerful feature lets you filter rows based on True/False values, either through boolean indices or by passing boolean arrays directly to the DataFrame. Creating a DataFrame with Boolean Index First, let's create a DataFrame with a boolean index vector − import pandas as pd # data data = { 'Name': ['Hafeez', 'Srikanth', 'Rakesh'], 'Age': [19, 20, 19] } # creating a DataFrame with boolean index vector data_frame = pd.DataFrame(data, index=[True, False, ...
Read MoreCalendar Functions in Python -(monthrange(), prcal(), weekday()?)
Python's calendar module provides useful functions for working with dates and calendars. We'll explore three key methods: monthrange(), prcal(), and weekday() for date calculations and calendar display. calendar.monthrange(year, month) The monthrange() method returns a tuple containing the starting weekday number (0=Monday, 6=Sunday) and the number of days in the specified month ? Example import calendar # Getting month information for January 2019 year = 2019 month = 1 weekday, no_of_days = calendar.monthrange(year, month) print(f'First weekday of the month: {weekday}') print(f'Number of days: {no_of_days}') # Let's also check what weekday 1 means weekdays ...
Read MoreMulti-Line printing in Python
Python offers several ways to print multiple lines of text. Instead of using multiple print() statements, you can use triple quotes, escape characters, or other techniques for cleaner multi-line output. Using Triple Quotes Triple quotes allow you to create multi-line strings that preserve formatting and line breaks ? print(''' Motivational Quote: Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Motivational Quote: Sometimes later becomes never, Do it now. Great things ...
Read More