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 566 of 2109
Writing files in the background in Python
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 MoreSum 2D array in Python using map() function
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 MoreRun Length Encoding in Python
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 MorePrint a Calendar in Python
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 MoreGet a google map image of specified location using Google Static Maps API in Python
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 MoreFind current weather of any city using OpenWeatherMap API in Python
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 MoreCount all prefixes in given string with greatest frequency using Python
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 MoreIncreasing Triplet Subsequence in Python
An increasing triplet subsequence is a sequence of three numbers from an array where each number is smaller than the next one, and they appear in the same order as in the original array (but not necessarily consecutive). The problem asks us to determine if such a triplet exists in an unsorted array. Problem Definition Given an array, return True if there exists indices i, j, k such that: arr[i] < arr[j] < arr[k] 0 ≤ i < j < k ≤ n-1 Otherwise, return False. Algorithm Approach We use a ...
Read MoreOdd Even Linked List in Python
An odd-even linked list groups all odd-positioned nodes together followed by even-positioned nodes. The positions are 1-indexed, so the 1st, 3rd, 5th nodes are odd positions, and 2nd, 4th, 6th are even positions. For example, if we have nodes [1, 22, 13, 14, 25], the result will be [1, 13, 25, 22, 14] where odd-positioned nodes (1, 13, 25) come first, followed by even-positioned nodes (22, 14). Algorithm To solve this problem efficiently in-place ? If head is null or has only one node, return head Use ...
Read MoreLongest Increasing Subsequence in Python
The Longest Increasing Subsequence (LIS) problem asks us to find the length of the longest subsequence in an array where elements are in strictly increasing order. For example, in the array [10, 9, 2, 5, 3, 7, 101, 18], the LIS is [2, 3, 7, 101] with length 4. Algorithm Overview We'll use a binary search approach with O(n log n) time complexity ? Create a tails array to store the smallest tail element for each possible LIS length For each number, use binary search to find its correct position Update the tails array and track ...
Read More