Python Articles

Page 672 of 855

Index Pairs of a String in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Finding index pairs of substrings within a text is a common string matching problem. Given a text string and a list of words, we need to find all index pairs [i, j] where the substring text[i:j+1] matches any word in the list. Problem Understanding For example, with text "ababa" and words ["aba", "ab"], we find overlapping matches ? "ab" at positions [0, 1] and [2, 3] "aba" at positions [0, 2] and [2, 4] Algorithm Steps The approach uses nested loops to check all possible substrings ? Initialize an empty result ...

Read More

Fixed Point in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

A fixed point in an array is an index where the element's value equals its position. Given a sorted array of unique integers, we need to find the smallest index i such that A[i] == i. For example, in the array [-10, -5, 0, 3, 7], index 3 has value 3, making it a fixed point. Algorithm To solve this problem, we follow these steps − Iterate through each index from 0 to length of array If i == A[i], return i as the fixed point If no fixed point is found, return -1 ...

Read More

Adding K to each element in a Python list of integers

Disha Verma
Disha Verma
Updated on 25-Mar-2026 1K+ Views

In this article, we will learn how to add a constant K value to each element in a Python list of integers. A list is a data type in Python that stores a sequence of items separated by commas ? items = [item1, item2, item3...] Suppose we have a list of integers and a constant value "k." We need to add this "k" to each item in the list. For example ? # Input numbers = [5, 10, 15, 20] k = 5 # Output: On adding 5 to each element ...

Read More

Writing files in the background in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 633 Views

In this tutorial, we will learn about multi-threading in Python to perform multiple tasks simultaneously. Python's threading module allows us to write files in the background while executing other operations concurrently. We'll demonstrate this by writing data to a file in the background while calculating the sum of numbers in a list. Here are the key steps involved ? Import the threading module Create a class inheriting from threading.Thread Implement the file writing logic in the run() method Start the background ...

Read More

Sum 2D array in Python using map() function

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 5K+ Views

In this tutorial, we are going to find the sum of a 2D array using map() function in Python. The map() function takes two arguments: function and iterable. It applies the function to every element of the iterable and returns a map object. We can convert the map object into a list or other iterable. Let's see how to find the sum of a 2D array using the map function ? Steps to Sum a 2D Array Initialize the 2D array using nested lists. Pass the sum ...

Read More

Run Length Encoding in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 2K+ Views

Run-length encoding compresses a string by grouping consecutive identical characters and representing them as character + count. For example, "aaabbc" becomes "a3b2c1". However, the example above shows a different approach - counting total occurrences of each character rather than consecutive runs. Let's explore both approaches. Character Frequency Encoding This approach counts total occurrences of each character ? import collections def character_frequency_encoding(string): # Initialize ordered dictionary to maintain character order count_dict = collections.OrderedDict.fromkeys(string, 0) # Count occurrences of ...

Read More

Print a Calendar in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 13K+ Views

In this tutorial, we are going to learn how to print the calendar of month and year using the calendar module of Python. Python's built-in calendar module makes it straightforward to display formatted calendars. We only need the year and month numbers. Printing a Year Calendar To print a complete year calendar, follow these steps ? Import the calendar module Initialize the year number Use calendar.calendar(year) to generate the calendar Example # importing the calendar module import calendar # ...

Read More

Get a google map image of specified location using Google Static Maps API in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 4K+ Views

Google provides a Static Maps API that returns a map image on HTTP request. We can directly request for a map image with different parameters based on our needs. We need to create a billing account on Google to use this API. You can visit the Google Static Maps API documentation for more details on getting an API key. Prerequisites Before starting, make sure you have ? A valid Google Maps API key with Static Maps API enabled The requests module installed (pip install requests) Steps to Get Map Image Follow these ...

Read More

Find current weather of any city using OpenWeatherMap API in Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 9K+ Views

In this tutorial, we will retrieve the current weather of any city using the OpenWeatherMap API in Python. OpenWeatherMap provides a free weather API that allows up to 60 calls per minute without cost. Prerequisites Before getting started, you need to: Create a free account at OpenWeatherMap Generate your API key from the dashboard Install the requests module if not already available Required Modules We need two Python modules for this tutorial − requests − for making HTTP requests to the API json − for parsing the JSON response (built-in module) ...

Read More

Count all prefixes in given string with greatest frequency using Python

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 689 Views

In this tutorial, we will write a Python program that finds all prefixes of a string where one character appears more frequently than another character. This is useful for analyzing character frequency patterns in strings. Given a string and two characters, we need to find all prefixes where the first character has a higher frequency than the second character, then display the count of such prefixes. Examples Example 1 For string "apple" with characters 'p' and 'e' − Input: string = "apple", char1 = 'p', char2 = 'e' Output: ap app appl apple ...

Read More
Showing 6711–6720 of 8,549 articles
« Prev 1 670 671 672 673 674 855 Next »
Advertisements